什么是Aop?
它的作用和优势?
Aop的动态代理技术:
概念相关:
开发相关:
开发步骤
xml实现:
1、导入 AOP 相关坐标;
2、创建目标接口和目标类(内部有切点);
3、创建切面类(内部有增强方法);
4、将目标类和切面类的对象创建权交给 spring;
5、在 applicationContext.xml 中配置织入关系;
6、测试代码。
<!-- 目标对象 -->
<bean id="target" class="xml实现.Target.Target"></bean>
<!-- 切面对象 -->
<bean id="myAspect" class="xml实现.Aspect.myAspect"></bean>
<!-- 配置织入:告诉spring框架哪些方法需要进行哪些增强 -->
<aop:config>
<!--声明切面-->
<aop:aspect ref="myAspect">
<!--声明切点和增强-->
<aop:before method="before" pointcut="execution(public void xml实现.Target.Target.run())"/>
<aop:after-returning method="afterReturning" pointcut="execution(void xml实现.Target.Target.run()))"/>
<aop:around method="around" pointcut="execution(void xml实现.Target.Target.run()))"/>
<aop:after-throwing method="afterThrowing" pointcut="execution(* xml实现.Target.Target.run())"/>
<aop:after method="after" pointcut="execution(* xml实现.Target.Target.run())"/>
<!--抽取切点表达式-->
<aop:pointcut id="myPointcut" expression="execution(* xml实现.Target.Target.run())"></aop:pointcut>
<aop:before method="before" pointcut-ref="myPointcut"/>
</aop:aspect>
</aop:config>
注解实现:
1、导入 AOP 相关坐标;
2、创建目标接口和目标类(内部有切点);
3、创建切面类(内部有增强方法);
4、在切面类中使用注解配置织入关系;
5、在 applicationContext.xml 中开启组件扫描和Aop自动代理;
<!-- aop自动代理 -->
<aop:aspectj-autoproxy/>
6、测试代码。