Spring AOP intercepting some Public Method but not other - spring

I have placed and interceptor in my aspect class as
#Before("execution( * com.mycompany.a.service.weblab.b.doesTreatmentEqualsAndTrigger(..))"){
.........
}
this interceptor works fine but when i place this interceptor
#Before("execution( * mycompany.a.b.bsf.c.requestReply(..))"){
............
}
the code is not intercepted.
My aspect class is in com.mycompany.x package and both the function on which i am trying to place the interceptor are public as needed by spring AOP.
My .xml is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"
default-autowire="no">
<context:annotation-config/>
<aop:aspectj-autoproxy/>
<bean name="com.a.serviceInterceptor" class="com.mycompany.x.RequestSpecificServiceAspect"/>
</beans>
Why is the second function not getting intercepted? Is it because of the vastly different package since they don't have anything in common? If so then how i can resolve this ?

Related

codahale metrics aren't being recorded

I'm experimenting with the codahale metrics and metrics-spring packages. I've been unable to get the metrics recorded.
Function Call
#Counted
public Response getSomething(#Context final HttpServletRequest request) {
return Response.ok(doWork()).build();
}
metrics-spring.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:metrics="http://www.ryantenney.com/schema/metrics"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.ryantenney.com/schema/metrics
http://www.ryantenney.com/schema/metrics/metrics-3.0.xsd">
<!-- Registry should be defined in only one context XML file -->
<metrics:metric-registry id="metrics"/>
<!-- annotation-driven must be included in all context files -->
<metrics:annotation-driven metric-registry="metrics"/>
<!-- (Optional) Registry should be defined in only one context XML file -->
<metrics:reporter type="console" metric-registry="metrics" period="10s"/>
</beans>
beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml"/>
<import resource="classpath:metrics-spring.xml"/>
...snip....
Output
When I run this in a war file and hit the endpoint, the counter isn't incremented. It stays at 0
5/19/15 5:01:48 PM =============================================================
-- Counters --------------------------------------------------------------------
package.getSomething
count = 0
It seems like everything is wired together correctly. Data is getting dumped to the console and it knows about the counter. What am I doing wrong?

Replace cxf parameters with spring (dynamically)

I have following cxf.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:http="http://cxf.apache.org/transports/http/configuration"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/transports/http/configuration
http://cxf.apache.org/schemas/configuration/http-conf.xsd">
<http:conduit name="http://localhost:8080/mockInterfaceSoap">
<http:client ReceiveTimeout="5000" />
</http:conduit>
</beans>
Is there any way to replace hardcoded ReceiveTimeout parameter value with spring?
EDIT:
I want to change it dynamically in runtime. Change is quite rare so app can change existing endpoint or create new one.
Maybe org.apache.cxf.jaxrs.client.WebClient helps. I have found some time ago a reference to it and have used it in the following example:
String endpointAddress = "*";
WebClient client = WebClient.create(endpointAddress);
HTTPConduit httpConduit = WebClient.getConfig(client).getHttpConduit();
httpConduit.setTlsClientParameters(new TLSClientParameters());
httpConduit.getTlsClientParameters().setUseHttpsURLConnectionDefaultHostnameVerifier(true);
httpConduit.getTlsClientParameters().setUseHttpsURLConnectionDefaultSslSocketFactory(true);
return httpConduit;
You can use it as follows.
<jaxrs-client:client id="clientId" serviceName="clientServiceName"
address="${urlService}" >
</<jaxrs-client:client>
<http-conf:conduit name="*clientServiceName*.http-conduit" >
<http-conf:client ReceiveTimeout="1"/>
</http-conf:conduit>

Spring 3 JNDI look up in glassfish3

