日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

SpringBoot默認(rèn)開啟AOP,采用Cglib代理方式?(Spring AOP快速入門)

作者:LuckyWangxs 更新時(shí)間: 2022-07-21 編程語言

1. Spring中的AOP需要手動(dòng)開啟

????????在Spring中,如果我們采用注解的方式進(jìn)行AOP,則需要手動(dòng)開啟SpringAOP支持,如下例子:
① 定義Spring的配置類,主要聲明需要掃描的包路徑,并且打開AOP功能

@Configuration
@ComponentScan("com.single")
@EnableAspectJAutoProxy
public class SpringConfig {


}

????????@EnableAspectJAutoProxy該注解即為打開AOP的注解,我們也可以通過該注解選擇動(dòng)態(tài)代理的方式,默認(rèn)情況下,Spring采用JDK的動(dòng)態(tài)代理,我們可以點(diǎn)進(jìn)該注解看下,請重點(diǎn)看下第一個(gè)屬性的注釋

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(AspectJAutoProxyRegistrar.class)
public @interface EnableAspectJAutoProxy {
	/**
	 * Indicate whether subclass-based (CGLIB) proxies are to be created as opposed
	 * to standard Java interface-based proxies. The default is {@code false}.
	 *  翻譯過來就是:指示是否創(chuàng)建基于子類(CGLIB)的代理,而不是標(biāo)準(zhǔn)的基于Java接口的代理。默認(rèn)值為{@code false}
	 *  說白了就是默認(rèn)是false,采用JDK,如果你想用Cglib,那你就設(shè)置為true
	 */
	boolean proxyTargetClass() default false;
	
	boolean exposeProxy() default false;
	
}

②自定義aop注解

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface AspectAnnotation {
}

③編寫切面類、切點(diǎn)、以及增強(qiáng)

@Component
@Aspect
public class AspectTest {
    @Pointcut("@annotation(com.single.annotation.AspectAnnotation)")
    public void pointCut() {}
    
    @Before("pointCut()")
    public void before(JoinPoint point) {
        System.out.println("前置增強(qiáng)");
    }
    
    @After("pointCut()")
    public void after() {
        System.out.println("后置增強(qiáng)");
    }
}

④在需要被增強(qiáng)的方法上添加自定義注解,方法所在bean一定要交給Spring管理

@Service
public class TestService {

    public TestService() {
        System.out.println("TestService is created...");
    }

    @AspectAnnotation
    public void test() {
        System.out.println("init......");
    }
}

⑤啟動(dòng)容器并調(diào)用方法

public class Application {

    public static void main(String[] args) {
        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
        TestService bean = ctx.getBean(TestService.class);
        bean.test();
    }

}

⑥結(jié)果
在這里插入圖片描述

2. SpringBoot中的AOP默認(rèn)開啟,并采用Cglib代理方式

????????在SpringBoot中,AOP的使用與上述沒有區(qū)別,只不過不需要我們手動(dòng)開啟AOP功能了,主要是因?yàn)?code>SpringBoot的自動(dòng)裝配,如果你讀過SpringBoot的源碼,相比你一定會(huì)知道在spring-boot-autoconfigureMETA-INF下有一個(gè)spring.factories文件,它里面指明了很多需要自動(dòng)裝配的配置類的路徑,在啟動(dòng)的時(shí)候會(huì)自動(dòng)將這些配置類中定義的bean裝配到IOC中,原理不多說了,感興趣的可以去研究一下。
????????我們來看一下這個(gè)文件
在這里插入圖片描述

????????可以看到自動(dòng)配置中有一個(gè)AOP的配置類,找到它,源碼如下:
在這里插入圖片描述
可以看到第53行和61行都用了@EnableAspectJAutoProxy注解,但是這個(gè)配置生效的前提條件是由@ConditionalOnProperty注解來控制的,此處還是涉及到了SpringBoot自動(dòng)裝配的原理了,因?yàn)樗亲詣?dòng)裝配,那么外部組件那么多,如果都要自動(dòng)裝配,那豈不是要加載很多無用的bean嗎,所以它肯定設(shè)定的bean被加載的條件,在META-INFO/spring-configuration-metadata.json中我們可以找到如下信息

    {
      "name": "spring.aop.proxy-target-class",
      "type": "java.lang.Boolean",
      "description": "Whether subclass-based (CGLIB) proxies are to be created (true), as opposed to standard Java interface-based proxies (false).",
      "defaultValue": true
    },

上述說明就是spring.aop.proxy-target-class的默認(rèn)值為true,結(jié)合@ConditionalOnProperty注解里的參數(shù),可以知道JDK的動(dòng)態(tài)代理配置不生效,因?yàn)樗募虞d條件是false,所以SpringBoot在啟動(dòng)的時(shí)候會(huì)自動(dòng)開啟AOP功能并采用Cglib方式進(jìn)行動(dòng)態(tài)代理

原文鏈接:https://blog.csdn.net/qq_41563912/article/details/125896748

欄目分類
最近更新