I have aspectjrt-1.5.4.jar, aspectjweaver-1.6.1.jar, org.springframework.aop-3.0.5.RELEASE.jar in my applications classpath.
My applicationContext.xml contains
<aop:aspectj-autoproxy/> <bean id="loggerAspect" class="com.company.logging.LoggerAspect" />
My LoggerAspect.java contains
@Aspect public class LoggerAspect { @Before("execution( * com.company..*(..)) and !execution( * com.company.*.action..*(..))") public void logEntry(JoinPoint joinPoint) { Logger logger = Logger.getLogger(joinPoint.getTarget().getClass()); String className = joinPoint.getTarget().getClass().getCanonicalName(); logger.debug(className + " :: " + joinPoint.getSignature().getName() + " :: " + "Entry"); } @AfterReturning("execution( * com.company..*(..)) and !execution( * com.company.*.action..*(..))") public void logExit(JoinPoint joinPoint) { Logger logger = Logger.getLogger(joinPoint.getTarget().getClass()); String className = joinPoint.getTarget().getClass().getCanonicalName(); logger.debug(className + " :: " + joinPoint.getSignature().getName() + " :: " + "Exit"); } @AfterThrowing(pointcut = "execution(* com.company..*(..))", throwing = "throwable") public void logException(Throwable throwable) { System.out.println(throwable.getMessage()); } }
If i comment the logException pointcut, everything else works just fine. And entry and exit logs are generated. However if i uncomment it, the below exception occurs.
I am getting an error as
java.lang.NullPointerException org.apache.struts2.convention.ConventionUnknownHandler.handleUnknownActionMethod(ConventionUnknownHandler.java:412) com.opensymphony.xwork2.DefaultUnknownHandlerManager.handleUnknownMethod(DefaultUnknownHandlerManager.java:102) com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:439) com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:291) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:254) com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:133) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:207) com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:207) com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:94) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:166) com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:187) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52) org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:485) org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77) org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91) org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:368) org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:109) org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:83) org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380) org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:97) org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380) org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:100) org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380) org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:78) org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380) org.springframework.security.web.servletapi.SecurityContextHolderAwareRequest开发者_StackOverflowFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54) org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380) org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:35) org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380) org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.java:177) org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380) org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:187) org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380) org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105) org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380) org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:79) org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380) org.springframework.security.web.session.ConcurrentSessionFilter.doFilter(ConcurrentSessionFilter.java:109) org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380) org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:169) org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:237) org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:167)
6. i did the following changes
- Used aspectjweaver-1.6.10.jar. And removed the two old jars.
- Made the change as proposed by Oleg. (which was actually a silly mistake on my part). I have updated the logException method above with the latest that i am using.
I am not getting the above particular error anymore. However now as soon as my first struts action of the application (home.action) is called, the response is a blank screen.
If my logException method has pointcut = "execution(* com.company1..(..))" where company1 is not a valid package of my project, things work fine. But if i have it as pointcut = "execution( com.company..*(..))" where company is a valid package of my project, i get a blank response to every struts action
"I have aspectjrt-1.5.4.jar, aspectjweaver-1.6.1.jar" could be your problem. Besides being potentially incompatible to each other, they are very old versions. You should upgrade to 1.6.10 version of aspectjweaver and drop aspectjrt--aspectjweaver includes all classes in it already.
As far as I know correct definition of pointcut should looks like this
@AfterThrowing(pointcut = "execution(* com.company..*(..))", throwing = "exception")
public void logException(Throwable exception)
{
System.out.println(exception.getMessage());
}
Please pay attention that throwing should contain name of parameter you're binding in the method itself (exception in this case).
While I don't see how it relates to exception you're getting this might be the root cause.
I was trying to apply advice to Struts actions which are not Spring beans. I modified the method as below and it started working.
@AfterThrowing(pointcut = "execution( * com.company..*(..)) and !execution( * com.company.*.action..*(..))", throwing = "throwable")
public void logException(JoinPoint joinPoint,Throwable throwable)
{
Logger logger = Logger.getLogger(joinPoint.getTarget().getClass());
String className = joinPoint.getTarget().getClass().getCanonicalName();
logger.error(className + " :: " + joinPoint.getSignature().getName() + " :: " + "Exception", throwable);
}
Keeping the above in mind, i wonder if its always a bad idea to use * com.company..*(..)
.
And rather keep it specific by applying on specific packages such as service, business, data access etc.
精彩评论