在Spring 3中为控制器实现AOP

1 aop spring controller

如何使用带注释的控制器实现AOP?

我已经搜索并找到了之前关于这个问题的两篇帖子,但似乎无法让解决方案起作用.

发布解决方案1

发布解决方案2

这就是我所拥有的:

派遣Servlet:

<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:p="http://www.springframework.org/schema/p"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

    <context:annotation-config/>
    <context:component-scan base-package="com.foo.controller"/>

    <bean id="fooAspect" class="com.foo.aop.FooAspect" />

    <aop:aspectj-autoproxy>
        <aop:include name="fooAspect" />
    </aop:aspectj-autoproxy>
</beans>
Run Code Online (Sandbox Code Playgroud)

控制器:

@Controller
public class FooController {

    @RequestMapping(value="/index.htm", method=RequestMethod.GET)
    public String showIndex(Model model){
        return "index";
    }
}
Run Code Online (Sandbox Code Playgroud)

方面:

@Aspect
public class FooAspect {

    @Pointcut("@target(org.springframework.stereotype.Controller)")
    public void controllerPointcutter() {}

    @Pointcut("execution(* *(..))")
    public void methodPointcutter() {}

    @Before("controllerPointcutter()")
    public void beforeMethodInController(JoinPoint jp){
        System.out.println("### before controller call...");
    }

    @AfterReturning("controllerPointcutter() && methodPointcutter() ")
    public void afterMethodInController(JoinPoin jp) {
        System.out.println("### after returning...");
    }

    @Before("methodPointcutter()")
    public void beforeAnyMethod(JoinPoint jp){
        System.out.println("### before any call...");
    }
}
Run Code Online (Sandbox Code Playgroud)

beforeAnyMethod()适用于不在控制器中的方法; 我无法在调用控制器时执行任何操作.我错过了什么吗?

mar*_*rio 9

为了将一个方面放在HandlerMethod中,@Controller在Spring 3.1中注释的类中,您需要proxy-target-class="true"aspectj-autoproxy元素上具有该属性.您还需要将CGLIB和ASM库作为WAR/EAR文件中的依赖项.您可以将带注释的方面类指定为bean,并使用aop:include上述方法,也可以aop:include在组件扫描元素中保留out并添加与此类似的过滤器:

<context:component-scan>
  <context:include-filter type="aspectj"
    expression="com.your.aspect.class.Here"/>
</context:component-scan>
Run Code Online (Sandbox Code Playgroud)

我不知道这是否只是Spring 3.1的结果,但我知道如果没有这个,你将无法在控制器HandlerMethod上放置一个方面.你会得到一个类似于的错误:

Caused by: java.lang.IllegalArgumentException: object is not an instance of declaring class
HandlerMethod details: 
Controller [$Proxy82]
Method [public void com.test.TestController.testMethod(java.security.Principal,javax.servlet.http.HttpServletResponse) throws javax.servlet.ServletException]
Resolved arguments: 
[0] [null] 
[1] [type=com.ibm.ws.webcontainer.srt.SRTServletResponse] [value=com.ibm.ws.webcontainer.srt.SRTServletResponse@dcd0dcd]
Run Code Online (Sandbox Code Playgroud)

如果您的方面是在不是控制器的类中的方法上,则不需要这样做


归档时间:

查看次数:

7159 次

最近记录:

13 年,5 月 前