2014-11-10 4 views
0

Привет, я прочитал все предыдущие темы по моей проблеме, и я применил все предложения, но я не нашел решения.
В моей конфигурации springMVC + AspectJ контроллер работает нормально, но pointcut не выполняется. Вот мои конфиги и код:SpringMVC + AspectJ не называется

-- /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; 
    } 
} 
+0

Мое предположение, что у вас также есть 'DispatcherServlet', который загружает класс контроллера. Аспекты (AOP в целом) будут применяться только к компонентам в том же контексте. –

ответ

1

Ваш аннотированный метод: protected, но он должен быть public.

Spring manual, chapter 9.2.3 говорит:

Благодаря прокси на основе характера рамок АОП в Spring, защищенные методы по определению не перехватили, ни для JDK прокси (если это не применимо), ни для CGLIB прокси (где это технически возможно, но не рекомендуется для целей АОП). Как следствие, любой данный pointcut будет соответствовать только общедоступным методам!

Если вы хотите, чтобы соответствовать protected или private методы, использовать AspectJ через LTW.

+0

Huawuuu !!!! Это была проблема !!!! Методы на контроллере должны быть общедоступными. Многие ребята! – Max

+0

Не только на контроллерах ... – Modi