最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring中基于XML的面向切面編程(AOP)詳解

 更新時(shí)間:2024年04月17日 09:15:26   作者:陳橘又青  
這篇文章主要詳細(xì)介紹了Spring中基于XML的面向切面編程(AOP),文中通過代碼示例給大家講解的非常詳細(xì),對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下

一、基于XML的AOP

1.1、打印日志案例

1.1.1、beans.xml中添加aop的約束

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

1.1.2、定義Bean

package cn.bdqn.domain;
public class User {

}
package cn.bdqn.service;
public interface UserService {

    // 保存用戶
    public void save(User user);

    // 根據(jù)id查詢用戶
    public User queryById(Integer id);

    // 查詢?nèi)坑脩?
    public List<User> queryAll();
}
package cn.bdqn.service;
public class UserServiceImpl implements UserService{

    // 保存用戶
    public void save(User user){

    }

    // 根據(jù)id查詢用戶
    public User queryById(Integer id){
        return new User();
    }

    // 查詢?nèi)坑脩?
    public List<User> queryAll(){
        return new ArrayList<User>();
    }
}

1.2、定義記錄日志的類【切面】

package cn.bdqn.advice;

// 定義記錄日志的類,這個(gè)類就封裝了我們所有的公共的代碼
public class Logger {

    //  該方法的作用是在切入點(diǎn)方法執(zhí)行之前執(zhí)行
    public void beforePrintLog(){
        System.out.println("開始打印日志啦");
    }
}

1.3、導(dǎo)入AOP的依賴

<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.9.4</version>
</dependency>

1.4、主配置文件中配置AOP

<beans>
  	<!--  1、注冊UserServiceImpl這個(gè)Bean  -->
    <bean id="userService" class="cn.bdqn.service.UserServiceImpl"/>

    <!--  2、以下操作都是Spring基于XML的AOP配置步驟
       2.1 把通知/增強(qiáng)Bean也需要注冊到Spring容器中
       2.2 使用<aop:config/>標(biāo)簽來去聲明開始AOP的配置了
       2.3 使用<aop:aspect/>標(biāo)簽來去表示開始配置切面了
        可以想一下:既然要配置切面,那切面就是切入點(diǎn)和通知的結(jié)合,所以肯定需要配置切入點(diǎn)和通知這兩部分
              id屬性:是給切面提供一個(gè)唯一標(biāo)識
              ref屬性:是指定通知類bean的Id。
       2.4 在<aop:aspect/>標(biāo)簽的內(nèi)部使用對應(yīng)標(biāo)簽來配置通知的類型
              前置通知/后置通知/異常通知/最終通知
              需求:beforePrintLog方法在切入點(diǎn)方法執(zhí)行之前之前:所以是前置通知
              前置通知:<aop:before/>
                  method屬性:用于指定Logger類中哪個(gè)方法是前置通知
                  pointcut屬性:用于指定切入點(diǎn)表達(dá)式,該表達(dá)式的含義指的是對業(yè)務(wù)層中哪些方法增強(qiáng)

          3、切入點(diǎn)表達(dá)式的寫法:
                關(guān)鍵字:execution(表達(dá)式)
                表達(dá)式:
                    訪問修飾符  方法返回值  包名1.包名2...類名.方法名(參數(shù)列表)
                需求:
                    我現(xiàn)在就想對UserServiceImpl類中的queryAll方法進(jìn)行攔截
                    public java.util.List cn.bdqn.service.UserServiceImpl.queryAll()
    -->

    <!--  2.1 把通知/增強(qiáng)Bean也需要注冊到Spring容器中  -->
    <bean id="logger" class="cn.bdqn.advice.Logger"/>
    <!--  2.2 使用此標(biāo)簽來去聲明開始AOP的配置了-->
    <aop:config>
        <!--配置切面 -->
        <aop:aspect id="loggerAdvice" ref="logger">
            <!-- 配置通知的類型,并且建立增強(qiáng)方法和切入點(diǎn)方法的關(guān)聯(lián)-->
            <aop:before method="beforePrintLog" 
                        pointcut="execution(public java.util.List cn.bdqn.service.UserServiceImpl.queryAll())"/>
        </aop:aspect>
    </aop:config>
</beans>

1.5、測試

@Test
public void testUserServiceImpl() throws Exception{

   	ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
    UserService userService = (UserService) ac.getBean("userService");

    userService.queryAll();
}

1.6、切入點(diǎn)表達(dá)式

