1. Spring中使用AOP

1.1 引入Aspect

1
2
3
4
5
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>

1.2 创建用于拦截的bean

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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);
}
}

1.3 创建Advisor

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
@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");
}
}

1.4 创建配置文件

要在Spring中开启AOP功能,还需要在配置文件中作如下声明,开启AOP:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?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>

1.5 注解开启AOP

开启AOPaop:aspectj-autoproxy/也可以使用注解的方式,如下,使用@EnableAspectJAutoProxy配置在任何一个@Configratrion或者@Component上

img

2. SpringBoot集成AOP

2.1 添加pom依赖

1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>

引入了AOP的场景启动器,我们点击去看看

img

还是引入了spring-aop和aspectj的依赖,和我们Spring集成AOP是引入了相同的包,接着我们直接就可以创建Advisor了,如上AspectJTest这个类,但是我们并没有通过**@EnableAspectJAutoProxy开启AOP呢?那是因为AOP的自动配置类帮我们开启了**

2.2 AopAutoConfiguration

一旦导入了spring-boot-starter-aop依赖后,SpringBoot就会启动AOP的自动配置类AopAutoConfiguration:

img

我们来看看AopAutoConfiguration这个自动配置类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
@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/是一样的


本站由 卡卡龙 使用 Stellar 1.27.0 主题创建

本站访问量 次. 本文阅读量 次.