Issue
Hi I read all previous threads on my issue and i applied all suggestions, but i haven't found a solution.
In my springMVC+AspectJ config, controller works fine but the pointcut is not performed.
Here my configs and code:
-- /WEB-INF/web.xml ---
<web-app>
<display-name>MODULE-WEB</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
...
**
-- /WEB-INF/ApplicationContext.xml -----
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd"
>
<mvc:annotation-driven/>
<context:annotation-config/>
<context:component-scan base-package="it.max.test"/>
<!-- AOP -->
<aop:aspectj-autoproxy/>
<!-- EJB -->
<bean id="testService" class="org.springframework.ejb.access.SimpleRemoteStatelessSessionProxyFactoryBean">
<property name="jndiName" value="java:app/MODULE-EJB/TestService"/>
<property name="businessInterface" value="it.max.test.services.ITestService"/>
</bean>
</beans>
**
-- Monitor.java ---
package it.max.test.security;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.stereotype.Component;
@Component
@Target(value={ElementType.METHOD, ElementType.TYPE})
@Retention(value=RetentionPolicy.RUNTIME)
public @interface Monitor {
}
**
-- AuthorizationInterceptor.java --
package it.max.test.security;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.CodeSignature;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class AuthorizationInterceptor {
@Before(value="@within(it.max.test.security.Monitor) || @annotation(it.max.test.security.Monitor)")
public void before(JoinPoint jp){
Object[] paramValues=jp.getArgs();
String[] paramNames=((CodeSignature)jp.getStaticPart().getSignature()).getParameterNames();
for(int i=0;i<paramValues.length;i++){
System.out.println("["+paramNames[i]+"]:["+paramValues[i]+"]");
}
}
}
**
--TestController.java---
package it.max.test.controllers;
import it.max.test.security.Monitor;
import it.max.test.services.ITestService;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class TestController {
@Autowired
private ITestService testService;
@Monitor
@RequestMapping("/test.view")
protected ModelAndView test(HttpServletRequest arg0,HttpServletResponse arg1) throws Exception {
ModelAndView mv=new ModelAndView();
testService.test("AAA");
mv.setViewName("test");
return mv;
}
}
Solution
Your annotated method is protected
, but it should be public
.
Spring manual, chapter 9.2.3 says:
Due to the proxy-based nature of Spring’s AOP framework, protected methods are by definition not intercepted, neither for JDK proxies (where this isn’t applicable) nor for CGLIB proxies (where this is technically possible but not recommendable for AOP purposes). As a consequence, any given pointcut will be matched against public methods only!
If you want to match protected
or private
methods, use AspectJ via LTW.
Answered By - kriegaex