問題:我們上面的案例經(jīng)過測試發(fā)現(xiàn)確實(shí)在調(diào)用業(yè)務(wù)方法之前增加了日志功能,但是問題是僅僅能針對某一個(gè)業(yè)務(wù)方法進(jìn)行增強(qiáng),而我們的業(yè)務(wù)方法又有可能有很多,所以顯然一個(gè)一個(gè)的去配置很麻煩,如何更加靈活的去配置呢?這個(gè)就需要使用到切入點(diǎn)表達(dá)式

? 語法:execution(表達(dá)式)

訪問修飾符  方法返回值  包名1.包名2...類名.方法名(參數(shù)列表)

1.6.1、訪問修飾符可以省略

// 完整寫法
public java.util.List cn.bdqn.service.UserServiceImpl.queryAll())

// 標(biāo)準(zhǔn)寫法
java.util.List cn.bdqn.service.UserServiceImpl.queryAll())

1.6.2、返回值可以使用通配符,表示任意返回值

* cn.bdqn.service.UserServiceImpl.queryAll())

1.6.3、包名可以使用通配符表示任意包。有幾級包,就幾個(gè)*

* *.*.*.UserServiceImpl.queryAll())

但是對于包來說,連續(xù)的寫3個(gè)*,顯然也是麻煩的,那么可以使用“…”表示當(dāng)前包及其子包。

// 表示的是任意包下的只要有UserServiceImpl類都會對queryAll方法進(jìn)行增強(qiáng)
* *..UserServiceImpl.queryAll())

1.6.4、類名也可以用*

* *..*.queryAll()

1.6.5、方法也可以用*

* *..*.*()

1.6.6、參數(shù)列表

寫法1、可以直接寫數(shù)據(jù)類型:
             基本類型直接寫名稱           
                  int、double
             引用類型寫包名.類名的方式   
                  java.lang.String、java.util.List
寫法2、可以使用通配符表示任意類型
             前提是必須要有參數(shù)。

寫法3、使用..
             可以使用..表示有無參數(shù)均可,如果有參數(shù)則表示的可以是任意類型    

1.6.7、全通配符寫法

 * *..*.*(..)

1.6.8、使用最多的寫法

實(shí)際中的寫法:切到業(yè)務(wù)層實(shí)現(xiàn)類下的所有方法。即:

* com.bdqn.service.impl.*.*(..)

1.7、通知類型的使用

1.7.1、在日志類中新增通知方法

// 定義記錄日志的類,這個(gè)類就封裝了我們所有的公共的代碼
public class Logger {

    //  該方法的作用是在切入點(diǎn)方法執(zhí)行之前執(zhí)行
    public void beforePrintLog(){
        System.out.println("前置通知(beforePrintLog):開始打印日志啦");
    }

    //  該方法的作用是在切入點(diǎn)方法執(zhí)行之后執(zhí)行
    public void afterReturningPrintLog(){
        System.out.println("后置通知(afterReturningPrintLog):業(yè)務(wù)方法執(zhí)行完了,日志打印");
    }

    //  該方法的作用是在切入點(diǎn)方法執(zhí)行出錯(cuò)后執(zhí)行
    public void afterThrowingPrintLog(){
        System.out.println("異常通知(afterThrowingPrintLog):業(yè)務(wù)方法出現(xiàn)異常了,日志打印");
    }

    //  該方法的作用是在切入點(diǎn)方法執(zhí)行之后不管有沒有錯(cuò)誤,都最終要執(zhí)行
    public void afterPrintLog(){
        System.out.println("最終通知(afterPrintLog):業(yè)務(wù)方法不管有沒有異常了,日志打印");
    }
}

1.7.2、配置AOP

<beans>
	<!--  2.1 把通知/增強(qiáng)Bean也需要注冊到Spring容器中  -->
    <bean id="logger" class="cn.bdqn.advice.Logger"/>
    <!--  2.2 使用此標(biāo)簽來去聲明開始AOP的配置了-->
    <aop:config>
        <!--配置切面 -->
        <aop:aspect id="loggerAdvice" ref="logger">
          
            <!-- 配置前置通知:在切入點(diǎn)方法執(zhí)行之前執(zhí)行-->
            <aop:before method="beforePrintLog" 
                        pointcut="execution(* cn.bdqn.service.UserServiceImpl.queryAll())"/>
          
            <!-- 后置通知:在切入點(diǎn)方法正常執(zhí)行之后值。它和異常通知永遠(yuǎn)只能執(zhí)行一個(gè)-->
            <aop:after-returning method="afterReturningPrintLog"  pointcut="execution(* cn.bdqn.service.UserServiceImpl.queryAll())"/>
          
            <!--配置異常通知:在切入點(diǎn)方法執(zhí)行產(chǎn)生異常之后執(zhí)行。它和后置通知永遠(yuǎn)只能執(zhí)行一個(gè)-->
            <aop:after-throwing method="afterThrowingPrintLog"  
                                pointcut="execution(* cn.bdqn.service.UserServiceImpl.queryAll())"/>
          
            <!--配置最終通知:無論切入點(diǎn)方法是否正常執(zhí)行它都會在其后面執(zhí)行-->
            <aop:after method="afterPrintLog"
                       pointcut="execution(* cn.bdqn.service.UserServiceImpl.queryAll())"/>
            
        </aop:aspect>
    </aop:config>
