【问题标题】:SpringMVC+AspectJ not calledSpringMVC+AspectJ 未调用
【发布时间】:2014-11-10 10:38:30
【问题描述】:

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

【问题讨论】:

  • 我猜你还有一个 DispatcherServlet 正在加载控制器类。方面(通常是 AOP)只会应用于相同上下文中的 bean。

标签: spring aspectj spring-aop


【解决方案1】:

你的注解方法是protected,但应该是public

Spring manual, chapter 9.2.3 说:

由于 Spring 的 AOP 框架基于代理的性质,根据定义,受保护的方法不会被拦截,对于 JDK 代理(如果不适用)和 CGLIB 代理(这在技术上可行但不推荐用于 AOP目的)。因此,任何给定的切入点都只会与公共方法匹配!

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

【讨论】:

  • 哇哇哇!!!这就是问题!!!!控制器上的方法必须是公开的。非常感谢伙计们!!!
猜你喜欢
  • 2017-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-09
  • 2013-07-28
  • 1970-01-01
  • 1970-01-01
  • 2014-10-01
相关资源
最近更新 更多