Spring AOP(Aspect)不执行

时间:2013-01-22 14:44:32

标签: spring spring-mvc aop 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 ==================");
    }
}

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

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

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

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

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

由于

6 个答案:

答案 0 :(得分:24)

我不确定我是否做得不错,但对我来说解决它的问题是将@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;
    }
}

(只是为了关闭循环 - 如果您使用的是注释,请不要忘记将@EnableAspectJAutoProxy添加到您的Config类。

@EnableAspectJAutoProxy

答案 1 :(得分: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>

谢谢你们

答案 2 :(得分:4)

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

@Configuration
@EnableAspectJAutoProxy
@ComponentScan
public class MyConfig {
    @Bean
    public LoggingAspect loggingAspect(){
        return new LoggingAspect();
    }
}

答案 3 :(得分:1)

如果您还没有尝试过...尝试基于xml的spring-aop配置,如下所示:

    <aop:config>
    <aop:aspect ref="loggingAspect">
        <aop:pointcut expression="execution(* uk.co.txttools.web.controller.compose.PreviewMessageController.set*(..))" id="previewMessageControllerSetters"/>
        <aop:before method="setLoggingAdvice" pointcut-ref="previewMessageControllerSetters"/>

         // set other 2 pointcuts similarly....
        </aop:aspect>       
    </aop:config>
    <bean id="loggingAspect" class="uk.co.txttools.aspects.LoggingAspect" />

答案 4 :(得分:1)

仅使可能的答案列表完整:

在我看来,您似乎在maven pom.xml中缺少以下依赖项:

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-aop</artifactId>
  <version>2.5.6</version>
</dependency>

答案 5 :(得分:0)

在配置文件中尝试此操作:

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