[英]AOP for all methods in a package
I am using AOP for the first time. 我是第一次使用AOP。 I have written the below AOP code which works fine when i use it to intercept a particular method.
我写了下面的AOP代码,当我用它来拦截一个特定的方法时它工作正常。
Can somebody guide me - how I can set it up to intercept all methods in a certain package (com.test.model)? 有人可以指导我 - 如何设置它来拦截某个包中的所有方法(com.test.model)?
Basically how to setup appcontext.xml. 基本上如何设置appcontext.xml。
Also, do i need to do something like below to call before calling each method? 另外,在调用每个方法之前,我是否需要执行类似下面的操作?
AopClass aoptest = (AopClass) _applicationContext.getBean("AopClass");
aoptest.addCustomerAround("dummy");
Can somebody help? 有人可以帮忙吗?
Please let me if some more explanation is needed. 如果需要更多解释,请告诉我。
Below is my code: 以下是我的代码:
Interface: 接口:
package com.test.model;
import org.springframework.beans.factory.annotation.Autowired;
public interface AopInterface {
@Autowired
void addCustomerAround(String name);
}
Class: 类:
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: 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: appcontext:
<!-- Aspect -->
<aop:aspectj-autoproxy />
<bean id="AopClass" class="com.test.model.AopClass" />
<bean id="TestAdvice" class="com.test.model.TestAdvice" />
Just put: 刚刚放:
@Around("execution(* com.test.model..*.*(..))")
The format of an execution expression is: 执行表达式的格式为:
execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern) throws-pattern?)
where only ret-type-pattern
, name-pattern
and param-pattern
are required, so at least we need an expression like: 其中只需要
ret-type-pattern
, name-pattern
和param-pattern
,所以至少我们需要一个像这样的表达式:
execution(ret-type-pattern name-pattern(param-pattern))
ret-type-pattern
matches the return type, *
for any ret-type-pattern
匹配返回类型, *
为any name-pattern
matches the method name, you can use *
as wildcard and ..
to indicate sub-package name-pattern
与方法名称匹配,您可以使用*
作为通配符和..
来表示子包 param-pattern
matches the method parameters, (..)
for any number of parameters param-pattern
匹配方法参数, (..)
用于任意数量的参数 You can find more information here: 10. Aspect Oriented Programming with Spring , there are some useful examples . 您可以在此处找到更多信息: 10。使用Spring进行面向方面编程 ,有一些有用的示例 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.