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

Spring深入講解實現(xiàn)AOP的三種方式

 更新時間:2022年05月20日 11:17:36   作者:張志明(努力奮斗版)  
Spring的AOP就是通過動態(tài)代理實現(xiàn)的,使用了兩個動態(tài)代理,分別是JDK的動態(tài)代理和CGLIB動態(tài)代理,本文重點給大家介紹下Spring?Aop的三種實現(xiàn),感興趣的朋友一起看看吧

[重點] 使用AOP織入 需要導(dǎo)入一個依賴包

  <dependencies>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.9.1</version>
        </dependency>
    </dependencies>

方式一:使用原生Spring API接口

配置文件

<?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:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c"
       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">
<!--    注冊bean-->
    <bean id="userService" class="com.kero.service.UserServiceImpl"/>
    <bean id="log" class="com.kero.log.Log"/>
    <bean id="afterlLog" class="com.kero.log.AfterLog"/>
<!--    配置aop 需要導(dǎo)入aop的約束-->
    <aop:config>
<!--        切入點 expression表達(dá)式 expression(要執(zhí)行的位置  )-->
        <aop:pointcut id="pointcut" expression="execution(* com.kero.service.UserServiceImpl.*(..))"/>
<!--        執(zhí)行環(huán)繞增加-->
        <aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
        <aop:advisor advice-ref="afterlLog" pointcut-ref="pointcut"/>
    </aop:config>
</beans>

Log

import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
public class Log implements MethodBeforeAdvice {
//method:要執(zhí)行的目標(biāo)對象的方法
    //objects:參數(shù)
    //target:目標(biāo)對象
    @Override
    public void before(Method method, Object[] args, Object target) throws Throwable {
        System.out.println(target.getClass().getName() + "的" + method.getName() + "被執(zhí)行了");
    }
}
import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;
public class AfterLog implements AfterReturningAdvice {
    //method:要執(zhí)行的目標(biāo)對象的方法
    //objects:參數(shù)
    //target:目標(biāo)對象
    //returnValue:返回值
    @Override
    public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
        System.out.println("執(zhí)行了"+ method.getName() + "方法,返回結(jié)果為:"+returnValue);
    }
}

Service

import org.springframework.stereotype.Service;
@Service
public interface UserService {
    public void add();
    public void delete();
    public void update();
    public void search();
}
public class UserServiceImpl implements UserService{
    @Override
    public void add() {
    }
    @Override
    public void delete() {
    }
    @Override
    public void update() {
    }
    @Override
    public void search() {
    }
}

test動態(tài)代理 代理的是接口(代理模式是SpringAOP的底層)

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class test {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //動態(tài)代理 代理的是接口
        UserService userService = context.getBean("userService", UserService.class);
        userService.add();
    }
}

方式二:使用自定義類

配置文件

<?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:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c"
       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">
<!--    注冊bean-->
    <bean id="userService" class="com.kero.service.UserServiceImpl"/>
    <bean id="log" class="com.kero.log.Log"/>
    <bean id="afterlLog" class="com.kero.log.AfterLog"/>
    <bean id="diy" class="com.kero.diy.DiyPointCut"/>
    <aop:config>
<!--        自定義切面 ref要引用的類-->
        <aop:aspect ref="diy">
<!--            切入點-->
            <aop:pointcut id="point" expression="execution(* com.kero.service.UserServiceImpl.*(..))"/>
<!--            通知-->
            <aop:before method="before" pointcut-ref="point"/>
            <aop:after method="after" pointcut-ref="point"/>
        </aop:aspect>
    </aop:config>
</beans>

DIY類

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

其他的不變

方式三:使用注解實現(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:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       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 http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!--    注冊bean-->
    <bean id="userService" class="com.kero.service.UserServiceImpl"/>
    <bean id="log" class="com.kero.log.Log"/>
    <bean id="afterlLog" class="com.kero.log.AfterLog"/>
<!--    開啟注解支持-->
    <aop:aspectj-autoproxy/>
    <bean id="annotationPointCut" class="com.kero.diy.AnnotationPointCut"/>
</beans>

自定義類

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
//使用注解方式實現(xiàn)AOP
@Aspect
//標(biāo)注這個類是一個切面
public class AnnotationPointCut {
    @Before("execution(* com.kero.service.UserServiceImpl.*(..))")
    public void before(){
        System.out.println("````方法執(zhí)行前````");
    }
    @After("execution(* com.kero.service.UserServiceImpl.*(..))")
    public void after(){
        System.out.println("````方法執(zhí)行后````");
    }
}

其他不變

補(bǔ)充知識:execution表達(dá)式

execution表達(dá)式的詳解

切入點表達(dá)式:execution(* 包名.*.*(..))

整個表達(dá)式可以分為五個部分:

1、execution(): 表達(dá)式主體。

2、第一個*號:方法返回類型, *號表示所有的類型。

3、包名:表示需要攔截的包名。

4、第二個*號:表示類名,*號表示所有的類。

5、*(..):最后這個星號表示方法名,*號表示所有的方法,后面( )里面表示方法的參數(shù),兩個句點表示任何參數(shù)

其中除了返回類型模式、方法名模式和參數(shù)模式外,其它項都是可選的。

舉例:

execution(public * *(..)) 匹配所有的public修飾符的方法

execution(* set*(..)) 匹配所有”set”開頭的方法:

execution(* com.kero.service.UserServiceImpl.*(..))) 匹配UserServiceImpl接口/類的所有方法:

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

相關(guān)文章

  • java開源好用的簡繁轉(zhuǎn)換類庫推薦

    java開源好用的簡繁轉(zhuǎn)換類庫推薦

    這篇文章主要為大家介紹了java開源好用的簡繁轉(zhuǎn)換類庫推薦,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-08-08
  • Java啟用Azure Linux虛擬機(jī)診斷設(shè)置

    Java啟用Azure Linux虛擬機(jī)診斷設(shè)置

    這篇文章主要介紹了Java啟用Azure Linux虛擬機(jī)診斷設(shè)置,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-05-05
  • Java Builder模式構(gòu)建MAP/LIST的實例講解

    Java Builder模式構(gòu)建MAP/LIST的實例講解

    下面小編就為大家?guī)硪黄狫ava Builder模式構(gòu)建MAP/LIST的實例講解。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-10-10
  • Java實現(xiàn)PDF文件的分割與加密功能

    Java實現(xiàn)PDF文件的分割與加密功能

    這篇文章主要為大家分享了如何利用Java語言實現(xiàn)PDF文件的分割與加密以及封面圖的生成,文中的示例代碼簡潔易懂,感興趣的可以了解一下
    2022-04-04
  • Docker使用Dockerfile自定義鏡像方式

    Docker使用Dockerfile自定義鏡像方式

    這篇文章主要介紹了Docker使用Dockerfile自定義鏡像方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • Java Servlet中Response對象的使用方法

    Java Servlet中Response對象的使用方法

    本文介紹了Java Servlet中Response對象的使用方法,包括設(shè)置響應(yīng)頭、設(shè)置響應(yīng)編碼、向客戶端發(fā)送數(shù)據(jù)、重定向等操作,同時介紹了常用的響應(yīng)狀態(tài)碼和響應(yīng)類型,幫助讀者更好地理解和掌握Servlet中Response對象的用法
    2023-05-05
  • Java map的學(xué)習(xí)及代碼示例

    Java map的學(xué)習(xí)及代碼示例

    這篇文章主要介紹了Java map的學(xué)習(xí)及代碼示例,簡單介紹了map與collection的比較,map的相關(guān)內(nèi)容,分享了map的一些簡介代碼示例,具有一定參考價值,需要的朋友可以了解下。
    2017-11-11
  • SpringAOP 如何通過JoinPoint獲取參數(shù)名和值

    SpringAOP 如何通過JoinPoint獲取參數(shù)名和值

    這篇文章主要介紹了SpringAOP 通過JoinPoint獲取參數(shù)名和值的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • Java面試題沖刺第三十天--數(shù)據(jù)庫(6)

    Java面試題沖刺第三十天--數(shù)據(jù)庫(6)

    這篇文章主要為大家分享了最有價值的三道關(guān)于數(shù)據(jù)庫的面試題,涵蓋內(nèi)容全面,包括數(shù)據(jù)結(jié)構(gòu)和算法相關(guān)的題目、經(jīng)典面試編程題等,感興趣的小伙伴們可以參考一下
    2021-09-09
  • SpringBoot使用Feign調(diào)用其他服務(wù)接口

    SpringBoot使用Feign調(diào)用其他服務(wù)接口

    這篇文章主要介紹了SpringBoot使用Feign調(diào)用其他服務(wù)接口,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03

最新評論

阿尔山市| 丽江市| 乐都县| 平果县| 子长县| 朝阳县| 屏东县| 紫阳县| 青铜峡市| 江达县| 且末县| 万山特区| 延寿县| 深水埗区| 察隅县| 阳谷县| 绥芬河市| 黑山县| 彭阳县| 隆安县| 沙坪坝区| 从江县| 武冈市| 肥西县| 汤原县| 温泉县| 金乡县| 康平县| 车致| 富宁县| 连平县| 塘沽区| 绥中县| 古蔺县| 黄大仙区| 波密县| 政和县| 手游| 嘉荫县| 昆明市| 尉犁县|