Spring AOP(方面)未执行 [英] Spring AOP (Aspect) Not executing

查看:33
本文介绍了Spring AOP(方面)未执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I ams using Spring 2.5.6, asm 1.5.3, aspectjrt/aspectjweaver 1.6.1, cglib 2.1_3 In my Web based Spring application I have following class:

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其中有 onSubmit() 方法,可以从网页中调用.我有单独的applicationContext.xml` 文件.

I have one Controller:uk.co.txttools.web.controller.compose.PreviewMessageController which hasonSubmit()method, which get called from web page. I have separateapplicationContext.xml` file.

我的springapp-servlet.xml(在带有org.springframework.web.servlet.DispatcherServlet的web.xml文件中使用)文件看起来像这样:

My springapp-servlet.xml(which is used in web.xml file with org.springframework.web.servlet.DispatcherServlet) file looks like this:

<?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 得到初始化,这意味着我的控制器和 Aspect live 是同一个容器.

Below in same xml file PreviewMessageController get initialize which means my Controller and Aspect live is same container.

我在运行应用程序时没有遇到任何异常,但我的方面类 LoggingAspect 从未被调用.我不确定缺少什么或我做错了什么.请帮帮我..

I don't get any exception while running application but my aspect class LoggingAspect never get called. I'm not sure what is that missing or I'm doing wrong. Please help me..

谢谢

推荐答案

终于解决了.

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

I think I was missing aspectj-maven-plugin. It required for spring to weaving of aspects. None tutorial provide this information though. Added following to my 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>

谢谢各位

这篇关于Spring AOP(方面)未执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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