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

Spring Aop注解實現(xiàn)

 更新時間:2021年07月16日 14:33:44   作者:寧在春  
本文我們通過Spring AOP和Java的自定義注解來實現(xiàn)日志的插入功能,非常不錯,具有一定的參考借鑒價值,需要的朋友一起看看吧,希望對你有所幫助

Spring-aop-理論知識 Spring-Aop-注解實現(xiàn) 項目結(jié)構(gòu)圖

在這里插入圖片描述

具體步驟:

1、創(chuàng)建maven 項目 導(dǎo)入依賴 創(chuàng)建好項目結(jié)構(gòu)

    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.18</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.3.4</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>5.3.4</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>5.3.4</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>5.3.4</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>5.3.4</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.6</version>
        </dependency>
    </dependencies>

2、寫一個接口 及 其實現(xiàn)類

/**
 * @version 1.0
 * @author: crush
 * @date: 2021-03-05 10:26
 */
public interface TestDao {
    public void delete();
}
/**
 * @version 1.0
 * @author: crush
 * @date: 2021-03-05 10:27
 */
@Service
public class TestDaoImpl implements TestDao {
    public void delete() {
        System.out.println("正在執(zhí)行的方法-->刪除");
    }
}

3、切面類

/**
 * @version 1.0
 * @author: crush
 * @date: 2021-03-10 18:04
 */
@Aspect
@Component
public class MyAspectAnnotation {
    @Pointcut("execution(* com.dao.*.*(..))")
    private void myPointCut(){
    }
    /**
     * 前置通知 使用JoinPoint 接口作為參數(shù)獲得目標(biāo)對象的信息
     *    @Before("myPointCut()") ==
     *    <aop:after method="after" pointcut-ref="myPointCut"/>
     **/
    @Before("myPointCut()")
    public void before(JoinPoint jp){
        System.out.print("前置通知:模擬權(quán)限控制");
        System.out.println("目標(biāo)對象:"+jp.getTarget()+",被增強的方法:"+jp.getSignature().getName());
    }
    @AfterReturning("myPointCut()")
    public void afterReturning(JoinPoint jp){
        System.out.print("后置返回通知:"+"模擬刪除臨時文件");
        System.out.println(",被增強的方法"+jp.getSignature().getName());
    }
    @Around("myPointCut()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("環(huán)繞開始:執(zhí)行目標(biāo)方法前,模擬開啟事務(wù)");
        Object obj = pjp.proceed();
        System.out.println("環(huán)繞結(jié)束:執(zhí)行目標(biāo)方法后,模擬關(guān)閉事務(wù)");
        return obj;
    }
    @AfterThrowing(value = "myPointCut()",throwing = "throwable")
    public void except(Throwable throwable){
        System.out.println("異常通知:"+"程序執(zhí)行異常"+throwable.getMessage());
    }

    @After("myPointCut()")
    public void after(){
        System.out.println("最終通知:釋放資源");
    }

}

4、application.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 https://www.springframework.org/schema/aop/spring-aop.xsd">
        <context:component-scan base-package="com.dao"/>
        <context:component-scan base-package="com.aspect"/>
        <!--啟動基于注解的AspectJ支持-->
        <aop:aspectj-autoproxy proxy-target-class="true"/>
</beans>

測試

    @Test
    public void Test(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        TestDao testDao = applicationContext.getBean("testDaoImpl", TestDaoImpl.class);
        testDao.delete();
        /**
         * 輸出:
         * 環(huán)繞開始:執(zhí)行目標(biāo)方法前,模擬開啟事務(wù)
         * 前置通知:模擬權(quán)限控制目標(biāo)對象:com.dao.TestDaoImpl@2bef51f2,被增強的方法:delete
         * 正在執(zhí)行的方法-->刪除
         * 后置返回通知:模擬刪除臨時文件,被增強的方法delete
         * 最終通知:釋放資源
         * 環(huán)繞結(jié)束:執(zhí)行目標(biāo)方法后,模擬關(guān)閉事務(wù)
         */
    }

總結(jié)

本篇文章就到這里了,希望能給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!

相關(guān)文章

  • IntelliJ IDEA進行中文漢化的詳細教程(附圖文講解)

    IntelliJ IDEA進行中文漢化的詳細教程(附圖文講解)

    今天為大家?guī)淼氖?nbsp;IntelliJ IDEA 中文漢化教程以及中文插件包下載教程,經(jīng)常收到小伙伴在后臺給我留言,問 IDEA 怎么進行中文漢化,因為很多小伙伴是剛?cè)腴T Java,看到 IDEA 菜單全英文有些不太適應(yīng),需要的朋友可以參考下
    2024-12-12
  • SpringBoot如何對LocalDateTime進行格式化并解析

    SpringBoot如何對LocalDateTime進行格式化并解析

    這篇文章主要介紹了SpringBoot如何對LocalDateTime進行格式化方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • Java servlet、filter、listener、interceptor之間的區(qū)別和聯(lián)系

    Java servlet、filter、listener、interceptor之間的區(qū)別和聯(lián)系

    這篇文章主要介紹了Java servlet、filter、listener、interceptor之間的區(qū)別和聯(lián)系的相關(guān)資料,需要的朋友可以參考下
    2016-11-11
  • 幾種常見mybatis分頁的實現(xiàn)方式

    幾種常見mybatis分頁的實現(xiàn)方式

    這篇文章主要介紹了幾種常見mybatis分頁的實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • Flink部署集群整體架構(gòu)源碼分析

    Flink部署集群整體架構(gòu)源碼分析

    這篇文章主要為大家介紹了Flink部署集群及整體架構(gòu)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-12-12
  • myeclipse安裝Spring Tool Suite(STS)插件的方法步驟

    myeclipse安裝Spring Tool Suite(STS)插件的方法步驟

    這篇文章主要介紹了myeclipse安裝Spring Tool Suite(STS)插件的方法步驟,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • Java List簡介_動力節(jié)點Java學(xué)院整理

    Java List簡介_動力節(jié)點Java學(xué)院整理

    Java中可變數(shù)組的原理就是不斷的創(chuàng)建新的數(shù)組,將原數(shù)組加到新的數(shù)組中,下文對Java List用法做了詳解。需要的朋友參考下吧
    2017-05-05
  • Collection stream使用示例詳解

    Collection stream使用示例詳解

    這篇文章主要介紹了Collection stream使用示例,stream流幾乎可以完成對集合的任意操作,映射、去重、分組、排序、過濾等
    2022-12-12
  • Java中final關(guān)鍵字詳解及實例

    Java中final關(guān)鍵字詳解及實例

    這篇文章主要介紹了Java中final關(guān)鍵字詳解及實例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • 詳解eclipse項目中.classpath文件的使用

    詳解eclipse項目中.classpath文件的使用

    這篇文章主要介紹了詳解eclipse項目中.classpath文件的使用,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-10-10

最新評論

桃园市| 百色市| 芒康县| 佛山市| 准格尔旗| 永济市| 潜江市| 会理县| 灵丘县| 修武县| 平远县| 乡城县| 谢通门县| 交城县| 金山区| 辰溪县| 东兰县| 青铜峡市| 曲阳县| 东辽县| 绩溪县| 吉水县| 玉山县| 三门峡市| 沂源县| 保康县| 原阳县| 宝丰县| 霍林郭勒市| 乐平市| 虎林市| 清原| 潼关县| 贵州省| 连云港市| 突泉县| 曲麻莱县| 元阳县| 桃园市| 汝城县| 吴桥县|