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

Java?SpringAOP技術(shù)之注解方式詳解

 更新時間:2022年02月23日 11:55:21   作者:C'z?x  
這篇文章主要為大家詳細(xì)介紹了Java?SpringAOP技術(shù)之注解方式,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助

1.配置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">
    <!--開啟注解掃描-->
    <context:component-scan base-package="com.qcby"></context:component-scan>
</beans>

2.配置注解

package com.qcby;
import org.springframework.stereotype.Component;
@Component(value = "user")
public class User {
    //連接點/切入點
    public void add(){
        System.out.println("add......");
    }
}

給切面類添加注解 @Aspect,編寫增強的方法,使用通知類型注解聲明

@Component
@Aspect  //生成代理對象
public class UserProxy {
}

3.配置文件中開啟自動代理

<?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">
    <!--開啟掃描-->
    <context:component-scan base-package="com.qcby"></context:component-scan>
    <!--開啟Aspect生成代理對象-->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

4.通知類型注解

@Before -- 前置通知

@AfterReturing -- 后置通知

@Around -- 環(huán)繞通知(目標(biāo)對象方法默認(rèn)不執(zhí)行的,需要手動執(zhí)行)

@After -- 最終通知

@AfterThrowing -- 異常拋出通知

package com.qcby;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
@Component
@Aspect  //生成代理對象
public class UserProxy {
    //增強/通知  ---》前置通知
    @Before(value = "execution(public void com.qcby.User.add())")
    public void before(){
        System.out.println("前置通知.............");
    }
    // 環(huán)繞通知
    @Around(value = "execution(public void com.qcby.User.add())")
    public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("環(huán)繞前置.............");
        //  執(zhí)行被增強的方法
        proceedingJoinPoint.proceed();
        System.out.println("環(huán)繞后置.............");
    }
    // 最終通知
    @After(value = "execution(public void com.qcby.User.add())")
    public void after() {
        System.out.println("最終通知.............");
    }
    //后置通知
    @AfterReturning(value = "execution(public void com.qcby.User.add())")
    public void afterReturning() {
        System.out.println("后置通知.............");
    }
    //異常通知
    @AfterThrowing(value = "execution(public void com.qcby.User.add())")
    public void afterThrowing() {
        System.out.println("出錯了.............");
    }
}

5.測試類

package com.qcby.test;
import com.qcby.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class UserTest {
    @Test
    public void aopTest1(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("SpringConfig.xml");
        User user = (User) applicationContext.getBean("user");
        user.add();
    }
}

6.結(jié)果 

總結(jié)

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

相關(guān)文章

  • java 線程鎖詳細(xì)介紹及實例代碼

    java 線程鎖詳細(xì)介紹及實例代碼

    這篇文章主要介紹了java 線程鎖詳細(xì)介紹及實例代碼的相關(guān)資料,需要的朋友可以參考下
    2016-12-12
  • JDK源碼中一些實用的“小技巧”總結(jié)

    JDK源碼中一些實用的“小技巧”總結(jié)

    這篇文章主要給大家總結(jié)介紹了關(guān)于JDK源碼中一些實用的“小技巧”,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用jdk源碼具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2018-03-03
  • 如何用idea編寫并運行第一個spark scala處理程序

    如何用idea編寫并運行第一個spark scala處理程序

    詳細(xì)介紹了如何使用IntelliJ IDEA創(chuàng)建Scala項目,包括配置JDK和Scala SDK,添加Maven支持,編輯pom.xml,并創(chuàng)建及運行Scala程序,這為Scala初學(xué)者提供了一個基礎(chǔ)的項目搭建和運行指南
    2024-09-09
  • springboot啟動feign項目報錯:Service id not legal hostnam的解決

    springboot啟動feign項目報錯:Service id not legal hostnam的解決

    這篇文章主要介紹了springboot啟動feign項目報錯:Service id not legal hostnam的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • 使用spring注入枚舉類型作為參數(shù)

    使用spring注入枚舉類型作為參數(shù)

    這篇文章主要介紹了使用spring注入枚舉類型作為參數(shù),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • java發(fā)送http get請求的兩種方法(總結(jié))

    java發(fā)送http get請求的兩種方法(總結(jié))

    下面小編就為大家?guī)硪黄猨ava發(fā)送http get請求的兩種方法(總結(jié))。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-05-05
  • spring cloud feign實現(xiàn)遠(yuǎn)程調(diào)用服務(wù)傳輸文件的方法

    spring cloud feign實現(xiàn)遠(yuǎn)程調(diào)用服務(wù)傳輸文件的方法

    這篇文章主要介紹了spring cloud feign實現(xiàn)遠(yuǎn)程調(diào)用服務(wù)傳輸文件的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-09-09
  • MySQL 新增字段但 Java 實體未更新存在潛在問題與解決方案

    MySQL 新增字段但 Java 實體未更新存在潛在問題與解決方案

    在 Java + MySQL 的開發(fā)中,我們通常使用 ORM 框架(如 MyBatis、MyBatis-Plus、Hibernate)來映射數(shù)據(jù)庫表與 Java 對象,這篇文章主要介紹了MySQL 新增字段但 Java 實體未更新:潛在問題與解決方案,需要的朋友可以參考下
    2025-04-04
  • 詳解JVM中的本機內(nèi)存跟蹤

    詳解JVM中的本機內(nèi)存跟蹤

    在本文里小編給大家整理了一篇關(guān)于JVM中的本機內(nèi)存跟蹤的相關(guān)知識點內(nèi)容,有興趣的朋友們參考學(xué)習(xí)下。
    2019-07-07
  • 詳解SpringSecurity如何實現(xiàn)前后端分離

    詳解SpringSecurity如何實現(xiàn)前后端分離

    這篇文章主要為大家介紹了詳解SpringSecurity如何實現(xiàn)前后端分離,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-03-03

最新評論

原平市| 信阳市| 富源县| 垫江县| 凌海市| 安仁县| 汾西县| 建瓯市| 麻栗坡县| 双辽市| 长治县| 卓尼县| 顺昌县| 闽侯县| 吉水县| 靖州| 二连浩特市| 保定市| 同德县| 张家口市| 句容市| 邵东县| 上高县| 温宿县| 沁阳市| 永丰县| 灌云县| 赤水市| 高密市| 宾阳县| 德令哈市| 科技| 越西县| 莱芜市| 巴林右旗| 林西县| 彩票| 崇礼县| 章丘市| 台山市| 苏尼特右旗|