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

詳解Java Spring AOP

 更新時(shí)間:2022年01月04日 17:00:07   作者:呆萌小新@淵潔  
這篇文章主要為大家介紹了Java Spring AOP,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助

前言

面向切面編程,利用 AOP 可以對業(yè)務(wù)邏輯的各個(gè)部分進(jìn)行隔離,從而使得業(yè)務(wù)邏輯各部分之間的耦合度降低,提高程序的可重用性,同時(shí)提高了開發(fā)的效率。
即不改變源代碼而添加新功能,可插拔的.

提示:以下是本篇文章正文內(nèi)容,下面案例可供參考

一.AOP底層原理

1.AOP底層使用動態(tài)代理

有接口:jdk動態(tài)代理,即創(chuàng)建接口實(shí)現(xiàn)類代理對象

無接口:CGLIB動態(tài)代理,即創(chuàng)建子類代理對象

jdk動態(tài)代理的實(shí)現(xiàn)

在這里插入圖片描述

創(chuàng)建接口

package com.vector.spring5;

public interface UserDao {
    public int add(int a,int b);
    public String update(String id);
}

接口實(shí)現(xiàn)類

接口實(shí)現(xiàn)類的方法,屬于源代碼,用aop思想增添新功能時(shí)這里不能動!

package com.vector.spring5;

public class UserDaoImpl implements UserDao{

    @Override
    public int add(int a, int b) {
        return a+b;
    }

    @Override
    public String update(String id) {
        return id;
    }
}

使用JDK動態(tài)代理對象,增添新功能

package com.vector.spring5;

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

public class JDKProxy {
    public static void main(String[] args) {
        //創(chuàng)建接口實(shí)現(xiàn)類代理對象
        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)建代理對象
class UserDaoProxy implements InvocationHandler{
    //有參構(gòu)造傳遞增強(qiáng)對象
    private Object obj;
    public UserDaoProxy(){};
    public UserDaoProxy(Object obj){
        this.obj=obj;
    }
    //增強(qiáng)的邏輯
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        //方法之前
        System.out.println("方法之前執(zhí)行: "+method.getName()+":傳遞的參數(shù): "+ Arrays.toString(args));
        //被增強(qiáng)的方法執(zhí)行
        //可以根據(jù)method.getName()判斷選擇增強(qiáng)
        Object res = method.invoke(obj,args);
        //方法之后
        System.out.println("方法之后執(zhí)行: "+obj);
        return res;
    }
}

在這里插入圖片描述

jdk代理圖像解析

在這里插入圖片描述

在這里插入圖片描述

二.AOP術(shù)語

1.連接點(diǎn)

類里可以被增強(qiáng)的方法,稱為連接點(diǎn).

2.切入點(diǎn)

類中實(shí)際被增強(qiáng)的方法,成為切入點(diǎn).

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

(1)實(shí)際被增強(qiáng)的方法中的邏輯部分稱為通知(增強(qiáng)).

(2)通知包含:前置通知,后置通知,環(huán)繞通知,異常通知,最終通知

4.切面

把增強(qiáng)應(yīng)用到切入點(diǎn)的過程稱為切面

三.AOP 操作(準(zhǔn)備工作)

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

(1)AspectJ 不是 Spring 組成部分,獨(dú)立 AOP 框架,一般把 AspectJ 和 Spirng 框架一起使用,進(jìn)行 AOP 操作

maven準(zhǔn)備

<dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.8.RC1</version>
        </dependency>

方式一:使用Spring的接口實(shí)現(xiàn)增添功能

實(shí)現(xiàn)組合crud和日志功能結(jié)合

applicationContext.xml

    <context:component-scan base-package="com.vector"/>
    <aop:config>
<!--        切入點(diǎn): expression:表達(dá)式 execution(要執(zhí)行的位置!* * * * *)-->
        <aop:pointcut id="pointcut" expression="execution(* com.vector.service.UserServiceImpl.*(..))"/>

<!--        執(zhí)行環(huán)繞增加!-->
        <aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
        <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
    </aop:config>

log.java

package com.vector.log;

import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

@Component("log")
public class Log implements MethodBeforeAdvice {
    //method: 要執(zhí)行的目標(biāo)對象的方法
    //args: 參數(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í)行了");

    }
}

userService.java

package com.vector.service;


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

userServiceImpl.java

package com.vector.service;

import org.springframework.stereotype.Service;

@Service("userService")
public class UserServiceImpl implements UserService{
    @Override
    public void add() {
        System.out.println("增加了一個(gè)用戶");
    }
}

MyTest.java

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

在這里插入圖片描述

方式二:自定義類

DiyPoint.java

package com.vector.diy;

import org.springframework.stereotype.Component;

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

UserServiceImpl.java

package com.vector.service;

import org.springframework.stereotype.Service;

@Service("userService")
public class UserServiceImpl implements UserService{
    @Override
    public void add() {
        System.out.println("增加了一個(gè)用戶");
    }
}

applicationContext.xml

    <aop:config>
