基於XML的AOP配置--學習筆記

基於一個簡單的案例

在業務層方法調用之前先調用日誌類的方法


先引入依賴
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>mulinhu</groupId>
    <artifactId>day03_eesy_03springAOP</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.4</version>
        </dependency>
    </dependencies>
</project>

package mu.lin.hu.service;

public interface IAcountService {
    void saveAccount();
    void updateAccount(int i);
    int deleteAccount();
}

package mu.lin.hu.service.impl;

import mu.lin.hu.service.IAcountService;

public class AccountServiceImpl implements IAcountService {
    public void saveAccount() {
        System.out.println("執行了保存");
    }

    public void updateAccount(int i) {
        System.out.println("執行了更新 "+i);
    }

    public int deleteAccount() {
        System.out.println("執行了刪除");
        return 0;
    }
}

package mu.lin.hu.util;

public class Logger {
    public void printLog(){
        System.out.println("Logger類中的printLog執行了!");
    }
}

bean.xml
<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd">

    <bean id="accountService" class="mu.lin.hu.service.impl.AccountServiceImpl"></bean>
    <bean id="logger" class="mu.lin.hu.util.Logger"></bean>
    <aop:config>

        <aop:aspect id="logAdvice" ref="logger">
            <aop:before method="printLog" pointcut="execution(public void mu.lin.hu.service.impl.AccountServiceImpl.saveAccount())"></aop:before>
        </aop:aspect>
    </aop:config>
</beans>
spring中基於xml的aop配置步驟
    1.把通知bean交給spring來管理
    2.使用aop:config標籤表明AOP開始的位置
    3.使用aop:aspect 標籤表明配置切面
            id屬性:是給切面提供一個唯一標識
            ref屬性:是指定通知類bean的id
    4.在aop:aspect標籤的內部使用對應標籤來配置通知的類型
            aop:before:表示配置前置通知
            pointcut屬性:用於指定切入點表達式,該表達式的含義是指對業務層中的哪些方法增強

         切入點表達式
            關鍵字:execution(表達式)
            表達式:
                訪問修飾符 返回值 包名.包名...包名.類名(參數列表)
             標準的表達式寫法:
                public void mu.lin.hu.service.impl.AccountServiceImpl.saveAccount()
              返回值可以使用通配符,表示任意返回值
                * mu.lin.hu.service.impl.AccountServiceImpl.saveAccount()
              包名可以使用通配符,表示任意包。但是有幾級包,就需要寫幾個*.
              * *.*.*.*.*.AccountServiceImpl.saveAccount()
              包名可以使用..表示當前包及其子包
              * *..AccountServiceImpl.saveAccount()
              類名和方法名都可以使用*來實現通配
              * *..*.*()
              參數列表
                    可以直接寫數據類型
                        基本數據類型直接寫名稱 int
                        引用類型寫包名.類名的方式 java.lang.String
                     可以使用通配符表示任意類型,但必須有參數
                     可以使用..表示有無參數均可,有參數可以是任意類型
              全通配寫法
                * *..*.*(..)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章