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

Spring框架實現(xiàn)AOP的兩種方式詳解

 更新時間:2022年09月04日 09:58:23   作者:那人獨釣寒江雪.  
這篇文章主要為大家詳細介紹了Spring框架實現(xiàn)AOP的兩種方式,文中的示例代碼講解詳細,對我們學習有一定的借鑒價值,需要的可以參考一下

第一種AOP實現(xiàn)方式

AfterLog

package com.xxx.demo.service1;

import org.junit.After;
import org.springframework.aop.AfterReturningAdvice;

import java.lang.reflect.Method;

public class AfterLog implements AfterReturningAdvice {
    @Override
    //returnValue:返回值
    public void afterReturning(Object returnValue, Method method, Object[] objects, Object o1) throws Throwable {
        System.out.println(
                "執(zhí)行了"+method.getName()+"返回的結果:"+returnValue
        );
    }
}

Log

package com.xxx.demo.service1;

import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

//前置通知
public class log implements MethodBeforeAdvice {
    @Override
    //method:要執(zhí)行的目標對象的方法 args:參數(shù) target:目標讀寫
    public void before(Method method, Object[] args, Object target) throws Throwable {
        System.out.println(target.getClass().getName()+"的"+method.getName()+"被執(zhí)行了");
    }
}

配置文件

applicationContext.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-->
    <bean id="userService" class="com.xxx.demo.service1.UserServicelmp"></bean>
    <bean id="log" class="com.xxx.demo.service1.log"></bean>
    <bean id="afterLog" class="com.xxx.demo.service1.AfterLog"></bean>


<!--    配置aop:需要導入aop的約束-->
    <aop:config>
        <!--        切入點:expression:表達式,execution(要執(zhí)行的位置!* * * *)-->
        <aop:pointcut id="pointcut" expression="execution(* com.xxx.demo.service1.UserServicelmp.*(..))"/>

<!--        執(zhí)行環(huán)繞增加  把log的類添加到切入點里面-->
        <aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
        <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"></aop:advisor>
    </aop:config>
</beans>

實例調用

package com.xxx.demo.service1;

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

public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        //動態(tài)代理代理的是接口
        UserService userService =(UserService) context.getBean("userService");
        userService.add();
        userService.delete();
        userService.select();
        userService.update();
    }
}

定義接口

package com.xxx.demo.service1;

public class UserServicelmp implements UserService{
    @Override
    public void add() {
        System.out.println("增加了一個用戶");
    }

    @Override
    public void delete() {
        System.out.println("刪除了一個用戶");
    }

    @Override
    public void update() {
        System.out.println("更新了一個用戶");
    }

    @Override
    public void select() {
        System.out.println("查詢了一個用戶");
    }
}

第二種AOP實現(xiàn)方式

<?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-->
    <bean id="userService" class="com.xxx.demo.service1.UserServicelmp"></bean>
    <bean id="log" class="com.xxx.demo.service1.log"></bean>
    <bean id="afterLog" class="com.xxx.demo.service1.AfterLog"></bean>
<!--    方式二:自定義類-->
    <bean id="diy" class="com.xxx.demo.service1.DiyPointCut"></bean>
    <aop:config>
<!--        自定義切面 ref 要引用的類-->
        <aop:aspect ref="diy">
<!--            切入點-->
            <aop:pointcut id="point" expression="execution(* com.xxx.demo.service1.UserServicelmp.*(..))"/>
<!--                通知-->
            <aop:before method="before" pointcut-ref="point"></aop:before>
            <aop:after method="after" pointcut-ref="point"></aop:after>
        </aop:aspect>
    </aop:config>
</beans>

到此這篇關于Spring框架實現(xiàn)AOP的兩種方式詳解的文章就介紹到這了,更多相關Spring實現(xiàn)AOP內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • 在 Spring Boot 中使用 @Autowired和 @Bean注解的示例詳解

    在 Spring Boot 中使用 @Autowired和 @Bean

    本文通過一個示例演示了如何在SpringBoot中使用@Autowired和@Bean注解進行依賴注入和Bean管理,示例中定義了一個Student類,并通過配置類TestConfig初始化Student對象,在測試類中,通過@Autowired注解自動注入Student對象并輸出其屬性值,感興趣的朋友跟隨小編一起看看吧
    2025-02-02
  • IntelliJ IDEA中查看當前類的所有繼承關系圖

    IntelliJ IDEA中查看當前類的所有繼承關系圖

    今天小編就為大家分享一篇關于IntelliJ IDEA中查看當前類的所有繼承關系圖,小編覺得內容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2018-10-10
  • PowerJob的MapProcessor工作流程源碼解讀

    PowerJob的MapProcessor工作流程源碼解讀

    這篇文章主要為大家介紹了PowerJob的MapProcessor工作流程源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2024-01-01
  • spring boot 測試單元修改數(shù)據(jù)庫不成功的解決

    spring boot 測試單元修改數(shù)據(jù)庫不成功的解決

    這篇文章主要介紹了spring boot 測試單元修改數(shù)據(jù)庫不成功的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • springboot 2.0 mybatis mapper-locations掃描多個路徑的實現(xiàn)

    springboot 2.0 mybatis mapper-locations掃描多個路徑的實現(xiàn)

    這篇文章主要介紹了springboot 2.0 mybatis mapper-locations掃描多個路徑的實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • Java實現(xiàn)將CSV轉為Excel的示例代碼

    Java實現(xiàn)將CSV轉為Excel的示例代碼

    CSV(Comma?Separated?Values)文件是一種純文本文件,包含用逗號分隔的數(shù)據(jù),常用于將數(shù)據(jù)從一個應用程序導入或導出到另一個應用程序。本文將利用Java實現(xiàn)CSV轉為Excel,感興趣的可以了解一下
    2022-03-03
  • java實現(xiàn)微信點餐申請微信退款

    java實現(xiàn)微信點餐申請微信退款

    這篇文章主要為大家詳細介紹了java實現(xiàn)微信點餐申請微信退款,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-09-09
  • 解決idea中debug工具欄消失后如何顯示的問題

    解決idea中debug工具欄消失后如何顯示的問題

    這篇文章主要介紹了解決idea中debug工具欄消失后如何顯示的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-02-02
  • Springboot詳解RocketMQ實現(xiàn)消息發(fā)送與接收流程

    Springboot詳解RocketMQ實現(xiàn)消息發(fā)送與接收流程

    這篇文章主要介紹了SpringBoot整合RocketMQ實現(xiàn)消息發(fā)送和接收功能,我們使用主流的SpringBoot框架整合RocketMQ來講解,使用方便快捷,本文分步驟給大家介紹的非常詳細,需要的朋友可以參考下
    2022-06-06
  • spring gateway如何解決跨域問題

    spring gateway如何解決跨域問題

    這篇文章主要介紹了spring gateway如何解決跨域問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-04-04

最新評論

申扎县| 格尔木市| 安泽县| 广宗县| 旺苍县| 弥勒县| 通渭县| 新蔡县| 商河县| 内黄县| 聂荣县| 山东省| 高碑店市| 静乐县| 崇义县| 镇沅| 东丰县| 寻甸| 徐汇区| 措美县| 晋宁县| 比如县| 东丰县| 淄博市| 邵阳县| 德化县| 武城县| 丰台区| 竹山县| 基隆市| 灵璧县| 镇巴县| 兴山县| 南丹县| 正蓝旗| 祁东县| 喀什市| 玉环县| 惠东县| 永胜县| 孝义市|