欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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 {
    //連接點(diǎn)/切入點(diǎn)
    public void add(){
        System.out.println("add......");
    }
}

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

@Component
@Aspect  //生成代理對(duì)象
public class UserProxy {
}

3.配置文件中開啟自動(dòng)代理

<?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生成代理對(duì)象-->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

4.通知類型注解

@Before -- 前置通知

@AfterReturing -- 后置通知

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

@After -- 最終通知

@AfterThrowing -- 異常拋出通知

package com.qcby;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
@Component
@Aspect  //生成代理對(duì)象
public class UserProxy {
    //增強(qiáng)/通知  ---》前置通知
    @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í)行被增強(qiáng)的方法
        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("出錯(cuò)了.............");
    }
}

5.測(cè)試類

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)文章

  • 詳解IntelliJ IDEA 2020 的Debug功能

    詳解IntelliJ IDEA 2020 的Debug功能

    這篇文章主要介紹了IntelliJ IDEA 2020 的Debug功能,本文通過實(shí)例截圖相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-04-04
  • Springcloud Nacos基本操作代碼實(shí)例

    Springcloud Nacos基本操作代碼實(shí)例

    這篇文章主要介紹了Springcloud Nacos基本操作代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-12-12
  • java中hashCode方法與equals方法的用法總結(jié)

    java中hashCode方法與equals方法的用法總結(jié)

    總的來說,Java中的集合(Collection)有兩類,一類是List,再有一類是Set。前者集合內(nèi)的元素是有序的,元素可以重復(fù);后者元素?zé)o序,但元素不可重復(fù)
    2013-10-10
  • springboot實(shí)現(xiàn)mock平臺(tái)的示例代碼

    springboot實(shí)現(xiàn)mock平臺(tái)的示例代碼

    本文主要介紹了springboot實(shí)現(xiàn)mock平臺(tái)的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-06-06
  • Java責(zé)任鏈模式定義與用法分析

    Java責(zé)任鏈模式定義與用法分析

    這篇文章主要介紹了Java責(zé)任鏈模式定義與用法,結(jié)合具體實(shí)例分析了java責(zé)任鏈模式的功能、定義、使用方法、適用情況等,需要的朋友可以參考下
    2017-06-06
  • java自動(dòng)根據(jù)文件內(nèi)容的編碼來讀取避免亂碼

    java自動(dòng)根據(jù)文件內(nèi)容的編碼來讀取避免亂碼

    這篇文章主要介紹了java自動(dòng)根據(jù)文件內(nèi)容的編碼來讀取避免亂碼,需要的朋友可以參考下
    2014-02-02
  • 詳解JDBC使用

    詳解JDBC使用

    JDBC(Java Database Connectivity),即Java數(shù)據(jù)庫(kù)連接,是一種用于執(zhí)行SQL語(yǔ)句的Java API,可以為多種關(guān)系數(shù)據(jù)庫(kù)提供同一訪問,它由一組用Java語(yǔ)言編寫的類和接口組成。
    2017-05-05
  • java ReentrantLock詳解

    java ReentrantLock詳解

    這篇文章主要介紹了java ReentrantLock,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • 對(duì)比Java設(shè)計(jì)模式編程中的狀態(tài)模式和策略模式

    對(duì)比Java設(shè)計(jì)模式編程中的狀態(tài)模式和策略模式

    這篇文章主要介紹了Java設(shè)計(jì)模式編程中的狀態(tài)模式和策略模式對(duì)比,文中列舉了兩種模式的相似點(diǎn)和不同點(diǎn),并都舉了代碼的實(shí)例作為參照,需要的朋友可以參考下
    2016-04-04
  • Kotlin與Java 泛型缺陷和應(yīng)用場(chǎng)景詳解

    Kotlin與Java 泛型缺陷和應(yīng)用場(chǎng)景詳解

    這篇文章主要為大家介紹了Kotlin與Java 泛型缺陷和應(yīng)用場(chǎng)景詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-12-12

最新評(píng)論