在 Spring 3 中为控制器实现 AOP [英] implement AOP for Controllers in Spring 3

查看:42
本文介绍了在 Spring 3 中为控制器实现 AOP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用带注释的控制器实现 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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsdhttp://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></豆类>

控制器:

@Controller公共类 FooController {@RequestMapping(value="/index.htm", method=RequestMethod.GET)公共字符串显示索引(模型模型){返回索引";}}

方面:

@Aspect公共类 FooAspect {@Pointcut("@target(org.springframework.stereotype.Controller)")public void controllerPointcutter() {}@Pointcut("执行(* *(..))")public void methodPointcutter() {}@Before("controllerPointcutter()")公共无效beforeMethodInController(JoinPoint jp){System.out.println("### 控制器调用前...");}@AfterReturning("controllerPointcutter() && methodPointcutter() ")public void afterMethodInController(JoinPoin jp) {System.out.println("###返回后...");}@Before("methodPointcutter()")公共无效beforeAnyMethod(JoinPoint jp){System.out.println("### 在任何调用之前...");}}

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

解决方案

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

<context:include-filter type="aspectj"表达式="com.your.aspect.class.Here"/></context:component-scan>

我不知道这是否仅是 Spring 3.1 的要求,但我知道如果没有这个,您将无法在控制器 HandlerMethod 上放置方面.您会收到类似于以下内容的错误:

Caused by: java.lang.IllegalArgumentException: object is not an instance of declaring classHandlerMethod详情:控制器 [$Proxy82]方法 [public void com.test.TestController.testMethod(java.security.Principal,javax.servlet.http.HttpServletResponse) 抛出 javax.servlet.ServletException]已解决的论点:[0] [空][1] [type=com.ibm.ws.webcontainer.srt.SRTServletResponse] [value=com.ibm.ws.webcontainer.srt.SRTServletResponse@dcd0dcd]

如果您的方面位于不是控制器的类中的方法上,则不需要此项

How do I implement AOP with an annotated Controller?

I've search and found two previous posts regarding the problem, but can't seem to get the solutions to work.

posted solution 1

posted solution 2

Here's what I have:

Dispatch 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>

Controller:

@Controller
public class FooController {

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

Aspect:

@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...");
    }
}

The beforeAnyMethod() works for methods NOT in a controller; I cannot get anything to execute on calls to controllers. Am I missing something?

解决方案

In order to put an aspect on a HandlerMethod in a class annotated with @Controller in Spring 3.1 you need to have the proxy-target-class="true" attribute on the aspectj-autoproxy element. You also need to have the CGLIB and the ASM libraries as a dependency in your WAR/EAR file. You can either specify your annotated aspect class as a bean and use the aop:include as stated above or you can leave the aop:include out and add a filter similar to this in your component scan element:

<context:component-scan>
  <context:include-filter type="aspectj"
    expression="com.your.aspect.class.Here"/>
</context:component-scan>

I do not know if this is a requirement as a result of Spring 3.1 only but I know that without this, you will not be able to put an aspect on your controller HandlerMethod. You would get an error similar to:

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]

This is not required if your aspect is on a method in a class that is not a controller

这篇关于在 Spring 3 中为控制器实现 AOP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