</beans>

1.7.3、測試

@Test
public void testUserServiceImpl() throws Exception{

        ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
        UserService userService = (UserService) ac.getBean("userService");

        userService.queryAll();
}
/***
	前置通知(beforePrintLog):開始打印日志啦
    查詢?nèi)坑脩魣?zhí)行啦
    后置通知(afterReturningPrintLog):業(yè)務(wù)方法執(zhí)行完了,日志打印
    最終通知(afterPrintLog):業(yè)務(wù)方法不管有沒有異常了,日志打印
**/

1.8、切入點(diǎn)表達(dá)式改進(jìn)

通過11.7可以發(fā)現(xiàn),我們在配置文件中配置了四種通知類型,其中的pointcut配置的是切入點(diǎn)表達(dá)式,發(fā)現(xiàn)是一模一樣的,那么有沒有一種改進(jìn)寫法呢?可以將表達(dá)式抽取出來,將來可以引用。

1.8.1、方式一

<beans>
	<!--  1、注冊UserServiceImpl這個(gè)Bean  -->
    <bean id="userService" class="cn.bdqn.service.UserServiceImpl"/>

    <!--  2、以下操作都是Spring基于XML的AOP配置步驟-->

    <!--  2.1 把通知/增強(qiáng)Bean也需要注冊到Spring容器中  -->
    <bean id="logger" class="cn.bdqn.advice.Logger"/>
    <!--  2.2 使用此標(biāo)簽來去聲明開始AOP的配置了-->
    <aop:config>
        <!--配置切面 -->
        <aop:aspect id="loggerAdvice" ref="logger">

            <!--
                配置切入點(diǎn)表達(dá)式
                    id屬性用于指定切入點(diǎn)表達(dá)式的唯一標(biāo)識。
                    expression屬性用于指定表達(dá)式內(nèi)容
                此標(biāo)簽寫在aop:aspect標(biāo)簽內(nèi)部只能當(dāng)前切面使用。
           -->
            <aop:pointcut id="loggerPt" 
                          expression="execution(* 																					cn.bdqn.service.UserServiceImpl.queryAll())"/>
            
            <!-- 配置前置通知:在切入點(diǎn)方法執(zhí)行之前執(zhí)行-->
            <aop:before method="beforePrintLog" pointcut-ref="loggerPt"/>
            <!-- 后置通知:在切入點(diǎn)方法正常執(zhí)行之后值。它和異常通知永遠(yuǎn)只能執(zhí)行一個(gè)-->
            <aop:after-returning method="afterReturningPrintLog" pointcut-ref="loggerPt"/>
            <!--配置異常通知:在切入點(diǎn)方法執(zhí)行產(chǎn)生異常之后執(zhí)行。它和后置通知永遠(yuǎn)只能執(zhí)行一個(gè)-->
            <aop:after-throwing method="afterThrowingPrintLog" pointcut-ref="loggerPt"/>
            <!--配置最終通知:無論切入點(diǎn)方法是否正常執(zhí)行它都會在其后面執(zhí)行-->
            <aop:after method="afterPrintLog" pointcut-ref="loggerPt"/>
                
        </aop:aspect>
    </aop:config>
</beans>

1.8.2、方式二

對于方式一,我們將aop:pointcut標(biāo)簽寫在了aop:aspect里面,這樣的話這切入點(diǎn)表達(dá)式只能被當(dāng)前的切面使用,而如果其他切面想使用就使用不到了,所以我們可以把這個(gè)切入點(diǎn)表示再定義到外面。

<beans>
	<bean id="userService" class="cn.bdqn.service.UserServiceImpl"/>

    <!--  2、以下操作都是Spring基于XML的AOP配置步驟-->

    <!--  2.1 把通知/增強(qiáng)Bean也需要注冊到Spring容器中  -->
    <bean id="logger" class="cn.bdqn.advice.Logger"/>
    <!--  2.2 使用此標(biāo)簽來去聲明開始AOP的配置了-->
    <aop:config>

        <!--
                配置切入點(diǎn)表達(dá)式
                    id屬性用于指定切入點(diǎn)表達(dá)式的唯一標(biāo)識。
                    expression屬性用于指定表達(dá)式內(nèi)容
                此標(biāo)簽寫在aop:aspect標(biāo)簽外面,那么所有的切面都可以使用。
          -->
        <aop:pointcut id="loggerPt" 
                      expression="execution(* cn.bdqn.service.UserServiceImpl.queryAll())"/>

        <!--配置切面 -->
        <aop:aspect id="loggerAdvice" ref="logger">

            <!-- 配置前置通知:在切入點(diǎn)方法執(zhí)行之前執(zhí)行-->
            <aop:before method="beforePrintLog" pointcut-ref="loggerPt"/>
            <!-- 后置通知:在切入點(diǎn)方法正常執(zhí)行之后值。它和異常通知永遠(yuǎn)只能執(zhí)行一個(gè)-->
            <aop:after-returning method="afterReturningPrintLog" pointcut-ref="loggerPt"/>
            <!--配置異常通知:在切入點(diǎn)方法執(zhí)行產(chǎn)生異常之后執(zhí)行。它和后置通知永遠(yuǎn)只能執(zhí)行一個(gè)-->
            <aop:after-throwing method="afterThrowingPrintLog" pointcut-ref="loggerPt"/>
            <!--配置最終通知:無論切入點(diǎn)方法是否正常執(zhí)行它都會在其后面執(zhí)行-->
            <aop:after method="afterPrintLog" pointcut-ref="loggerPt"/>
                
        </aop:aspect>
    </aop:config>
</beans>

1.9、環(huán)繞通知

1.9.1、在日志記錄類中新增環(huán)繞通知

public class Logger {
    // 環(huán)繞通知
    public void aroundPrintLog(){
        System.out.println("環(huán)繞通知....aroundPrintLog.....");
    }
}

1.9.2、AOP配置環(huán)繞通知

<beans>
   <!--  1、注冊UserServiceImpl這個(gè)Bean  -->
    <bean id="userService" class="cn.bdqn.service.UserServiceImpl"/>

    <!--  2、以下操作都是Spring基于XML的AOP配置步驟-->
    <!--  2.1 把通知/增強(qiáng)Bean也需要注冊到Spring容器中  -->
    <bean id="logger" class="cn.bdqn.advice.Logger"/>
    <!--  2.2 使用此標(biāo)簽來去聲明開始AOP的配置了-->
    <aop:config>

        <!--    配置切入點(diǎn)表達(dá)式    -->
        <aop:pointcut id="loggerPt" 
                      expression="execution(* cn.bdqn.service.UserServiceImpl.queryAll())"/>

        <!--配置切面 -->
        <aop:aspect id="loggerAdvice" ref="logger">

            <!-- 環(huán)繞通知-->
            <aop:around method="aroundPrintLog" pointcut-ref="loggerPt"/>
                
        </aop:aspect>
    </aop:config>
</beans>

1.9.3、測試1

@Test
public void testUserServiceImpl() throws Exception{

        ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
        UserService userService = (UserService) ac.getBean("userService");

        userService.queryAll();
}
/**
	環(huán)繞通知....aroundPrintLog.....
	發(fā)現(xiàn):僅僅打印了環(huán)繞通知的代碼。當(dāng)我們配置了環(huán)繞通知之后,切入點(diǎn)方法沒有執(zhí)行,而通知方法執(zhí)行了
*/

1.9.4、解決

Spring框架為我們提供了一個(gè)接口:ProceedingJoinPoint。該接口有一個(gè)方法proceed(),此方法就相當(dāng)于明確調(diào)用切入點(diǎn)方法。該接口可以作為環(huán)繞通知的方法參數(shù),在程序執(zhí)行時(shí),spring框架會為我們提供該接口的實(shí)現(xiàn)類供我們使用。

public class Logger {
    // 環(huán)繞通知
    public Object aroundPrintLog(ProceedingJoinPoint pjp){
        Object result = null;
        try{
            Object[] args = pjp.getArgs();
            System.out.println(pjp.getSignature().getName());
            System.out.println("前置");
            result = pjp.proceed(args);
            System.out.println("后置");
            return result;
        }catch (Throwable t){
            System.out.println("異常");
            throw new RuntimeException(t);
        }finally {
            System.out.println("最終");
        }
    }
}
/**
	環(huán)繞通知:它是spring框架為我們提供的一種可以在代碼中手動控制增強(qiáng)方法何時(shí)執(zhí)行的方式。
*/