<!--        自定義切面,ref要引用的類-->
        <aop:aspect ref="diyPointCut">
<!--            切入點(diǎn)-->
            <aop:pointcut id="pointcut" expression="execution(* com.vector.service.UserServiceImpl.*(..))"/>
<!--            通知-->
            <aop:before method="before" pointcut-ref="pointcut"/>
            <aop:after method="after" pointcut-ref="pointcut"/>
        </aop:aspect>
    </aop:config>

MyTest.java

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

在這里插入圖片描述

方式三:全注解配置實(shí)現(xiàn)

UserServiceImpl.java

package com.vector.service;

import org.springframework.stereotype.Service;

@Service("userService")
public class UserServiceImpl implements UserService{
    @Override
    public void add() {
        System.out.println("增加了一個(gè)用戶");
    }
}

AnnotationPointCut.java

package com.vector;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;

//標(biāo)注這個(gè)類是一個(gè)切面
@Aspect
@Component("annotationPointCut")
//開啟aop注解驅(qū)動
@EnableAspectJAutoProxy
public class AnnotationPointCut {
    @Before("execution(* com.vector.service.UserServiceImpl.*(..))")
    public void before(){
        System.out.println("===方法執(zhí)行前===");
    }
    @After("execution(* com.vector.service.UserServiceImpl.*(..))")
    public void after(){
        System.out.println("===方法執(zhí)行后===");
    }
}

MyTest.java

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

在這里插入圖片描述

總結(jié)

本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!

相關(guān)文章

  • java后端PayPal支付實(shí)現(xiàn)教程

    java后端PayPal支付實(shí)現(xiàn)教程

    本文主要介紹了java后端PayPal支付實(shí)現(xiàn)教程,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-12-12
  • Java窗體居中顯示的2種方法(實(shí)例講解)

    Java窗體居中顯示的2種方法(實(shí)例講解)

    下面小編就為大家?guī)硪黄狫ava窗體居中顯示的2種方法(實(shí)例講解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-07-07
  • SpringBoot使用Kaptcha實(shí)現(xiàn)驗(yàn)證碼的生成與驗(yàn)證功能

    SpringBoot使用Kaptcha實(shí)現(xiàn)驗(yàn)證碼的生成與驗(yàn)證功能

    這篇文章主要介紹了SpringBoot使用Kaptcha實(shí)現(xiàn)驗(yàn)證碼的生成與驗(yàn)證功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-03-03
  • 理解java中的深復(fù)制和淺復(fù)制

    理解java中的深復(fù)制和淺復(fù)制

    這篇文章主要幫助大家理解java中的深復(fù)制和淺復(fù)制,對java中的深復(fù)制和淺復(fù)制進(jìn)行剖析,感興趣的小伙伴們可以參考一下
    2016-02-02
  • JVM:晚期(運(yùn)行期)優(yōu)化的深入理解

    JVM:晚期(運(yùn)行期)優(yōu)化的深入理解

    今天小編就為大家分享一篇關(guān)于JVM:晚期(運(yùn)行期)優(yōu)化的深入理解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2019-02-02
  • Java8 中使用Stream 讓List 轉(zhuǎn) Map使用問題小結(jié)

    Java8 中使用Stream 讓List 轉(zhuǎn) Map使用問題小結(jié)

    這篇文章主要介紹了Java8 中使用Stream 讓List 轉(zhuǎn) Map使用總結(jié),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-06-06
  • Java 自定義Spring框架以及Spring框架的基本使用

    Java 自定義Spring框架以及Spring框架的基本使用

    Spring框架是由于軟件開發(fā)的復(fù)雜性而創(chuàng)建的。Spring使用的是基本的JavaBean來完成以前只可能由EJB完成的事情。然而,Spring的用途不僅僅限于服務(wù)器端的開發(fā)
    2021-10-10
  • MyBatis中association的基本使用方法

    MyBatis中association的基本使用方法

    在項(xiàng)目中某些實(shí)體類之間肯定有關(guān)鍵關(guān)系,比如一對一,一對多等,在hibernate中用one to one和one to many,而mybatis中就用association和collection,下面這篇文章主要給大家介紹了關(guān)于MyBatis中association基本使用方法的相關(guān)資料,需要的朋友可以參考下
    2022-09-09
  • 深入探討Java多線程中的volatile變量

    深入探討Java多線程中的volatile變量

    這篇文章主要為大家詳細(xì)并深入的探討Java多線程中的volatile變量,volatile用來確保將變量的更新操作通知到其他線程,保證了新值能立即同步到主內(nèi)存,以及每次使用前立即從主內(nèi)存刷新,感興趣的小伙伴們可以參考一下
    2016-02-02
  • 你真的知道Java中對象的銷毀嗎

    你真的知道Java中對象的銷毀嗎

    java自帶垃圾回收機(jī)制,會自動識別內(nèi)存中不再會被使用的對象并將其銷毀,釋放內(nèi)存,下面這篇文章主要介紹了Java中對象銷毀的相關(guān)資料,需要的朋友可以參考下
    2021-10-10

最新評論