Spring Aop配置文件使用

前言

本文介紹通過xml的方式來配置aop

1.編寫目標類

@Data
public class Apple {
    private String name;
    private int price;
    public void grow() {
        System.out.println("Apple grow.......");
    }
}

2.編寫通知

public class AppleAdvice {

    public void before(JoinPoint joinPoint) {
        System.out.println("advice before ...");
    }

    public void after(JoinPoint joinPoint) {
        System.out.println("advice after ...");
    }

    public void afterReturn(JoinPoint joinPoint) {
        System.out.println("advice after return ...");
    }

    public void afterThrow(JoinPoint joinPoint) {
        System.out.println("advice after throw ...");
    }

    public void around(ProceedingJoinPoint point) throws Throwable {
        System.out.println("around before_");
        point.proceed();
        System.out.println("around after_");
    }
}

3.配置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:context="http://www.springframework.org/schema/context" 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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
    <!--被切入對象-->
    <bean id="apple" class="com.cqliu.aop.Apple"/>
    <!--定義通知-->
    <bean id="appleAdvice" class="com.cqliu.aop.AppleAdvice"/>
    <aop:config>
        <!--定義切面-->
        <aop:aspect id="advisor" ref="appleAdvice">
            <!--定義切點-->
            <aop:pointcut id="pointcut" expression="execution(* com.cqliu.aop.Apple.*grow())"/>
            <!--定義通知-->
            <aop:before method="before" pointcut-ref="pointcut"/>
            <aop:after method="after" pointcut-ref="pointcut"/>
            <aop:after-returning method="afterReturn" pointcut-ref="pointcut"/>
            <aop:after-throwing method="afterThrow" pointcut-ref="pointcut"/>
            <aop:around method="around" pointcut-ref="pointcut"/>
        </aop:aspect>
    </aop:config>
</beans>

4.輸出

public class SpringAopApp {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("aop-beans.xml");
        Apple apple = (Apple) applicationContext.getBean("apple");
        apple.grow();
    }
}
advice before ...
around before_
Apple grow.......
around after_
advice after return ...
advice after ...

5.切點表達式說明

  1. public void com.cqliu.aop.Apple.grow() //一個完整的方法路徑

  2. void void com.cqliu.aop.Apple.grow()//省略默認的public

    • void com.cqliu.aop.Apple.grow() //返回類型不要求 用*取代
    • void com.cqliu.aop.Apple.grow().() //對象方法全設為目標方法 用取代
    • void com.cqliu.aop.Apple.grow().*(..) //方法的參數不作要求 用..表示
    • com.cqliu.aop.Apple.(..) //所有以Apple結尾類名 用*Apple表示
    • com.cqliu.aop..Apple.(..) //com.cqliu.aop包下的所有子包也包過在內加入.表示
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內容