? 1.AOP的實現(xiàn):1.1配置文件的形式實現(xiàn)的。
? ? 實現(xiàn)核心業(yè)務和輔助功能的交織:
? 目的是為了將核心業(yè)務與輔助業(yè)務分開
? 步驟: 1.定義好輔助功能的切面類腥刹,以及核心功能業(yè)務類。
? ? ? ? ? 2.通過ApplicationContext.xml配置切面與業(yè)務方法的交織
? (spring支持方法的切入)
? 3. 配置切入點
? ? ? ? ? ? ? ? ? ? 需要一個代理對象實現(xiàn)輔助功能的調用装哆,
AOP是通過動態(tài)代理的形式實現(xiàn)切面的 ?
? ? <aop:config proxy-target-class="true">
? ? <aop:aspect id="" ref="">
? ? ? ? ? <aop:pointcut id="display" expression="execution(* service.StudentService.display(..))"/>
? ? ? ? 配置具體的通知塌鸯,輔助功能的方法
? <aop:before method="before" pointcut-ref="display"/>
? <aop:after method="after" pointcut-ref="display"/>
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <aop:after-returning method="returnning" pointcut-ref="display" returning="o"/>
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <aop:after-throwing method="exception" pointcut-ref="display"/>
? ? </aop:aspect>
</aop:conig>
1.2注解的形式實現(xiàn)AOP:
? ? ? ? ? 1.定義好輔助功能的切面類瞎嬉,以及核心功能業(yè)務類。?
? ? ? ? ? 2.開啟注解扰肌,并使用aspectj的動態(tài)代理的形式實現(xiàn) ?
? ? ? ? <context:component-scan base-package="AOP"></context:component-scan>
<!--指明使用aspectj-autoproxy動態(tài)代理的形式實現(xiàn)-->
? ? ? ? <aop:aspectj-autoproxy proxy-target-class="true"/>
3.對應的切面類寫明注解:
? @Aspect
@Component
public class WatchAOP {
? ? @Pointcut("execution(* service.StudentService.display(..))")
? ? public? void? myProject(){}
? @Before("myProject()")
? ? public? void? before(){
? ? ? ? System.out.println("核心業(yè)務方法調用之前");
? ? ? ? //日志功能抛寝,性能統(tǒng)計。曙旭。盗舰。。
? ? };
? @After("myProject()")
? ? public? void? after(){
? ? ? ? System.out.println("核心業(yè)務方法調用之后");
? ? };
? @AfterThrowing("myProject()")
? ? public? void? exception(){
? ? ? ? System.out.println("核心業(yè)務方法出現(xiàn)異常");
? ? };
? @AfterReturning(value = "myProject()",returning = "o")
? public? void? returnning(JoinPoint point,Object o){
? ? ? ? System.out.println("方法已經執(zhí)行完成,核心方法的類名是:"+point.getTarget().getClass().getName()+"核心業(yè)務的方法是"+point.getSignature().getName()+"方法的返回值是:"+o);
? ? };
? ? };