如何在 Spring 3 中将 @Aspect 与 @Controller 结合使用? [英] How can I combine @Aspect with @Controller in Spring 3?

查看:23
本文介绍了如何在 Spring 3 中将 @Aspect 与 @Controller 结合使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

package my.package

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

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

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

My dispatcher-servlet has the following Controller handlers:

<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" />

Web 应用程序在提供的 Maven 工件上运行良好:

The webapp is running good with the supplied maven artifacts:

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

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

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

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

添加到我的 applicationContext.xml:

added to my applicationContext.xml:

<aop:aspectj-autoproxy/>

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

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

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

并开始充实@Aspect:

And started fleshing out the @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

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

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

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

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?

任何帮助将不胜感激.如果我忘记包含任何信息,请询问.希望有人能在这里帮助我.

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

非常感谢!

推荐答案

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

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

这篇关于如何在 Spring 3 中将 @Aspect 与 @Controller 结合使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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