I want to look up some properties from JNDI configured in glassfish v3 server. I want to do it using spring. Here is my spring configuration:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.0.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml"/>
<import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
<!--
JNDI look ups.
!-->
<jee:jndi-lookup id="properties"
jndi-name="java:comp/env/jndi/ws_properties"
expected-type="java.util.Properties"/>
</beans>
I have mapped jndi/ws_properties in sun-web.xml and web.xml files. Problem is that this lookup always gives me null properties. But if I do it in java code:
try {
InitialContext context = new InitialContext();
properties = (Properties) context.lookup("jndi/ws_properties");
} catch (NamingException e) {
LOGGER.error("", e);
}
It is ok. I see my properties keys and values.
Could somebody tell me where is the problem here?
This is probably because of your "jndi-name" property.
You don't have to put "java:comp/env/" in the name.
The "resource-ref" property defaults to true and unless you set it to false, it will automatically add the java:comp/env to the name.

Adding Xml configurations to a Spring ApplicationContext that is already setup

I was wondering if there is a way to do the following:
have my wep app startup with its servle-context.xml
When, at a certain point, one particular bean in this xml config is instantiated, it will add it's own xml configuration to the application context (or to a child perhaps?).
I'm asking this because I want to pack some functionality in a stand alone library and then reuse it in different projects, so that initializing a bean of this library will load its xml config.
What I wrote is:
public class IrisLibHelper {
ApplicationContext context;
ApplicationContext irisContext;
#Required
#Autowired
public void setContext(ApplicationContext ctx){
this.context = ctx;
ClassPathXmlApplicationContext xap = new ClassPathXmlApplicationContext(ctx);
xap.setConfigLocation("classpath:com/dariodario/irislib/xmldefs/irisconfig.xml");
this.irisContext = xap;
}
public ApplicationContext getIrisContext() {
return irisContext;
}
public void setIrisContext(ApplicationContext irisContext) {
this.irisContext = irisContext;
}
}
and the irisconfig.xml is:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- <context:component-scan base-package="com.dariodario"></context:component-scan> -->
<mvc:annotation-driven />
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"
p:synchronizeOnSession="true" />
<context:component-scan base-package="com.dariodario.iris.controllers"></context:component-scan>
</beans>
The problem is that it doesn't seem to scan the com.dariodario.iris.controllers package, in fact the controllers don't get mapped! (I've logging debugging on and I don't seen anything).
Why not use the tag <import resource="classpath:applicationConfig.xml" /> ? You can load a spring configuration file which is in jar. In a jar, the Spring XML configuration is always at the root. But if not, you can use this notation: <import resource="${configurablePath}/applicationConfig.xml" /> where configurablePath can be reach by a property place holder or other.
I think this way is cleaner than merging two Spring context.

spring destroy-method + request scope bean

So I wanted to do something like this:
#Component
#Scope(value="request", proxyMode=ScopedProxyMode.INTERFACES)
public class MyBean {
#Autowired HttpServletRequest request;
#PreDestroy
public void afterRequest() {
try {
System.out.println("After request...");
// use request here:
}
finally {
System.out.println("Completed successfully...");
}
}
}
And I end up with the following message, AFTER the "Completed successfully..." message logs:
09:19:16 WARN Invocation of destroy method failed on bean with name 'scopedTarget.myBean': java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
I'm not really sure what to make of this, since my logging indicates the destroy method completed successfully. Does anyone know what's going on?
EDIT:
Here's the mvc-servlet.xml. As you can see there is not much going on here. It's all annotation driven:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-2.0.xsd">
<!-- properties file -->
<context:property-placeholder location="app.properties" />
<context:component-scan base-package="my.package.web" />
<context:component-scan base-package="my.package.services" />
<mvc:annotation-driven />
<bean class="org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:viewClass="org.springframework.web.servlet.view.JstlView" p:prefix="/WEB-INF/view" p:suffix=".jspx" />
</beans>
If you use request scope without spring MVC you should declare org.springframework.web.context.request.RequestContextListener in web-app listener.
<web-app>
...
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
...
</web-app>
check http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/beans.html#beans-factory-scopes-other-web-configuration
I never did get this working, but I ended up changing the code to apply #After advice on the controller methods, which has the same effect.

Resources