aop的底层是动态代理 作用是在不改变原有代码的基础上,添加新的功能,切面编程。不熟悉动态代理的同学可以查看这篇文章
原理
将要添加的功能通过Spring提供的方法(动态代理),织入在所需方法之前或之后 共有三种方式实现 来看看吧
1.使用Spring的api接口实现aop
目标类
public interface Rent {
void rent();
}
//真实类
public class RentImpl implements Rent{
@Override
public void rent() {
System.out.println("租了一套房");
}
}
将要织入的类实现接口
将要织入的类 实现AfterReturningAdvice或MethodBeforeAdvice接口,重写其中方法。该方法就可以织入到目标类的前方或后方
public class AfterLog implements AfterReturningAdvice {
@Override
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println("方法名"+method.getName()+"返回内容"+returnValue);
}
}
public class BeforLog implements MethodBeforeAdvice {
@Override
public void before(Method method, Object[] objects, Object o) throws Throwable {
System.out.println(o.getClass().getName()+"正在使用方法"+method.getName()+"参数为"+ Arrays.toString(objects));
}
}
在xml中配置
注入目标类和要植入的类
<bean id="rentImpl" class="RentImpl"/>
<bean id="befor" class="BeforLog"/>
<bean id="after" class="AfterLog"/>
切入
<aop:config>
<!--execution(返回值 类名 方法名 参数)-->
<!-- 切入点 expression 表达式 execution 执行的位置-->
<aop:pointcut id="pointcut" expression="execution(* RentImpl.*(..))"></aop:pointcut>
<!--执行环绕增加-->
<aop:advisor advice-ref="befor" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="after" pointcut-ref="pointcut"/>
</aop:config>
2.使用自定义类实现aop 切面定义
定义要织入的类
public class ABLog {
public void befor(){
System.out.println("正在启动日志 开始记录");
}
public void after(){
System.out.println("关闭日志 正在保存记录");
}
}
在xml中配置
注入目标类和要植入的类
<bean id="rentImpl" class="RentImpl"/>
<bean id="ablog" class="ABLog"/>
切面织入
<aop:config>
<!--定义切面 ref要使用的类 -->
<aop:aspect ref="ablog">
<!--切入点-->
<aop:pointcut id="pointcut" expression="execution(* RentImpl.*(..))"/>
<!--通知-->
<aop:after method="after" pointcut-ref="pointcut"/>
<aop:before method="befor" pointcut-ref="pointcut"/>
</aop:aspect>
</aop:config>
2.注解实现aop
定义类
//只是一个切面
@Aspect
public class AnnotationLog {
@Before("execution(* RentImpl.*(..))")
public void befor(){
System.out.println("日志开始");
}
@After("execution(* RentImpl.*(..))")
public void after(){
System.out.println("日志结束");
}
/**
*
* @param pj
* @throws Throwable
* 结果是 环绕前 日志开始 日至结束 环绕后
*/
//在环绕增强中 我们可以给出一个参数来代表 我们要处理切入的点
@Around("execution(* RentImpl.*(..))")
public void around(ProceedingJoinPoint pj) throws Throwable {
System.out.println("环绕前");
//执行切入点的方法
Object proceed = pj.proceed();
System.out.println("环绕后");
}
}
开启注解
<bean id="annotationLog" class="AnnotationLog"/>
<!--aop方式三 使用注解定义-->
<aop:aspectj-autoproxy/>