1. 程式人生 > >註釋方式切面程式設計(Spring AOP技術)

註釋方式切面程式設計(Spring AOP技術)

導論:Aspect Oriented Programming(AOP)即面向切面程式設計。AOP主要實現的目的是針對業務處理過程中的切面進行提取,它所面對的是處理過程中的某個步驟或階段,以獲得邏輯過程中各部分之間低耦合性的隔離效果。它使得程式碼更加靈活,並且提供了程式碼的重用性。
                                                                                                                      

可以寫普通JAVA類+配置檔案的方式進行切面程式設計,也可以用註釋(annotation)方式進行切面程式設計。本例中採用後者。
流程:


1、寫特殊的JAVA類,它通過添加註釋(annotation)方式,指定切面、切入點和通知----->
2、在配置檔案中加入一行,以便開啟aspectJ註解,使其自動生成代理------>
3、寫目標類的目標方法----->
4、測試類中呼叫目標類的目標方法。

1、寫特殊的JAVA類,它通過添加註釋(annotation)方式
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Service;
@Aspect
//指定這是一個其切面
public class myAnnotationAspect {

//指定切入點
    @Pointcut("execution(* myTarena.*.*(..))")
    public void myPointCut(){}

  //指定通知以及通知的範圍
    @Before("myPointCut()")
    public void beforeMethod(){
        System.out.println("前置通知。。。");
    }
   
    @Around("myPointCut()")
    public Object aroundMethod(ProceedingJoinPoint pjp) throws Throwable{
        System.out.println("環繞通知上。。。");
        Object obj=pjp.proceed();
        System.out.println("環繞通知下。。。");
        return obj;
    }
   
    @After("myPointCut()")
    public void afterMethod(){
        System.out.println("後置通知。。。");
      
    }   
}

2、在配置檔案中加入一行,以便開啟aspectJ註解,使其自動生成代理

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.0.xsd">
           <bean id="myService" class="myTarena.MyService"></bean>
           <bean id="myAnnotationAspect" class="myTarena.myAnnotationAspect"></bean>
          <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

</beans>

3、寫目標類的目標方法
public class MyService implements IMyService {
    public void regist(){
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("----執行註冊操作----");
    }
}

4、測試類中呼叫目標類的目標方法。
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AspectTest {
    public static void main(String[] args) {
        ApplicationContext ctx=new ClassPathXmlApplicationContext("aop-aspect.xml");
        IMyService myService=(IMyService) ctx.getBean("myService");
        myService.regist();
    }
}