Spring AOP(Aspect)不执行

bha*_*vin 17 aop spring spring-mvc spring-aop

我使用Spring 2.5.6,asm 1.5.3,aspectjrt/aspectjweaver 1.6.1,cglib 2.1_3在我的基于Web的Spring应用程序中,我有以下类:

package uk.co.txttools.aspects;

@Aspect
public class LoggingAspect {
    @Before("execution(* uk.co.txttools.web.controller.compose.PreviewMessageController.set*(..))")
    public void setLoggingAdvice(){
        System.out.println("********************************* Advice run..... set mothod called....");
    }

    @AfterThrowing("execution(* uk.co.txttools.web.controller.compose.PreviewMessageController.onSubmit(..) throws java.lang.Exception)")
    public void hadleException(){
       System.out.println("================= PreviewMessageController =========== ON SUBMIT Exception Throwen ==================");
    }

    @Before("execution(* uk.co.txttools.web.controller.compose.PreviewMessageController.onSubmit(..) throws java.lang.Exception)")
    public void OnSubmitAspect(){
        System.out.println("================= PreviewMessageController =========== ON SUBMIT CALLED ==================");
    }
}
Run Code Online (Sandbox Code Playgroud)

我有一个Controller:uk.co.txttools.web.controller.compose.PreviewMessageController which hasonSubmit()method, which get called from web page. I have separateapplicationContext.xml`文件.

我的springapp-servlet.xml(在web.xml文件中使用org.springframework.web.servlet.DispatcherServlet)文件如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
            http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd">

<aop:aspectj-autoproxy proxy-target-class="true" />

<bean id="loggingAspect" class="uk.co.txttools.aspects.LoggingAspect" />
.
.
Run Code Online (Sandbox Code Playgroud)

下面在同一个xml文件中PreviewMessageController获取初始化,这意味着我的Controller和Aspect live是同一个容器.

我在运行应用程序时没有遇到任何异常,但我的方程类LoggingAspect永远不会被调用.我不确定那是什么遗失或我做错了.请帮我..

谢谢

小智 28

我不确定我是否做得不错,但对我来说解决它的是加入@Component"Aspect'ed"类 -

@Aspect
@Component
public class PerformanceLogger {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Around("within(com.something.rest.service..*)")
    public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {
        long start = System.currentTimeMillis();
        Object retVal = pjp.proceed();
        long end = System.currentTimeMillis();
        logger.debug(pjp.getSignature().toShortString() + " Finish: " + (end - start) + "ms");
        return retVal;
    }
}
Run Code Online (Sandbox Code Playgroud)

(并且只是关闭循环 - 如果您使用基于注释,请不要忘记添加@EnableAspectJAutoProxy到您的Config类.

@EnableAspectJAutoProxy
Run Code Online (Sandbox Code Playgroud)

  • @idipous 我相信清除这个问题的文档是 [link](https://docs.spring.io/spring/docs/3.1.x/spring-framework-reference/html/aop.html#aop-ataspectj) : _您可以在 Spring XML 配置中将方面类注册为常规 bean,或者通过类路径扫描自动检测它们 - 就像任何其他 Spring 管理的 bean 一样。但是,请注意@Aspect 注解对于类路径中的自动检测是不够的:为此,您需要添加单独的@Component 注解..._ (5认同)
  • 我想知道为什么在文档中对此不太清楚。它也解决了我的问题。 (2认同)

bha*_*vin 8

终于解决了.

我想我缺少aspectj-maven-plugin.它需要春天编织方面.没有教程提供此信息.在我的pom.xml中添加了以下内容.

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>aspectj-maven-plugin</artifactId>
    <version>1.0</version>
    <dependencies>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.6.1</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjtools</artifactId>
            <version>1.6.1</version>
        </dependency>
    </dependencies>
    <executions>
        <execution>
            <goals>
                <goal>compile</goal>
                <goal>test-compile</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <outxml>true</outxml>
        <verbose>true</verbose>
        <showWeaveInfo>true</showWeaveInfo>
        <aspectLibraries>
            <aspectLibrary>
                <groupId>org.springframework</groupId>
                <artifactId>spring-aspects</artifactId>
            </aspectLibrary>
        </aspectLibraries>
        <source>1.6</source>
        <target>1.6</target>
    </configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)

多谢你们

  • 不,只有当您想要使用完整的AspectJ加载时编织或编译时编织时,才需要该插件来编织Spring AOP方面.你做的是使用CTW.真正的解决方法是修复Spring AOP配置. (5认同)

Zak*_*ria 5

对于那些选择的人JavaConfig,您可以将your声明Aspect为bean并添加@EnableAspectJAutoProxy注释以启用自动代理:

@Configuration
@EnableAspectJAutoProxy
@ComponentScan
public class MyConfig {
    @Bean
    public LoggingAspect loggingAspect(){
        return new LoggingAspect();
    }
}
Run Code Online (Sandbox Code Playgroud)