我是第一次使用AOP.
我写了下面的AOP代码,当我用它来拦截一个特定的方法时它工作正常.
有人可以指导我 – 如何设置它来拦截某个包中的所有方法(com.test.model)?
基本上如何设置appcontext.xml.
另外,在调用每个方法之前,我是否需要执行类似下面的操作?
AopClass aoptest = (AopClass) _applicationContext.getBean("AopClass");
aoptest.addCustomerAround("dummy");
有人可以帮忙吗?
如果需要更多解释,请告诉我.
以下是我的代码:
接口:
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!");
}
}
appcontext: