【Spring實戰】—— 13 AspectJ注解切面


前面了解了典型的AOP基於配置的使用方法,下面介紹下如何依賴於注解來實現AOP。

基於注解降低了配置文件的復雜程度,但是引入了程序間的耦合,其中的優劣待用戶自己判斷了。

需要注意的是,確定AspectJ與JDK之間的版本,否則會報錯,詳情請見

  首先看一下基於注解的切面類,這時的切面不僅僅是一個POJO類了,與AOP進行了緊密的耦合。但是配置過程和方式都與原來的方式差不多。

package com.spring.test.chap44;

import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class Audience {
    @Pointcut("execution(* com.spring.test.chap44.Instrumentalist.perform(..))")
    public void performance(){}
    
    @Before("performance()")
    public void takeSeats(){
        System.out.println("takeSeats()");
    }
    @Before("performance()")
    public void turnOffCellphones(){
        System.out.println("turnOffCellphones()");
    }
    @AfterReturning("performance()")
    public void applaud(){
        System.out.println("applaud()");
    }
    @AfterThrowing("performance()")
    public void demandRefund(){
        System.out.println("demandRefund()");
    }
}

  接下來是其他一些必不可少的類:

  切點接口類:

package com.spring.test.chap44;

public interface Performer {
    public void perform();
}

  切點實現類:

package com.spring.test.chap44;

import org.springframework.stereotype.Component;

@Component
public class Instrumentalist implements Performer{
    public void perform() {
        System.out.println("__________ perform ___________");
    }
}

  測試類:

package com.spring.test.chap44;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class test {
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml");
        
        Performer performer = (Performer)ctx.getBean("xingoo");
        performer.perform();
    }
}

  下面是重點的配置文件

  此時的配置文件注意要使spring知道哪一個是普通的bean,哪一個是通知。因此需要加上一個屬性,保證AOP自動的識別通知。

<aop:aspectj-autoproxy proxy-target-class="true"/>

  配置文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
                         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                         http://www.springframework.org/schema/context
                         http://www.springframework.org/schema/context/spring-context-3.0.xsd
                         http://www.springframework.org/schema/tx
                         http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
                         http://www.springframework.org/schema/aop 
                         http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
    <bean id="xingoo" class="com.spring.test.chap44.Instrumentalist"/>
    <bean id="audience" class="com.spring.test.chap44.Audience" />
    <aop:aspectj-autoproxy proxy-target-class="true"/>
</beans>

  執行結果如下:

turnOffCellphones()
takeSeats()
__________ perform ___________
applaud()

 

  如果需要使用around只需要在切面中添加如下的代碼就可以了:

    @Around("performance()")
    public void watchPerformance(ProceedingJoinPoint joinpoint){
        try{
            System.out.println("11111");
            
            long start = System.currentTimeMillis();
            
            joinpoint.proceed();
            
            long end = System.currentTimeMillis();
            
            System.out.println("time—— "+(end-start)+" millinseconds");
            System.out.println("22222");
        }catch(Throwable t){
            System.out.println("in watchPerformance Throwable()");
        }
    }

   對於參數的傳遞的通知,也與原先通過配置的差不多

  在切面中配置好切點的方法,注意帶上參數

    private String str;
    @Pointcut("execution(* com.spring.test.chap44.Instrumentalist.perform(String)) && args(str)")
    public void performance(String str){}
    
    @Before("performance(str)")
    public void takeSeats(String str){
        System.out.println("takeSeats()"+str);
    }

  其他的基本都不用動了,只要把切點的方法,修改成帶有參數的就可以了

public class Instrumentalist implements Performer{
    public void perform(String str) {
        System.out.println("__________ perform ___________" + str);
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM