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

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

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

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

  <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">
<!--    注冊(cè)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>
<!--        切入點(diǎn) 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)對(duì)象的方法
    //objects:參數(shù)
    //target:目標(biāo)對(duì)象
    @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)對(duì)象的方法
    //objects:參數(shù)
    //target:目標(biāo)對(duì)象
    //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動(dòng)態(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");
        //動(dòng)態(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">
<!--    注冊(cè)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">
<!--            切入點(diǎn)-->
            <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í)行后````");
    }
}

其他的不變

方式三:使用注解實(shí)現(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">
<!--    注冊(cè)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;
//使用注解方式實(shí)現(xiàn)AOP
@Aspect
//標(biāo)注這個(gè)類是一個(gè)切面
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ǔ)充知識(shí):execution表達(dá)式

execution表達(dá)式的詳解

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

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

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

2、第一個(gè)*號(hào):方法返回類型, *號(hào)表示所有的類型。

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

4、第二個(gè)*號(hào):表示類名,*號(hào)表示所有的類。

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

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

舉例:

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

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

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

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

相關(guān)文章

  • springboot ehcache 配置使用方法代碼詳解

    springboot ehcache 配置使用方法代碼詳解

    EhCache是一個(gè)比較成熟的Java緩存框架,Springboot對(duì)ehcache的使用非常支持,所以在Springboot中只需做些配置就可使用,且使用方式也簡易,今天給大家分享springboot ehcache 配置使用教程,一起看看吧
    2021-06-06
  • IDEA2020.1創(chuàng)建springboot項(xiàng)目(國內(nèi)腳手架)安裝lombok

    IDEA2020.1創(chuàng)建springboot項(xiàng)目(國內(nèi)腳手架)安裝lombok

    這篇文章主要介紹了IDEA2020.1創(chuàng)建springboot項(xiàng)目(國內(nèi)腳手架)安裝lombok,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • 詳解Java中的時(shí)區(qū)類TimeZone的用法

    詳解Java中的時(shí)區(qū)類TimeZone的用法

    TimeZone可以用來獲取或者規(guī)定時(shí)區(qū),也可以用來計(jì)算時(shí)差,這里我們就來詳解Java中的時(shí)區(qū)類TimeZone的用法,特別要注意下面所提到的TimeZone相關(guān)的時(shí)間校準(zhǔn)問題.
    2016-06-06
  • SpringCloud重試機(jī)制配置詳解

    SpringCloud重試機(jī)制配置詳解

    本篇文章主要介紹了SpringCloud重試機(jī)制配置詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-04-04
  • Java編程構(gòu)造方法與對(duì)象的創(chuàng)建詳解

    Java編程構(gòu)造方法與對(duì)象的創(chuàng)建詳解

    這篇文章主要介紹了Java編程構(gòu)造方法與對(duì)象的創(chuàng)建詳解,具有一定參考價(jià)值,需要的朋友可以了解下。
    2017-11-11
  • eclipse的web項(xiàng)目實(shí)現(xiàn)Javaweb購物車的方法

    eclipse的web項(xiàng)目實(shí)現(xiàn)Javaweb購物車的方法

    這篇文章主要介紹了eclipse的web項(xiàng)目實(shí)現(xiàn)Javaweb購物車的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-10-10
  • Java使用ArrayList實(shí)現(xiàn)撲克牌的示例代碼

    Java使用ArrayList實(shí)現(xiàn)撲克牌的示例代碼

    學(xué)習(xí)了關(guān)于集合類的知識(shí),我們可以做一個(gè)小項(xiàng)目來加深對(duì)集合類知識(shí)的學(xué)習(xí)!本文就來利用ArrayList實(shí)現(xiàn)撲克牌發(fā)牌洗牌效果,需要的可以參考一下
    2022-10-10
  • java線程之用Thread類創(chuàng)建線程的方法

    java線程之用Thread類創(chuàng)建線程的方法

    本篇文章介紹了,Thread類創(chuàng)建線程的方法。需要的朋友參考下
    2013-05-05
  • Spring Cache的基本使用與實(shí)現(xiàn)原理詳解

    Spring Cache的基本使用與實(shí)現(xiàn)原理詳解

    緩存是實(shí)際工作中非經(jīng)常常使用的一種提高性能的方法, 我們會(huì)在很多場景下來使用緩存。下面這篇文章主要給大家介紹了關(guān)于Spring Cache的基本使用與實(shí)現(xiàn)原理的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2018-05-05
  • Java隊(duì)列同步器之CountDownLatch實(shí)現(xiàn)詳解

    Java隊(duì)列同步器之CountDownLatch實(shí)現(xiàn)詳解

    這篇文章主要介紹了Java隊(duì)列同步器之CountDownLatch實(shí)現(xiàn)詳解,CountDownLatch是一個(gè)同步工具類,它允許一個(gè)或多個(gè)線程一直等待,直到其他線程執(zhí)行完后再執(zhí)行,例如,應(yīng)用程序的主線程希望在負(fù)責(zé)啟動(dòng)框架服務(wù)的線程已經(jīng)啟動(dòng)所有框架服務(wù)之后執(zhí)行,需要的朋友可以參考下
    2023-12-12

最新評(píng)論