【问题标题】:Spring AOP with AspectJ not intercepting带有 AspectJ 的 Spring AOP 不拦截
【发布时间】:2015-09-25 06:22:11
【问题描述】:

我正在编写我的第一个 AOP。我已经粘贴了下面的代码,它不会在方法调用中被拦截。 我不确定可能是什么原因。

在控制台上它只打印:

addCustomerAround() 正在运行,args : dummy

并且没有打印任何 AOP 建议代码。

有人可以帮忙吗?

界面:

package com.test.model;

import org.springframework.beans.factory.annotation.Autowired;

public interface AopInterface {


    @Autowired
    void addCustomerAround(String name);
}

类:

package com.test.model;

import com.test.model.AopInterface;
import org.springframework.stereotype.Component;
import org.springframework.beans.factory.annotation.Autowired;

@Component
public class AopClass implements AopInterface {

    public void addCustomerAround(String name){
        System.out.println("addCustomerAround() is running, args : " + name);
    }
}

AOP:

package com.test.model;

import java.util.Arrays;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;



@Aspect
public class TestAdvice{


     @Around("execution(* com.test.model.AopInterface.addCustomerAround(..))")
       public void testAdvice(ProceedingJoinPoint joinPoint) throws Throwable {

        System.out.println("testAdvice() is running!");
        System.out.println("hijacked method : " + joinPoint.getSignature().getName());
        System.out.println("hijacked arguments : " + Arrays.toString(joinPoint.getArgs()));

        System.out.println("Around before is running!");
        joinPoint.proceed(); 
        System.out.println("Around after is running!");

        System.out.println("******");

       }
}

appcontext.xml:

<!-- Aspect -->
    <aop:aspectj-autoproxy />
    <bean id="AopClass" class="com.test.model.AopClass" />
    <bean id="TestAdvice" class="com.test.model.TestAdvice" />

POM:

<!-- AOP -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.6.11</version>
        </dependency>

        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.6.11</version>
        </dependency>

方法调用:

aoptest.addCustomerAround("dummy");

【问题讨论】:

  • 在进行方法调用之前,您从哪里获得对 AopClass 实例的引用?它是由 Spring 连接的吗?

标签: java maven aop aspectj


【解决方案1】:

我在这里没有看到任何错误。我刚刚尝试了您的代码,它运行良好。 我的测试课:

public class App {
    public static void main(String[] args) throws Exception {

        ApplicationContext appContext = new ClassPathXmlApplicationContext("appcontext.xml");

        //-------------------------
        AopClass aopClass = (AopClass) appContext.getBean("AopClass");
        aopClass.addCustomerAround("dummy");
    }
}

结果

您可以重试构建您的 maven 项目吗?!再试一次?!

【讨论】:

  • 您是否对 appcontext.xml 进行了任何更改?
  • 不,只需要重新构建就可以了。傻我:-/
【解决方案2】:

根据Aspect Oriented Programming with Spring,当类实现接口时,将使用JDK动态代理,如果没有实现接口,则将创建CGLIB代理。要强制使用 CGLIB 代理,您必须设置 proxy-target-class="true"。

appcontext.xml:

<!-- Aspect -->
<aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>
<bean id="AopClass" class="com.test.model.AopClass" />
<bean id="TestAdvice" class="com.test.model.TestAdvice" />

输出:

testAdvice() is running!
hijacked method : addCustomerAround
hijacked arguments : [dummy]
Around before is running!
addCustomerAround() is running, args : dummy
Around after is running!
******

【讨论】:

    猜你喜欢
    • 2019-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-30
    • 1970-01-01
    相关资源
    最近更新 更多