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

一文詳解Spring?AOP的配置與使用

 更新時(shí)間:2022年11月08日 11:03:54   作者:世界盡頭與你  
面向切面編程(俗稱AOP)提供了一種面向?qū)ο缶幊?俗稱OOP)的補(bǔ)充,面向?qū)ο缶幊套詈诵牡膯卧穷?class),然而面向切面編程最核心的單元是切面(Aspects)。本文就來(lái)和大家聊聊AOP的配置與使用,感興趣的可以了解一下

1.關(guān)于AOP

面向切面編程(俗稱AOP)提供了一種面向?qū)ο缶幊?俗稱OOP)的補(bǔ)充,面向?qū)ο缶幊套詈诵牡膯卧穷?class),然而面向切面編程最核心的單元是切面(Aspects)。與面向?qū)ο蟮捻樞蛄鞒滩煌珹OP采用的是橫向切面的方式,注入與主業(yè)務(wù)流程無(wú)關(guān)的功能,例如事務(wù)管理和日志管理。

圖示:

Spring的一個(gè)關(guān)鍵組件是AOP框架。 雖然Spring IoC容器不依賴于AOP(意味著你不需要在IOC中依賴AOP),但AOP為Spring IoC提供了非常強(qiáng)大的中間件解決方案。

AOP 是一種編程范式,最早由 AOP 聯(lián)盟的組織提出的,通過預(yù)編譯方式和運(yùn)行期動(dòng)態(tài)代理實(shí)現(xiàn)程序功能的統(tǒng)一維護(hù)的一種技術(shù)。它是 OOP的延續(xù)。利用 AOP 可以對(duì)業(yè)務(wù)邏輯的各個(gè)部分進(jìn)行隔離,從而使得業(yè)務(wù)邏輯各部分之間的耦合度降低,提高程序的可重用性,同時(shí)提高了開發(fā)的效率

2.初步使用AOP環(huán)境配置

要使用Spring AOP,需要導(dǎo)入如下的maven包:

<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.3.23</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.9.9.1</version>
</dependency>

在對(duì)應(yīng)的Spring配置文件中,需要導(dǎo)入aop的約束:

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

整體的配置如下:

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

???????</beans>

編寫接口類:UserService.java

public interface UserService {
    public void add();
    public void delete();
    public void update();
    public void query();
}

接口實(shí)現(xiàn)類:UserServiceImpl.java

public class UserServiceImpl 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 query() {
        System.out.println("查找用戶");
    }
}

待插入的前置日志類:Log.java

/**
 * 插入的前置日志類
 */
public class Log implements MethodBeforeAdvice {
    @Override
    public void before(Method method, Object[] args, Object target) throws Throwable {
        System.out.println(target.getClass().getName() + "的" + method.getName() + "被執(zhí)行了!");
    }
}

待插入的后置日志類:AfterLog.java

/**
 * 插入的后置日志類
 */
public class AfterLog implements AfterReturningAdvice {
    @Override
    public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
        System.out.println("執(zhí)行了" + method.getName() + "方法,返回結(jié)果為:" + returnValue);
    }
}

注冊(cè)類的bean標(biāo)簽:

<!-- 注冊(cè)測(cè)試bean -->
<bean id="userService" class="top.imustctf.service.UserServiceImpl"/>
<bean id="log" class="top.imustctf.log.Log"/>
<bean id="afterLog" class="top.imustctf.log.AfterLog"/>

3.使用原生Spring API接口實(shí)現(xiàn)AOP

配置aop:

切入點(diǎn)是待切入的方法,使用正則表達(dá)式匹配

執(zhí)行環(huán)繞增加是具體向切入點(diǎn)添加日志的配置

<!-- 配置AOP -->
<aop:config>
    <!-- 配置切入點(diǎn) -->
    <aop:pointcut id="pointcut" expression="execution(* top.imustctf.service.UserServiceImpl.*(..))"/>
    <!-- 執(zhí)行環(huán)繞增加 -->
    <aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
    <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
</aop:config>

現(xiàn)在來(lái)測(cè)試一下吧:

可以看到,AOP動(dòng)態(tài)代理切入成功了!

@Test
public void test() {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    UserService userService = context.getBean("userService", UserService.class);
    userService.add();
    // top.imustctf.service.UserServiceImpl的add被執(zhí)行了!
    // 增加用戶
    // 執(zhí)行了add方法,返回結(jié)果為:null
}

4.使用自定義類實(shí)現(xiàn)AOP

先Diy一個(gè)切面類:DiyPointCut.java

public class DiyPointCut {
    public void before() {
        System.out.println("方法執(zhí)行前");
    }

    public void after() {
        System.out.println("方法執(zhí)行后");
    }
}

注冊(cè)diy類并配置切面:

<bean id="diy" class="top.imustctf.diy.DiyPointCut"/>
<aop:config>
    <!-- 定義一個(gè)切面,ref中為要引用的類對(duì)象 -->
    <aop:aspect ref="diy">
        <!-- 配置切入點(diǎn) -->
        <aop:pointcut id="point" expression="execution(* top.imustctf.service.UserServiceImpl.*(..))"/>
        <!-- 通知 -->
        <aop:before method="before" pointcut-ref="point"/>
        <aop:after method="after" pointcut-ref="point"/>
    </aop:aspect>
</aop:config>

來(lái)開始測(cè)試:

@Test
public void test() {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    UserService userService = context.getBean("userService", UserService.class);
    userService.add();
    // 方法執(zhí)行前
    // 增加用戶
    // 方法執(zhí)行后
}

5.使用注解實(shí)現(xiàn)AOP

