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

Spring框架學(xué)習(xí)之AOP詳解

 更新時(shí)間:2021年05月07日 09:00:29   作者:不善言談?wù)? 
這篇文章主要介紹了Spring框架學(xué)習(xí)之AOP詳解,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)Spring框架的小伙伴們有一定的幫助,需要的朋友可以參考下

一、概念

1.面向切面編程(方面),利用 AOP 可以對(duì)業(yè)務(wù)邏輯的各個(gè)部分進(jìn)行隔離,從而使得業(yè)務(wù)邏輯各部分之間的耦合度降低,提高程序的可重用性,同時(shí)提高了開發(fā)的效率。

2.通俗描述:不通過修改源代碼方式,在主干功能里面添加新功能

二、底層原理:動(dòng)態(tài)代理

有兩種情況動(dòng)態(tài)代理

2.1 有接口, JDK 動(dòng)態(tài)代理

1.被代理的對(duì)象

public class UserDaoImpl implements UserDao {

    @Override
    public int add(int a, int b) {
        System.out.println("add執(zhí)行");
        return a + b;
    }

    @Override
    public String update(String id) {
        System.out.println("update執(zhí)行");
        return id;
    }

}

2.代理

package cn.zj.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;

import cn.zj.dao.UserDao;
import cn.zj.dao.impl.UserDaoImpl;

public class JDKProxy {

    public static void main(String[] args) {
        // 創(chuàng)建接口實(shí)現(xiàn)類代理對(duì)象
        Class[] interfaces = { UserDao.class };
        UserDaoImpl userDao = new UserDaoImpl();
        UserDao dao = (UserDao) Proxy.newProxyInstance(JDKProxy.class.getClassLoader(), interfaces,
                new UserDaoProxy(userDao));
        
        int result = dao.add(1, 2);
        System. out .println( "result:"+result);
    }
}

//創(chuàng)建代理對(duì)象代碼
class UserDaoProxy implements InvocationHandler {

    private Object target;// 目標(biāo)類

    public UserDaoProxy(Object target) {
        this.target = target;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        // 方法執(zhí)行之前
        System.out.println("方法執(zhí)行之前.." + method.getName() + ";傳遞的參數(shù):" + Arrays.toString(args));
        // 增強(qiáng)方法
        Object res = method.invoke(target, args);
        // 方法執(zhí)行之后
        System.out.println("方法執(zhí)行之后.." + target);
        return res;
    }

}

3.打印

方法執(zhí)行之前..add;傳遞的參數(shù):[1, 2]
add執(zhí)行
方法執(zhí)行之后..cn.zj.dao.impl.UserDaoImpl@6c629d6e
result:3

2.2 無接口, CGLIB 動(dòng)態(tài)代理

1.被代理的對(duì)象

public class Cat {

    public void eat() {
        System.out.println("貓吃魚");
    }
}

2.代理

public class CglibProxy {

    public static void main(String[] args) {
        Cat c = new CglibProxy().createProxyObject(Cat.class);
        c.eat();
    }

    // JDK代理是對(duì)對(duì)象做代理,cglib代理是對(duì)類做代理
    public Cat createProxyObject(Class clazz) {
        // 1.創(chuàng)建內(nèi)存中的動(dòng)態(tài)類 Enhance
        // 內(nèi)存中造出一個(gè)沒有名稱的動(dòng)態(tài)類
        Enhancer enhancer = new Enhancer();
        // 2.現(xiàn)在的類最終完成原始類的功能,同時(shí)對(duì)其進(jìn)行功能的增強(qiáng),必須先具有原始類對(duì)應(yīng)的功能————繼承
        enhancer.setSuperclass(clazz);
        // 3.進(jìn)行功能的增強(qiáng)
        // 設(shè)置了方法的調(diào)用攔截
        // 設(shè)置具體的回調(diào)操作
        Callback callback = new MethodInterceptor() {
            // proxy:代理對(duì)象
            // method:被攔截的方法對(duì)象
            // args:調(diào)用參數(shù)
            // methodProxy:
            public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy)
                    throws Throwable {
                // 做增強(qiáng)
                System.out.println("執(zhí)行前");
                // Object obj=method.invoke(proxy, args);//這種執(zhí)行是代理類的方法,而不是目標(biāo)類的,會(huì)造成內(nèi)存溢出
                // 以下的兩種都是執(zhí)行目標(biāo)類的方法
                // Object obj = methodProxy.invokeSuper(proxy, args);//執(zhí)行目標(biāo)類的方法
                // Object obj=method.invoke(proxy, args);
                Object res = methodProxy.invokeSuper(proxy, args);
                System.out.println("執(zhí)行后");
                return res;
            }
        };
        enhancer.setCallback(callback);
        // 4.創(chuàng)建內(nèi)存中的全新的類的對(duì)象
        Object proxyObj = enhancer.create();
        return (Cat) proxyObj;
    }

}

