本篇主要集成Sping一個重要功能AOP
我們還是先回顧一下以前Spring中是如何使用AOP的,大家可以看看我這篇文章spring5 源碼深度解析----- AOP的使用及AOP自定義標簽
Spring中使用AOP
引入Aspect
<dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>${aspectj.version}</version> </dependency>
創建用於攔截的bean
public class TestBean { private String message = "test bean"; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public void test(){ System.out.println(this.message); } }
創建Advisor
@Aspect public class AspectJTest { @Pointcut("execution(* *.test(..))") public void test(){ } @Before("test()") public void beforeTest(){ System.out.println("beforeTest"); } @Around("test()") public Object aroundTest(ProceedingJoinPoint p){ System.out.println("around.....before"); Object o = null; try{ o = p.proceed(); }catch(Throwable e){ e.printStackTrace(); } System.out.println("around.....after"); return o; } @After("test()") public void afterTest() { System.out.println("afterTest"); } }
創建配置文件
要在Spring中開啟AOP功能,還需要在配置文件中作如下聲明,開啟AOP:
<?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"> <aop:aspectj-autoproxy/> <bean id="test" class="com.yhl.myspring.demo.aop.TestBean"> <property name="message" value="這是一個苦逼的程序員"/> </bean> <bean id="aspect" class="com.yhl.myspring.demo.aop.AspectJTest"/> </beans>
注解開啟AOP
開啟AOP<aop:aspectj-autoproxy/>也可以使用注解的方式,如下,使用@EnableAspectJAutoProxy配置在任何一個@Configratrion或者@Component上
SpringBoot集成AOP
添加pom依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency>
引入了AOP的場景啟動器,我們點擊去看看
還是引入了spring-aop和aspectj的依賴,和我們Spring集成AOP是引入了相同的包,接着我們直接就可以創建Advisor了,如上AspectJTest這個類,但是我們並沒有通過@EnableAspectJAutoProxy開啟AOP呢?那是因為AOP的自動配置類幫我們開啟了
AopAutoConfiguration
一旦導入了spring-boot-starter-aop依賴后,SpringBoot就會啟動AOP的自動配置類AopAutoConfiguration:
我們來看看AopAutoConfiguration這個自動配置類
@Configuration @ConditionalOnClass({ EnableAspectJAutoProxy.class, Aspect.class, Advice.class }) @ConditionalOnProperty(prefix = "spring.aop", name = "auto", havingValue = "true", matchIfMissing = true) public class AopAutoConfiguration { @Configuration //使用注解開啟AOP功能 @EnableAspectJAutoProxy(proxyTargetClass = false) @ConditionalOnProperty(prefix = "spring.aop", name = "proxy-target-class", havingValue = "false", matchIfMissing = true) public static class JdkDynamicAutoProxyConfiguration { } @Configuration //使用注解開啟AOP功能 @EnableAspectJAutoProxy(proxyTargetClass = true) @ConditionalOnProperty(prefix = "spring.aop", name = "proxy-target-class", havingValue = "true", matchIfMissing = false) public static class CglibAutoProxyConfiguration { } }
不管使用jdk代理還是cglib代理,都有@EnableAspectJAutoProxy注解,所以只要導入spring-boot-starter-aop依賴后,就自動幫我們開啟了AOP,我們可以直接添加切面使用AOP了
@EnableAspectJAutoProxy這個注解是整個AOP的靈魂,其作用和<aop:aspectj-autoproxy/>是一樣的,大家可以看看其源碼分析spring5 源碼深度解析----- AOP的使用及AOP自定義標簽