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

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í)表示,可以簡單理解為類中的方法,也就是可以被增強(qiáng)的方法

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

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

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

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

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

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

4.切面(Aspect)

切入點(diǎn)與通知的對(duì)應(yīng)關(guā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.測試

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

  • 深入淺出重構(gòu)Mybatis與Spring集成的SqlSessionFactoryBean(上)

    深入淺出重構(gòu)Mybatis與Spring集成的SqlSessionFactoryBean(上)

    通常來講,重構(gòu)是指不改變功能的情況下優(yōu)化代碼,但本文所說的重構(gòu)也包括了添加功能。這篇文章主要介紹了重構(gòu)Mybatis與Spring集成的SqlSessionFactoryBean(上)的相關(guān)資料,需要的朋友可以參考下
    2016-11-11
  • java中進(jìn)制的轉(zhuǎn)換,Byte與16進(jìn)制的轉(zhuǎn)換方法

    java中進(jìn)制的轉(zhuǎn)換,Byte與16進(jìn)制的轉(zhuǎn)換方法

    下面小編就為大家?guī)硪黄猨ava中進(jìn)制的轉(zhuǎn)換,Byte與16進(jìn)制的轉(zhuǎn)換方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2016-11-11
  • 如何獲取java類中的屬性注釋

    如何獲取java類中的屬性注釋

    在開發(fā)中,有時(shí)需要獲取Java類的屬性注釋,尤其是當(dāng)JPA生成的表缺少注釋時(shí),可以通過jdk自帶的tools.jar工具包來實(shí)現(xiàn),方法類似于生成javadoc文檔,需要在pom.xml文件中導(dǎo)入tools.jar的依賴,該jar文件一般位于JAVA_HOME/lib目錄下
    2024-09-09
  • java 查找字符串所在的位置代碼

    java 查找字符串所在的位置代碼

    這篇文章主要介紹了java 查找字符串所在的位置代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09
  • SpringCloud微服務(wù)多應(yīng)用腳手架的搭建與部署方式

    SpringCloud微服務(wù)多應(yīng)用腳手架的搭建與部署方式

    這篇文章主要介紹了SpringCloud微服務(wù)多應(yīng)用腳手架的搭建與部署方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • java異常處理攔截器詳情

    java異常處理攔截器詳情

    這篇文章主要介紹了java異常處理攔截器,使用異常處理攔截器,可以不用寫那么多try…catch…,下面就來學(xué)習(xí)關(guān)于java異常處理攔截器的詳情內(nèi)容吧,需要的朋友可以參考一下
    2021-10-10
  • springmvc 防止表單重復(fù)提交的兩種方法

    springmvc 防止表單重復(fù)提交的兩種方法

    最近在本地開發(fā)測試的時(shí)候,遇到一個(gè)表單重復(fù)提交的現(xiàn)象。本文主要介紹了springmvc 防止表單重復(fù)提交的兩種方法,感興趣的可以了解一下
    2021-08-08
  • 簡單了解springboot加載配置文件順序

    簡單了解springboot加載配置文件順序

    這篇文章主要介紹了簡單了解springboot加載配置文件順序,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-06-06
  • Java截取字符串的幾種常用方法

    Java截取字符串的幾種常用方法

    這篇文章主要給大家介紹了關(guān)于Java截取字符串的幾種常用方法,在Java編程語言中,String類提供了用于操作字符串的豐富方法,文中通過代碼示例介紹的非常詳細(xì),需要的朋友可以參考下
    2023-09-09
  • Java wait和notifyAll實(shí)現(xiàn)簡單的阻塞隊(duì)列

    Java wait和notifyAll實(shí)現(xiàn)簡單的阻塞隊(duì)列

    這篇文章主要介紹了Java wait和notifyAll實(shí)現(xiàn)簡單的阻塞隊(duì)列,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-10-10

最新評(píng)論