没有调用SpringMVC + AspectJ

时间:2014-11-10 10:38:30

标签: spring aspectj

您好我在我的问题上阅读了所有以前的主题并且我应用了所有建议,但我还没有找到解决方案。
在我的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;
    }
}

1 个答案:

答案 0 :(得分:1)

您的带注释的方法为protected,但应为public

Spring manual, chapter 9.2.3说:

  

由于Spring的AOP框架基于代理的特性,根据定义,受保护的方法既不是针对JDK代理(这不适用),也不针对CGLIB代理(这在技术上可行,但不建议用于AOP)目的)。因此,任何给定的切入点都只能与公共方法匹配!

如果您想匹配protectedprivate方法,请通过LTW使用AspectJ。