使用注解實(shí)現(xiàn)AOP,它更簡(jiǎn)單,更強(qiáng)大!

在使用注解開發(fā)前,需要在Spring配置文件中開啟動(dòng)態(tài)代理的支持:

<aop:aspectj-autoproxy/>

接下來(lái),使用注解直接開發(fā)AOP類:

@Component
@Aspect  // 標(biāo)注這個(gè)類是一個(gè)切面
public class AnnotationPointCut {
    @Before("execution(* top.imustctf.service.UserServiceImpl.*(..))")
    public void before() {
        System.out.println("方法執(zhí)行前??!");
    }

    @After("execution(* top.imustctf.service.UserServiceImpl.*(..))")
    public void after() {
        System.out.println("方法執(zhí)行后??!");
    }
}

現(xiàn)在來(lái)測(cè)試一下吧:

@Test
public void test() {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    UserService userService = context.getBean("userService", UserService.class);
    userService.add();
    // 方法執(zhí)行前??!
    // 增加用戶
    // 方法執(zhí)行后??!
}

到此這篇關(guān)于一文詳解Spring AOP的配置與使用的文章就介紹到這了,更多相關(guān)Spring AOP內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Mybatis逆向工程筆記小結(jié)

    Mybatis逆向工程筆記小結(jié)

    MyBatis官方為我們提供了一個(gè)逆向工程,通過這個(gè)逆向工程,只需要建立好數(shù)據(jù)表,MyBatis就會(huì)根據(jù)這個(gè)表自動(dòng)生成pojo類、mapper接口、sql映射文件,本文主要介紹了Mybatis逆向工程筆記小結(jié),具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-05-05
  • Java中內(nèi)存分配的幾種方法

    Java中內(nèi)存分配的幾種方法

    本文主要介紹Java中幾種分配內(nèi)存的方法。我們會(huì)看到如何使用sun.misc.Unsafe來(lái)統(tǒng)一操作任意類型的內(nèi)存。以前用C語(yǔ)言開發(fā)的同學(xué)通常都希望能在Java中通過較底層的接口來(lái)操作內(nèi)存,他們一定會(huì)對(duì)本文中要講的內(nèi)容感興趣
    2014-03-03
  • Java字符編碼解碼的實(shí)現(xiàn)詳解

    Java字符編碼解碼的實(shí)現(xiàn)詳解

    本篇文章介紹了,Java字符編碼解碼的實(shí)現(xiàn)詳解。需要的朋友參考下
    2013-05-05
  • springcloud之FeignClient使用詳解

    springcloud之FeignClient使用詳解

    Feign是一種聲明式、模板化的HTTP客戶端,可以簡(jiǎn)化微服務(wù)之間的遠(yuǎn)程過程調(diào)用,通過Feign,開發(fā)者可以像調(diào)用本地方法一樣調(diào)用遠(yuǎn)程服務(wù),而無(wú)需感知這是一個(gè)HTTP請(qǐng)求,引入Feign后,微服務(wù)之間的調(diào)用流程更加簡(jiǎn)化,結(jié)合Ribbon實(shí)現(xiàn)了路由負(fù)載、超時(shí)熔斷等功能
    2024-12-12
  • Java將時(shí)間按月份分段的實(shí)現(xiàn)思路與方法

    Java將時(shí)間按月份分段的實(shí)現(xiàn)思路與方法

    這篇文章主要給大家介紹了關(guān)于Java將時(shí)間按月份分段的實(shí)現(xiàn)思路與方法,通過文中介紹的方法可以將時(shí)間分成我們想要的時(shí)間段,文中給出了詳細(xì)的實(shí)例代碼,需要的朋友可以參考下
    2021-07-07
  • IntelliJ IDEA 刷題利器 LeetCode 插件詳解

    IntelliJ IDEA 刷題利器 LeetCode 插件詳解

    這篇文章主要介紹了IntelliJ IDEA 刷題利器 LeetCode 插件,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-08-08
  • Netty核心功能之?dāng)?shù)據(jù)容器ByteBuf詳解

    Netty核心功能之?dāng)?shù)據(jù)容器ByteBuf詳解

    這篇文章主要為大家介紹了Netty核心功能之?dāng)?shù)據(jù)容器ByteBuf詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-10-10
  • Jmeter線程組傳參原理解析

    Jmeter線程組傳參原理解析

    這篇文章主要介紹了jmeter線程組傳參原理解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-02-02
  • Java tomcat環(huán)境變量及idea配置解析

    Java tomcat環(huán)境變量及idea配置解析

    這篇文章主要介紹了Java tomcat環(huán)境變量及idea配置解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-12-12
  • Java HashMap實(shí)現(xiàn)原理分析(一)

    Java HashMap實(shí)現(xiàn)原理分析(一)

    這篇文章主要介紹了Java HashMap實(shí)現(xiàn)原理的分析,幫助大家更好的理解和使用Java,感興趣的朋友可以了解下
    2020-08-08

最新評(píng)論

荣昌县| 临泽县| 抚松县| 三门峡市| 赞皇县| 河源市| 横峰县| 枝江市| 宁阳县| 西青区| 馆陶县| 邹城市| 萝北县| 龙州县| 汉沽区| 安岳县| 抚松县| 双峰县| 岳普湖县| 郎溪县| 应用必备| 瑞丽市| 西城区| 高青县| 定安县| 乐陵市| 乃东县| 尼勒克县| 库伦旗| 平原县| 涟源市| 嫩江县| 东阳市| 且末县| 青铜峡市| 榕江县| 旬邑县| 静宁县| 工布江达县| 明溪县| 老河口市|