Java 面向切面程式設計AOP

百聯達發表於2017-02-15
一:背景
Spring的AOP的存在目的是為了解耦。AOP可以讓一組類共享相同的行為。在OOP中只能透過繼承類和實現介面,來是程式碼的耦合度增強,且類繼承只能為單繼承,阻礙更多行為新增到一組類上,AOP彌補了OPP的不足。

二:概述  Spring支援AspectJ的註解方式切面程式設計

1.使用@Aspect 宣告一個切面。

2.使用@After,@Before,@Around 定義建言(advice),可直接將攔截規則(切點)作為引數。

3.其中@After,@Before,@Around引數的攔截規則為切點(PointCut) ,為了使切點複用,可使用@PointCut 專門定義攔截規則,然後在@After,@Before,@Around的引數中呼叫。

4.其中符合條件的每一個被攔截處為連線點(JoinPoint)

三:程式碼例項

1.pom.xml


點選(此處)摺疊或開啟

  1. <dependency>
  2.             <groupId>org.springframework</groupId>
  3.             <artifactId>spring-core</artifactId>
  4.         </dependency>
  5.         <dependency>
  6.             <groupId>org.springframework</groupId>
  7.             <artifactId>spring-beans</artifactId>
  8.         </dependency>

  9.         <dependency>
  10.             <groupId>org.springframework</groupId>
  11.             <artifactId>spring-context</artifactId>
  12.         </dependency>

  13.         <dependency>
  14.             <groupId>org.springframework</groupId>
  15.             <artifactId>spring-aop</artifactId>
  16.         </dependency>

  17.         <dependency>
  18.             <groupId>org.aspectj</groupId>
  19.             <artifactId>aspectjrt</artifactId>
  20.         </dependency>

  21.         <dependency>
  22.             <groupId>org.aspectj</groupId>
  23.             <artifactId>aspectjweaver</artifactId>
  24.         </dependency>
2.攔截規則的註解


點選(此處)摺疊或開啟

  1. @Target(ElementType.METHOD)
  2. @Retention(RetentionPolicy.RUNTIME)
  3. @Documented
  4. public @interface Action {
  5.     String name();
  6. }
2.註解的被攔截類


點選(此處)摺疊或開啟

  1. @Service
  2. public class DemoAnnotationService {

  3.     @Action(name = "註解式攔截的add操作")
  4.     public void add() {
  5.        System.out.println("======DemoAnnotationService方法add()=========");
  6.     }
  7. }
3.方法規則被攔截類


點選(此處)摺疊或開啟

  1. @Service
  2. public class DemoMethodService {
  3.     public String add() throws Exception{
  4.         System.out.println("======DemoMethodService方法add()=========");
  5.         int i=100/0;
  6.         return "SUCCESS";
  7.     }
  8. }

4.編寫切面


點選(此處)摺疊或開啟

  1. @Aspect
  2. @Component
  3. public class LogAspect {

  4.     @Pointcut("@annotation(com.gemdale.gmap.spring.boot.demo.Action)")
  5.     public void annotationPointCut() {
  6.     }

  7.     @After("annotationPointCut()")
  8.     public void after(JoinPoint joinPoint) {
  9.         MethodSignature signature = (MethodSignature) joinPoint.getSignature();
  10.         Method method = signature.getMethod();
  11.         Action action = method.getAnnotation(Action.class);

  12.         System.out.println("註解式攔截 " + action.name());
  13.     }

  14.     @Before("execution(* com.gemdale.gmap.spring.boot.demo.DemoMethodService.*(..))")
  15.     public void methodBefore(JoinPoint joinPoint) {
  16.         MethodSignature signature = (MethodSignature) joinPoint.getSignature();
  17.         Method method = signature.getMethod();
  18.         System.out.println("before方法規則式攔截 " + method.getName());
  19.     }

  20.     @After("execution(* com.gemdale.gmap.spring.boot.demo.DemoMethodService.*(..))")
  21.     public void methodAfter(JoinPoint joinPoint) {
  22.         MethodSignature signature = (MethodSignature) joinPoint.getSignature();
  23.         Method method = signature.getMethod();
  24.         System.out.println("after方法規則式攔截 " + method.getName());
  25.     }

  26.     @AfterReturning(pointcut = "execution(* com.gemdale.gmap.spring.boot.demo.DemoMethodService.*(..))", returning = "result")
  27.     public void methodAfterResult(JoinPoint joinPoint, Object result) {
  28.         MethodSignature signature = (MethodSignature) joinPoint.getSignature();
  29.         Method method = signature.getMethod();
  30.         System.out.println("after result方法規則式攔截 " + method.getName() + "result=" + result);
  31.     }

  32.     @AfterThrowing(pointcut = "execution(* com.gemdale.gmap.spring.boot.demo.DemoMethodService.*(..))", throwing = "e")
  33.     public void methodAfterException(JoinPoint joinPoint, Exception e) {
  34.         MethodSignature signature = (MethodSignature) joinPoint.getSignature();
  35.         Method method = signature.getMethod();
  36.         System.out.println("after exception方法規則式攔截 " + method.getName() + " e=" + e.getMessage());
  37.     }

  38. }
5.配置類


點選(此處)摺疊或開啟

  1. @Configuration
  2. @ComponentScan("com.gemdale")
  3. @EnableAspectJAutoProxy
  4. public class AppliactionConfig {

  5. }

6.執行類


點選(此處)摺疊或開啟

  1. public class Start {
  2.     public static void main(String[] args) throws Exception{

  3.         AnnotationConfigApplicationContext configApplicationContext = new AnnotationConfigApplicationContext(
  4.                 AppliactionConfig.class);
  5.         // UseFunctionService
  6.         // useFunctionService=configApplicationContext.getBean(UseFunctionService.class);
  7.         // System.out.println(useFunctionService.sayHello("Gengchong"));
  8.         DemoAnnotationService demoAnnotationService = configApplicationContext.getBean(DemoAnnotationService.class);
  9.         DemoMethodService demoMethodService = configApplicationContext.getBean(DemoMethodService.class);

  10.         demoAnnotationService.add();

  11.         demoMethodService.add();

  12.         configApplicationContext.close();
  13.     }
  14. }


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28624388/viewspace-2133594/,如需轉載,請註明出處,否則將追究法律責任。

相關文章