三、術(shù)語

1.連接點(diǎn)(Joinpoint)

類中的任意方法的運(yùn)行時(shí)表示,可以簡(jiǎn)單理解為類中的方法,也就是可以被增強(qiáng)的方法

2.切入點(diǎn)(Pointcut)

具有共性功能的方法的運(yùn)行時(shí)表示,可以簡(jiǎn)單理解為具有共性功能的方法,也就是實(shí)際被增強(qiáng)的方法

注意:切入點(diǎn)對(duì)應(yīng)的是被挖去了共性功能后的方法執(zhí)行時(shí)匹配斷言(格式)

3.通知/增強(qiáng)(Advice)

共性功能模塊化,可以簡(jiǎn)單理解為將共性功能抽取出來制作成獨(dú)立的方法,實(shí)際增強(qiáng)的邏輯部分

類型:前置,后置,異常,最終,環(huán)繞

4.切面(Aspect)

切入點(diǎn)與通知的對(duì)應(yīng)關(guān)系,可以簡(jiǎn)單理解為被抽取的共性功能與共性功能被抽取位置對(duì)應(yīng)的方法之間的關(guān)系,把通知應(yīng)用到切入點(diǎn)的過程

5.目標(biāo)對(duì)象(Target Object)

包含切入點(diǎn)的運(yùn)行時(shí)對(duì)象,開發(fā)階段制作的是目標(biāo)對(duì)象對(duì)應(yīng)的類

6.AOP代理(AOP Proxy)

使用AOP的代理機(jī)制創(chuàng)建目標(biāo)對(duì)象運(yùn)行時(shí)代理對(duì)象,完成原始的功能

注意:原始目標(biāo)對(duì)象已經(jīng)被挖去了共性功能,此時(shí)使用目標(biāo)對(duì)象執(zhí)行任務(wù)無法完成原始任務(wù),使用AOP代理機(jī)制,創(chuàng)建一個(gè)代理對(duì)象來完成原始目標(biāo)對(duì)象的功能

7.織入(Weaving)

是一個(gè)將通知功能加入原始字節(jié)碼的動(dòng)態(tài)過程,將增強(qiáng)添加對(duì)目標(biāo)類具體連接點(diǎn)上的過程

Spring使用的是運(yùn)行時(shí)織入機(jī)制

8.引入(Introduction)

一種特殊的機(jī)制,可以為一個(gè)類的字節(jié)碼動(dòng)態(tài)的加入變量或方法

四、操作

4.1 Spring 框架一般都是基于 AspectJ 實(shí)現(xiàn) AOP

AspectJ 不是 Spring 組成部分,獨(dú)立 AOP 框架,一般把 AspectJ 和 Spirng 框架一起使

用,進(jìn)行 AOP 操作

4.2 實(shí)踐

 1.jar包引入

<dependencies>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>5.3.6</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>5.3.6</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.3.6</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-expression -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>5.3.6</version>
        </dependency>



        <!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>5.3.6</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.2</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.2.6</version>
        </dependency>

        <!-- @Resource注解需要 -->
        <dependency>
            <groupId>javax.annotation</groupId>
            <artifactId>javax.annotation-api</artifactId>
            <version>1.3.1</version>
        </dependency>


        <!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>5.3.6</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/net.sourceforge.cglib/com.springsource.net.sf.cglib -->
        <dependency>
            <groupId>net.sourceforge.cglib</groupId>
            <artifactId>com.springsource.net.sf.cglib</artifactId>
            <version>2.2.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.aopalliance/com.springsource.org.aopalliance -->
        <dependency>
            <groupId>org.aopalliance</groupId>
            <artifactId>com.springsource.org.aopalliance</artifactId>
            <version>1.0.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.aspectj/com.springsource.org.aspectj.weaver -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>com.springsource.org.aspectj.weaver</artifactId>
            <version>1.6.8.RELEASE</version>
        </dependency>



    </dependencies>

2.切入點(diǎn)表達(dá)式

作用:知道對(duì)哪個(gè)類里面的哪個(gè)方法進(jìn)行增強(qiáng)

語法結(jié)構(gòu): execution([權(quán)限修飾符] [返回類型] [類全路徑] 方法名稱 )

舉例 1:對(duì) com.zj.dao.BookDao 類里面的 add 進(jìn)行增強(qiáng)

execution(* com.zj.dao.BookDao.add(…))

舉例 2:對(duì) com.zj.dao.BookDao 類里面的所有的方法進(jìn)行增強(qiáng)

execution(* com.zj.dao.BookDao.* (…))

舉例 3:對(duì) com.zj.dao 包里面所有類,類里面所有方法進(jìn)行增強(qiáng)

execution(* com.zj.dao.. (…))

3.xml方式

1.創(chuàng)建類

package cn.zj.aop.xml;

public class Book {
    public void buy() {
        System.out.println("buy.............");
    }
}

2.增強(qiáng)類

package cn.zj.aop.xml;

public class BookProxy {
    public void before() {
        System.out.println("before.........");
    }
}

3.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">
    <!--創(chuàng)建對(duì)象-->
    <bean id="book" class="cn.zj.aop.xml.Book"></bean>
    <bean id="bookProxy" class="cn.zj.aop.xml.BookProxy"></bean>

    <!--配置aop增強(qiáng)-->
    <aop:config>
        <!--切入點(diǎn)-->
        <aop:pointcut id="p" expression="execution(* cn.zj.aop.xml.Book.buy(..))"/>
        <!--配置切面-->
        <aop:aspect ref="bookProxy">
            <!--增強(qiáng)作用在具體的方法上-->
            <aop:before method="before" pointcut-ref="p"/>
        </aop:aspect>
    </aop:config>
</beans>

4.注解方式

1.創(chuàng)建類

public class User {
    public void add() {
        System.out.println("add.......");
    }

}

2.創(chuàng)建增強(qiáng)類

package cn.zj.aop.an;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

//增強(qiáng)的類
@Component
@Aspect  //生成代理對(duì)象

public class UserProxy {

    //前置通知
    @Before(value = "execution(* cn.zj.aop.an.User.add(..))")
    public void before() {
        System.out.println("before.........");
    }

    //后置通知(返回通知)
    @AfterReturning(value = "execution(* cn.zj.aop.an.User.add(..))")
    public void afterReturning() {
        System.out.println("afterReturning.........");
    }

    //最終通知
    @After(value = "execution(* cn.zj.aop.an.User.add(..))")
    public void after() {
        System.out.println("after.........");
    }

    //異常通知
    @AfterThrowing(value = "execution(* cn.zj.aop.an.User.add(..))")
    public void afterThrowing() {
        System.out.println("afterThrowing.........");
    }

    //環(huán)繞通知
    @Around(value = "execution(* cn.zj.aop.an.User.add(..))")
    public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("環(huán)繞之前.........");

        //被增強(qiáng)的方法執(zhí)行
        proceedingJoinPoint.proceed();

        System.out.println("環(huán)繞之后.........");
    }
}

3.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="cn.zj.aop.an"></context:component-scan>

    <!-- 開啟Aspect生成代理對(duì)象-->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

4.測(cè)試

ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
        User user = context.getBean("user", User.class);
        user.add();

5.相同點(diǎn)抽取

//相同切入點(diǎn)抽取
    @Pointcut(value = "execution(* cn.zj.aop.an.User.add(..))")
    public void pointdemo() {

    }

    //前置通知
    //@Before注解表示作為前置通知
    @Before(value = "pointdemo()")
    public void before() {
        System.out.println("before.........");
    }

6.有多個(gè)增強(qiáng)類多同一個(gè)方法進(jìn)行增強(qiáng),設(shè)置增強(qiáng)類優(yōu)先級(jí)

在增強(qiáng)類上面添加注解 @Order(數(shù)字類型值),數(shù)字類型值越小優(yōu)先級(jí)越高

@Component
@Aspect 
@Order(1)
public class PersonProxy {

     //后置通知(返回通知)
    @Before(value = "execution(* cn.zj.aop.an.User.add(..))")
    public void afterReturning() {
        System.out.println("Person Before.........");
    }
}
//增強(qiáng)的類
@Component
@Aspect  //生成代理對(duì)象
@Order(2)
public class UserProxy {

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

相關(guān)文章

  • java N皇后實(shí)現(xiàn)問題解析

    java N皇后實(shí)現(xiàn)問題解析

    將 n 個(gè)皇后擺放在一個(gè) n x n 的棋盤上,使得每一個(gè)皇后都無法攻擊到其他皇后,N皇后問題是一個(gè)典型的約束求解問題,利用遞歸機(jī)制,可以很快的得到結(jié)果,本文將詳細(xì)介紹,需要了解的朋友可以參考下
    2012-11-11
  • java validation 后臺(tái)參數(shù)驗(yàn)證的使用詳解