以上就是Spring中基于XML的面向切面編程(AOP)詳解的詳細(xì)內(nèi)容,更多關(guān)于Spring 于XML面向切面編程的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • springboot配置mysql連接的實(shí)例代碼

    springboot配置mysql連接的實(shí)例代碼

    這篇文章主要介紹了springboot配置mysql連接的實(shí)例代碼,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-01-01
  • Java標(biāo)識接口的使用方法

    Java標(biāo)識接口的使用方法

    在本篇文章中小編給大家分享了關(guān)于Java標(biāo)識接口的使用方法和教程內(nèi)容,有需要的朋友們學(xué)習(xí)下。
    2019-01-01
  • ThreadLocal導(dǎo)致JVM內(nèi)存泄漏原因探究

    ThreadLocal導(dǎo)致JVM內(nèi)存泄漏原因探究

    ThreadLocal是JDK提供的線程本地變量機(jī)制,但若使用不當(dāng)可能導(dǎo)致內(nèi)存泄漏。正確的使用方式是在使用完后及時(shí)remove,或者使用弱引用等手段避免強(qiáng)引用導(dǎo)致的內(nèi)存泄漏。在多線程編程中,合理使用ThreadLocal可以提高并發(fā)性能,但也需要注意其潛在的內(nèi)存泄漏問題
    2023-04-04
  • Java定時(shí)任務(wù)Timer、TimerTask與ScheduledThreadPoolExecutor詳解

    Java定時(shí)任務(wù)Timer、TimerTask與ScheduledThreadPoolExecutor詳解

    這篇文章主要介紹了Java定時(shí)任務(wù)Timer、TimerTask與ScheduledThreadPoolExecutor詳解,  定時(shí)任務(wù)就是在指定時(shí)間執(zhí)行程序,或周期性執(zhí)行計(jì)劃任務(wù),Java中實(shí)現(xiàn)定時(shí)任務(wù)的方法有很多,本文從從JDK自帶的一些方法來實(shí)現(xiàn)定時(shí)任務(wù)的需求,需要的朋友可以參考下
    2024-01-01
  • Spring @Conditional注解從源碼層講解

    Spring @Conditional注解從源碼層講解

    @Conditional是Spring4新提供的注解,它的作用是按照一定的條件進(jìn)行判斷,滿足條件給容器注冊bean,這篇文章主要介紹了Spring @Conditional注解示例詳細(xì)講解,需要的朋友可以參考下
    2023-01-01
  • log4j的Appenders配置方法

    log4j的Appenders配置方法

    下面小編就為大家?guī)硪黄猯og4j的Appenders配置方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-05-05
  • SpringBoot手動開啟事務(wù):DataSourceTransactionManager問題

    SpringBoot手動開啟事務(wù):DataSourceTransactionManager問題

    這篇文章主要介紹了SpringBoot手動開啟事務(wù):DataSourceTransactionManager問題,具有很好的價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • MyBatis在insert插入操作時(shí)返回主鍵ID的配置(推薦)

    MyBatis在insert插入操作時(shí)返回主鍵ID的配置(推薦)

    這篇文章主要介紹了MyBatis在insert插入操作時(shí)返回主鍵ID的配置的相關(guān)資料,需要的朋友可以參考下
    2017-10-10
  • java如何根據(jù)時(shí)間戳生成有序ID

    java如何根據(jù)時(shí)間戳生成有序ID

    這篇文章主要介紹了java如何根據(jù)時(shí)間戳生成有序ID問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-04-04
  • java 使用ConcurrentHashMap和計(jì)數(shù)器實(shí)現(xiàn)鎖

    java 使用ConcurrentHashMap和計(jì)數(shù)器實(shí)現(xiàn)鎖

    這篇文章主要介紹了java 使用ConcurrentHashMap和計(jì)數(shù)器實(shí)現(xiàn)鎖的相關(guān)資料,需要的朋友可以參考下
    2017-05-05

最新評論

阜宁县| 米林县| 罗平县| 康保县| 左权县| 衡阳县| 石楼县| 崇左市| 富蕴县| 临朐县| 赤城县| 茂名市| 腾冲县| 腾冲县| 平湖市| 桂平市| 阿坝县| 岳阳县| 渭源县| 江孜县| 仙桃市| 海城市| 三门县| 额尔古纳市| 墨玉县| 昆明市| 教育| 庆元县| 惠来县| 南康市| 子洲县| 湛江市| 彭阳县| 子洲县| 康马县| 巴塘县| 高台县| 江川县| 乌审旗| 陕西省| 封开县|