天天看點

Spring面向切面程式設計(AOP)三之AOP的使用及XML配置

  1. 引入必須的jar包
  2. 再配置檔案裡面,添加aop限制
<?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:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd">
</beans>
           
  1. 建立Target,需要被增強的類,含有多個連接配接點(方法)
//需要被增強的對象,含有多個連接配接點
public class GoodsDao {
    public void save(){
        System.out.println("執行儲存操作");
    }
    public void delete(){
        System.out.println("執行删除操作");
    }
}
           
  1. 建立一個切面類,裡面擁有通知,就是需要增強的方法
//切面類,裡面擁有通知,就是需要增強的方法
public class Myaspect {

    //通知aspect
    public void check(){
        System.out.println("權限校驗");
    }
}
           
  1. 将Target與切面類都交給Spring管理
<?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:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd">
        
    <bean id="goods" class="SpringPro2.Demo3.GoodsDao"></bean>
    <!--首先把切面類交給Spring管理-->
    <bean id="myaspect" class="SpringPro2.Demo3.Myaspect"></bean>
    
</beans>
           
  1. 配置AOP

    分為兩步,配置切入點和配置切面

<!--配置Aop-->
    <aop:config>
        <!--配置切入點,給哪個方法增強-->
        <aop:pointcut id="cheaksave"
                      expression="execution(* SpringPro2.Demo3.GoodsDao.save(..))"></aop:pointcut>
        <!--配置切面,增強什麼方法-->
        <aop:aspect ref="myaspect">
            <aop:before method="check" pointcut-ref="cheaksave"></aop:before>
        </aop:aspect>
    </aop:config>
           

配置切入點,id随便起的名字,用于後面切面中的引用。expression寫一個execution表達式,表明給哪個類的哪個方法增強,表達式前面的*表示傳回類型為任意類型,方法後面的(…)表明方法的參數也是任意類型。

配置切面,一個參數ref,引用之前交給Spring管理的切面類的id。裡面配置

<aop:before>

表明通知的類型,比如還有

<aop:after-returning>

等等。method表示調用切面類中的哪個方法去用于增強,注意這裡method後面的方法直接寫名稱,不用寫雙括号()。pointcut-ref表明是要給哪個切入點增強,就是上面配置的切入點id。

  1. 測試
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.Resource;

//注解的形式,加載工廠(配置檔案)
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class AOPTest {

    //引用類型,注解形式注入
    @Resource(name = "goods")
    private GoodsDao goodsDao;

    @Test
    public void Test(){
        this.goodsDao.save();
    }
}
           

運作結果

權限校驗
執行儲存操作

Process finished with exit code 0
           

已經在save()方法之前添加了一個通知(增強了一個方法)。

總結:

Spring中隻需要配置參數,就會自動的建立代理對象,實作動态代理,方法的增強,其中的原理就是使用JDK方法代理或者Cglib方法代理。

Spring面向切面程式設計(AOP)原理一之使用JDK實作動态代理

Spring面向切面程式設計(AOP)原理二之使用Cglib實作動态代理