    java validation 后臺(tái)參數(shù)驗(yàn)證的使用詳解

    本篇文章主要介紹了java validation 后臺(tái)參數(shù)驗(yàn)證的使用詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-10-10
  • SpringBoot視圖解析實(shí)現(xiàn)原理深入分析

    SpringBoot視圖解析實(shí)現(xiàn)原理深入分析

    視圖解析其實(shí)就是SpringBoot某一個(gè)controller的方法執(zhí)行完成之后,它是跳轉(zhuǎn)到那個(gè)頁面。由于我們springboot項(xiàng)目默認(rèn)打包為jar包,是形成壓縮包的形式,而jsp又不支持壓縮,所以我們SpringBoot不知JSP的,需要引入第三方模板引擎才可以處理
    2022-10-10
  • Spring實(shí)戰(zhàn)之容器中的工程Bean用法示例

    Spring實(shí)戰(zhàn)之容器中的工程Bean用法示例

    這篇文章主要介紹了Spring實(shí)戰(zhàn)之容器中的工程Bean用法,結(jié)合實(shí)例形式分析了Sring框架容器中的工程Bean相關(guān)配置、使用操作技巧,需要的朋友可以參考下
    2019-11-11
  • SpringBoot實(shí)現(xiàn)的Mongodb管理工具使用解析

    SpringBoot實(shí)現(xiàn)的Mongodb管理工具使用解析

    這篇文章主要介紹了SpringBoot實(shí)現(xiàn)的Mongodb管理工具使用解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-09-09
  • 關(guān)于在Java中使用預(yù)定義類

    關(guān)于在Java中使用預(yù)定義類

    這篇文章主要介紹了關(guān)于在Java中使用預(yù)定義類,預(yù)定義類就是Java類庫(或第三方庫)中已經(jīng)定義好的類,例如,Math 類和 Date 類,需要的朋友可以參考下
    2023-05-05
  • SpringBoot中讀取配置的七種方法總結(jié)

    SpringBoot中讀取配置的七種方法總結(jié)

    在Spring Boot應(yīng)用中,讀取配置是一項(xiàng)基礎(chǔ)且重要的任務(wù),Spring Boot提供了多種靈活的方式來讀取配置,以滿足不同場(chǎng)景下的需求,本文將詳細(xì)介紹Spring Boot中讀取配置的幾種常用方法,需要的朋友可以參考下
    2024-08-08
  • SpringBoot Starter機(jī)制及整合tomcat的實(shí)現(xiàn)詳解

    SpringBoot Starter機(jī)制及整合tomcat的實(shí)現(xiàn)詳解

    這篇文章主要介紹了SpringBoot Starter機(jī)制及整合tomcat的實(shí)現(xiàn),我們知道SpringBoot自己在“后臺(tái)”幫我們配置了很多原本需要我們手動(dòng)去的東西,至于這個(gè)“后臺(tái)”是啥,就是Starter機(jī)制
    2022-09-09
  • 什么是Spring Boot

    什么是Spring Boot

    Spring是一個(gè)非常受歡迎的Java框架,它用于構(gòu)建web和企業(yè)應(yīng)用。本文介紹將各種Spring的配置方式,幫助您了解配置Spring應(yīng)用的復(fù)雜性
    2017-08-08
  • JMagick實(shí)現(xiàn)基本圖像處理的類實(shí)例

    JMagick實(shí)現(xiàn)基本圖像處理的類實(shí)例

    這篇文章主要介紹了JMagick實(shí)現(xiàn)基本圖像處理的類,實(shí)例分析了java圖像處理的相關(guān)技巧,需要的朋友可以參考下
    2015-06-06

最新評(píng)論

永济市| 莒南县| 汝州市| 江油市| 阳朔县| 特克斯县| 聂拉木县| 会昌县| 麻江县| 津南区| 扶余县| 荥阳市| 河池市| 竹北市| 清水河县| 信丰县| 淳安县| 济阳县| 宾川县| 西充县| 古蔺县| 上高县| 磐石市| 游戏| 西乡县| 德格县| 丰城市| 锡林郭勒盟| 格尔木市| 大荔县| 墨脱县| 鄂州市| 绥棱县| 清河县| 合川市| 嵊泗县| 五大连池市| 阜新市| 岳普湖县| 鹤庆县| 响水县|