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

查看:823
本文介绍了如何在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";
}

我的dispatcher-servlet具有以下Controller处理程序:

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

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

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。我得到了libs:

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

现在这是它停止工作的地方。
fooo从不打印。

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

可能是,因为切入点(拼写?)对象必须是spring-managed 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天全站免登陆