java 如何在 Spring 3 中将 @Aspect 与 @Controller 结合使用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3991249/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 04:16:47  来源:igfitidea点击:

How can I combine @Aspect with @Controller in Spring 3?

javamodel-view-controllerspringaopaspectj

提问by chzbrgla

I'm trying to setup a Spring 3 Web MVC project, using the @Controller, annotation-based approach.

我正在尝试使用基于注解的 @Controller 方法设置 Spring 3 Web MVC 项目。

package my.package

@Controller
@RequestMapping("/admin/*")
public class AdminMultiActionController {

@RequestMapping(value = "admin.htm", method = RequestMethod.GET)
public String showAdminSection() {
    return "admin";
}

My dispatcher-servlet has the following Controller handlers:

我的调度程序 servlet 具有以下控制器处理程序:

<context:component-scan base-package="my.package" />
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />

The webapp is running good with the supplied maven artifacts:

使用提供的 Maven 工件,webapp 运行良好:

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.0.2.RELEASE</version>
</dependency>

Now I wanted to add @AspectJ AOP. I got the libs:

现在我想添加@AspectJ AOP。我得到了库:

<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.9</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.6.9</version>
</dependency>

added to my applicationContext.xml:

添加到我的 applicationContext.xml:

<aop:aspectj-autoproxy/>

Made sure to create the relevant bean in the applicationContext.xml as well:

确保也在 applicationContext.xml 中创建相关的 bean:

<bean id="securityInterceptor" class="my.package.service.SecurityInterceptor"/>

And started fleshing out the @Aspect:

并开始充实@Aspect:

package my.package.service

@Aspect
public class SecurityInterceptor {

@Pointcut("execution(* showAdminSection(..))")// the pointcut expression
private void foo() {
    System.out.println("fooo");
}// the pointcut signature

Now this is where it stopped working. "fooo" is never printed.

现在这是它停止工作的地方。“foo”永远不会被打印出来。

Could it be, because the pointcutted (spelling?) objects must be spring-managed beans and my @Controller in combination with the DefaultAnnotationHandlerMapping is not perceived as such?

可能是因为切入点(拼写?)对象必须是弹簧管理的 bean,而我的 @Controller 与 DefaultAnnotationHandlerMapping 的结合不被认为是这样?

Any help would be appreciated. If I forgot to include any information, please ask. Hope someone can help me out here.

任何帮助,将不胜感激。如果我忘记包含任何信息,请询问。希望有人可以帮助我。

Thanks a lot!

非常感谢!

采纳答案by Affe

The pointcut method defines the pointcut, it doesn't get called on a match, you need to define something to actually happen. e.g.,

切入点方法定义了切入点,它不会在匹配时被调用,您需要定义一些实际发生的事情。例如,

@Before("foo()")
public void beforeFoo(JoinPoint joinPoint) {
    System.out.println("foooo");
}

回答by chzbrgla

Argh god.. finally got it working!

啊上帝..终于让它工作了!

Thanks for your reply Affe!

谢谢阿飞的回复!

For the curious:

对于好奇:

  1. Don't use component-scan and defaultannotationhandlers to get your controllers
  2. Wire them in spring xml
  3. Don't have said controllers in dispatcher-servlet whilst aop config sits in applicationContext.
  4. Move both to the dispatcher-servlet

  5. Of course Affe is right: don't forget the advice :p

  1. 不要使用组件扫描和默认注释处理程序来获取您的控制器
  2. 在 spring xml 中连接它们
  3. 当 aop 配置位于 applicationContext 时,不要在 dispatcher-servlet 中说控制器。
  4. 将两者都移动到调度程序 servlet

  5. 当然 Affe 是对的:不要忘记建议:p

回答by Scifiballer24

To second chzbrgla, for those who view this later (like me), my issue resolved after moving the controller component scanning and aop config to the dispatcher servlet like so:

第二个 chzbrgla,对于那些稍后查看此内容的人(如我),在将控制器组件扫描和 aop 配置移动到调度程序 servlet 后,我​​的问题解决了,如下所示:

...
<context:component-scan base-package="com.mypackage.controller"/>
<!--  Configure aspects. -->
<bean id="myAspect1" class="com.mypackage.intercept.SiteAccessAspect"/>

<aop:aspectj-autoproxy proxy-target-class="true">
    <aop:include name="myAspect1" />
</aop:aspectj-autoproxy>
...

As you can see, component scanning still worked in my case. Moving both to the dispatcher servlet solved it for me.

如您所见,组件扫描在我的情况下仍然有效。将两者都移动到调度程序 servlet 为我解决了这个问题。

The apspect (myApect1, in this case) was configured as follows:

appspect(在本例中为 myAspect1)配置如下:

@Pointcut("within(@org.springframework.stereotype.Controller *)")
public void controller() {}

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

@Before("controller() && method()")
public void doAccessCheck(JoinPoint joinPoint) {
System.out.println(joinPoint.getSignature()); // For testing purposes.
}