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

Spring框架AOP面向切面編程原理全面分析

 更新時(shí)間:2021年09月15日 14:52:10   作者:DrLai  
這篇文章主要介紹了Spring框架AOP面向切面編程的全面分析,文中附含詳細(xì)的示例代碼分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助

1.什么是AOP

AOP:Aspect Oriented Programming ⾯向切⾯編程。

AOP面向切面的優(yōu)勢(shì)

  • 降低模塊之間的耦合度。
  • 使系統(tǒng)更容易擴(kuò)展。 更好的代碼復(fù)⽤。
  • ⾮業(yè)務(wù)代碼更加集中,不分散,便于統(tǒng)⼀管理。
  • 業(yè)務(wù)代碼更加簡(jiǎn)潔存粹,不參雜其他代碼的影響。

AOP 是對(duì)⾯向?qū)ο缶幊痰?#12032;個(gè)補(bǔ)充,在運(yùn)⾏時(shí),動(dòng)態(tài)地將代碼切⼊到類的指定⽅法、指定位置上的編程 思想就是⾯向切⾯編程。將不同⽅法的同⼀個(gè)位置抽象成⼀個(gè)切⾯對(duì)象,對(duì)該切⾯對(duì)象進(jìn)⾏編程就是 AOP。

AOP需要添加的依賴

<dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</artifactId>
      <version>5.3.9</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aspects</artifactId>
      <version>5.3.9</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.3.9</version>
    </dependency>

2.簡(jiǎn)述AOP工作運(yùn)行原理

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;
public class MyInvocationHandler implements InvocationHandler {
    private Object object=null;
    public Object bind(Object object){
        this.object=object;
        return Proxy.newProxyInstance(object.getClass().getClassLoader(),object.getClass().getInterfaces(),this);
    }
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println(method.getName()+"方法的參數(shù)"+ Arrays.toString(args));
        Object result=method.invoke(this.object,args);
        System.out.println(method.getName()+"的結(jié)果是"+result);
        return result;
    }
}

以上是動(dòng)態(tài)創(chuàng)建AOP的方法,首先通過(guò)bind返回

Proxy.newProxyInstance(object.getClass().getClassLoader(),object.getClass.getInterfaces(),this)返回代理對(duì)象

接著運(yùn)用反射的invoke方法,實(shí)現(xiàn)面向切面編程

用method.getName()獲取方法名

用arg獲取屬性

動(dòng)態(tài)創(chuàng)建的總結(jié):

以上是通過(guò)動(dòng)態(tài)代理實(shí)現(xiàn) AOP 的過(guò)程,⽐較復(fù)雜,不好理解,Spring 框架對(duì) AOP 進(jìn)⾏了封裝,使⽤ Spring 框架可以⽤⾯向?qū)ο蟮乃枷雭?lái)實(shí)現(xiàn) AOP

3.使用Spring創(chuàng)建AOP

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
import java.util.Arrays;
@Aspect
@Component
public class LoggerAspect {
    //@before表適方法執(zhí)行的具體位置和時(shí)機(jī)
    @Before("execution(public int Util.impl.Calimpl.*(..))")
    public void before(JoinPoint joinPoint){
        //獲取方法名
        String name=joinPoint.getSignature().getName();
        //獲取參數(shù)
        String args= Arrays.toString(joinPoint.getArgs());
        System.out.println(name+"方法的參數(shù)是"+args);
    }
    @After("execution(public int Util.impl.Calimpl.*(..))")
    public  void after(JoinPoint joinPoint){
        String name=joinPoint.getSignature().getName();
        System.out.println(name+"執(zhí)行完畢");
    }
    @AfterReturning(value = "execution(public int Util.impl.Calimpl.*(..))",returning = "result")
    public void returning(JoinPoint joinPoint,Object result){
        System.out.println("結(jié)果是:"+result);
    }
}

注解@Aspect指的是面向切面編程

@Component指的是交給Ioc容器管理

通過(guò)Joinpoint.getSignature().getname()獲取方法名

然后實(shí)現(xiàn)在調(diào)用這個(gè)方法前,進(jìn)行運(yùn)行所需要的日志信息

測(cè)試類

import Util.Cal;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test2 {
    public static void main(String[] args) {
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring-aop.xml");
        Cal proxy = (Cal) applicationContext.getBean("calimpl");
        proxy.add(1,1);
    }
}

還是通過(guò)Application 讀取Spring.xml 再將getBean 默認(rèn)小寫名字,再調(diào)用方法即可

Spring.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"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
 http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
 http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
">
    <!-- ⾃動(dòng)掃描 -->
    <context:component-scan base-package="Util">
    </context:component-scan>
    <!-- 是Aspect注解⽣效,為⽬標(biāo)類⾃動(dòng)⽣成代理對(duì)象 -->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

以上就是Spring框架AOP面向切面編程全面分析的詳細(xì)內(nèi)容,更多關(guān)于Spring框架AOP面向切面的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論

濮阳市| 潞城市| 清徐县| 台安县| 汉寿县| 镇平县| 四平市| 潮安县| 钟祥市| 金华市| 宜州市| 崇文区| 乌兰浩特市| 萍乡市| 齐河县| 南丹县| 社旗县| 农安县| 民丰县| 澳门| 团风县| 弥勒县| 会宁县| 沙坪坝区| 霍城县| 班玛县| 岗巴县| 理塘县| 额济纳旗| 贡觉县| 大英县| 名山县| 韶山市| 临城县| 塘沽区| 石泉县| 襄垣县| 石首市| 龙江县| 九寨沟县| 互助|