在面向切面编程时,我们会使用< aop:aspect>;
在进行事务管理时,我们会使用< aop:advisor>。
< aop:aspect>定义切面时,只需要定义一般的bean就行,
而定义< aop:advisor>中引用的通知时,通知必须实现Advice接口。
切面代码 :
<bean id="testAdvice" class="com.myspring.app.aop.MyAdvice"/>
import java.lang.reflect.Method;
import org.aspectj.lang.JoinPoint;
import org.springframework.aop.MethodBeforeAdvice;
/**
* 方法前置通知
*/
@Component("myAdvice")//如果是自动装配,在定义切面的时候直接写在ref属性里就可以了
public class MyAdvice implements MethodBeforeAdvice{
//如果使用aop:advisor配置,那么切面逻辑必须要实现advice接口才行!否则会失败!
@Override
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println("前置通知");
}
//如果是<aop:aspect>配置,编写一般的方法就可以了,然后在切面配置中指定具体的方法名称!
public void doBefore(JoinPoint point) throws Throwable {
}
}
摘取自:https://blog.csdn.net/u011710466/article/details/52888277