Java 包中所有方法的AOP
我第一次使用AOP。 我已经编写了下面的AOP代码,当我使用它截取一个特定的方法时,它可以很好地工作 有人能指导我吗?我如何设置它来拦截某个包(com.test.model)中的所有方法 基本上是如何设置appcontext.xml 另外,在调用每个方法之前,是否需要执行如下操作来调用Java 包中所有方法的AOP,java,spring,aop,Java,Spring,Aop,我第一次使用AOP。 我已经编写了下面的AOP代码,当我使用它截取一个特定的方法时,它可以很好地工作 有人能指导我吗?我如何设置它来拦截某个包(com.test.model)中的所有方法 基本上是如何设置appcontext.xml 另外,在调用每个方法之前,是否需要执行如下操作来调用 AopClass aoptest = (AopClass) _applicationContext.getBean("AopClass"); aoptest.addCustomerAround("dummy");
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);
}
}
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!");
}
}
<!-- Aspect -->
<aop:aspectj-autoproxy />
<bean id="AopClass" class="com.test.model.AopClass" />
<bean id="TestAdvice" class="com.test.model.TestAdvice" />
类别:
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);
}
}
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!");
}
}
<!-- Aspect -->
<aop:aspectj-autoproxy />
<bean id="AopClass" class="com.test.model.AopClass" />
<bean id="TestAdvice" class="com.test.model.TestAdvice" />
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);
}
}
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!");
}
}
<!-- Aspect -->
<aop:aspectj-autoproxy />
<bean id="AopClass" class="com.test.model.AopClass" />
<bean id="TestAdvice" class="com.test.model.TestAdvice" />
appcontext:
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);
}
}
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!");
}
}
<!-- Aspect -->
<aop:aspectj-autoproxy />
<bean id="AopClass" class="com.test.model.AopClass" />
<bean id="TestAdvice" class="com.test.model.TestAdvice" />
只要把:
@Around("execution(* com.test.model..*.*(..))")
执行表达式的格式为:
execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern) throws-pattern?)
其中只需要ret-type模式
,name模式
和param模式
,因此至少我们需要一个表达式,如:
execution(ret-type-pattern name-pattern(param-pattern))
与返回类型匹配,ret-type模式
用于任何*
与方法名称匹配,您可以使用名称模式
作为通配符,并使用*
指示子包。
匹配方法参数,参数模式
匹配任意数量的参数(..)