Spring
面向切面编程
什么是 AOP
AOP 为 Aspect Oriented Programming 的缩写,意思为面向切面编程,是通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。
AOP 是 OOP 的补充,是软件开发中的一个重要的技术,同时也是Spring框架中的核心内容。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的 耦合度降低 ,提高程序的可重用性,同时提高了开发的效率。
AOP
的作用及其优势
- 作用:在程序运行期间,在不修改源码的情况下对方法进行功能增强。
- 优势:减少重复代码,提高开发效率,并且便于维护。
AOP
的底层实现
AOP
的底层是通过Spring提供的动态代理技术实现的。运行期间Spring会通过动态代理技术动态生成代理对象,代理对象方法执行时会在合适的时机进行增强功能的介入,再调用目标对象的方法,实现功能的增强。
AOP
动态代理技术
常用的动态代理技术
JDK
代理:基于接口的动态代理技术
CGLB
代理:基于父类的动态代理技术
动态代理的扩展
动态代理都是通过继承父类、实现接口,并进行方法重写完成的。
继承的子类或实现接口的实现类都由工具创建,而不是我们自己实现的。
Spring AOP
**Spring AOP**实现底层就是对动态代理进行了封装,封装后我们只需要对我们需要关注的部分进行代码编写,并通过配置的方式完成制定目标的方法功能的增强。
术语 | 解释(术语并非属于Spring ) |
---|---|
Target | 目标对象 |
Proxy | 代理对象 |
JoinPoint | 连接点 指目标对象中的方法 |
PointCut | 切入点 指对哪些连接点进行拦截的定义 |
Advice | 通知/建议 值拦截指定方法后执行的操作 |
Aspect | 切面 由切点和通知组成 |
weaving | 织入 将切点与建议结合的过程 |
切点表达式
用来匹配需要代理的方法,以便于拦截代理
表达式语法:execution( [修饰符] 返回值类型 包名.类名.方法名( 参数类型列表 ) )
- 访问修饰符可以省略
- 返回值类型、包名、类名、方法名可以使用星号
*
代表任意 - 包名与类名之间一个点
.
代表当前包下的类,两个点..
表示当前包及其子包下的类 - 参数类型列表:多个用逗号隔开,可以使用两个点
..
表示任意个数和类型的参数列表
示例 | 说明 |
---|---|
execution( public * *(..) ) | 执行任何公共方法 |
execution(* set*(..)) | 名称以set开头的任何方法的执行 |
execution(* com.xuetang9.service.UserService.*(..)) | 执行UserService接口定义的任何方法 |
execution(* com.xuetang9.service.* .*(..)) | 执行service包中定义的任何方法 |
execution(* com.xuetang9.service.. *. *(..)) | 执行service包或其子包中定义的任何方法 |
通知的类型
名称 | 标签 | 注解 | 说明 |
---|---|---|---|
前置建议 | <aop:before> | @Before | 运行在连接点之前,但不能阻止连接点运行 |
后置运行建议 | <aop:after-returning> | @AfterReturning | 运行在连接点之后,正常结束不抛出异常 |
后置异常建议 | <aop:after-throwing> | @AfterThrowing | 运行在连接点出现异常之后 |
后置建议 | <aop:after> | @After | 不管连接点是否出现异常都会运行 |
前置建议 | <aop:around> | @Around | 环绕在一个连接点运行,可处理上面的所有操作 |
纯配置文件实现方式
导入aspectjweaver
的坐标
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.6</version>
</dependency>
书写增强类
@Component
@Aspect
public class AdviceAop {
public void methodBefore(){
System.out.println("执行之前...");
}
public void methodAfterReturning(){
System.out.println("方法执行返回结果之后...");
}
public void methodAfterThrowing(){
System.out.println("方法执行出现异常之后...");
}
public void methodAfterFinally(){
System.out.println("方法执行之后...");
}
public void methodAround(){
System.out.println("环绕增强...");
}
}
在XML中手动配置AOP的代理
<!--配置功能增强通知类-->
<bean id="adviceAop" class="demo.aop.AdviceAop"></bean>
<!--配置AOP-->
<aop:config>
<!--配置一个具体的切面织入-->
<aop:aspect ref="adviceAop">
<!--配置切点-->
<aop:pointcut id="methodPointcut" expression="execution(* com.xuetang9.spring.demo..*.*(..))"/>
<!--配置切点通知-->
<aop:before method="methodBefore" pointcut-ref="methodPointcut"/>
<aop:after-returning method="methodAfterReturning" pointcut-ref="methodPointcut"/>
<aop:after-throwing method="methodAfterThrowing" pointcut-ref="methodPointcut"/>
<aop:after method="methodAfterFinally" pointcut-ref="methodPointcut"/>
<aop:around method="methodAround" pointcut-ref="methodPointcut"/>
</aop:aspect>
</aop:config>
基于注解+配置方式的实现
导入坐标同上
用注解定义增强类
public class AdviceAop {
/**
* 定义通用的切点
*/
@Pointcut("execution(* demo.dao..*.*(..))")
public void joinPoint() {}
@Before("joinPoint()")
public void methodBefore() {
System.out.println("执行之前...");
}
@AfterReturning("joinPoint()")
public void methodAfterReturning() {
System.out.println("方法执行返回结果之后...");
}
@AfterThrowing("joinPoint()")
public void methodAfterThrowing() {
System.out.println("方法执行出现异常之后...");
}
@After("joinPoint()")
public void methodAfterFinally() {
System.out.println("方法执行之后...");
}
@Around("joinPoint()")
public void methodAround() {
System.out.println("环绕增强...");
}
}
在xml中配置自动代理
<aop:aspectj-autoproxy proxy-target-class="true"/>
proxy-target-class
:默认值为false- false:使用JDK动态代理
- true:使用CGLIB动态代理
纯注解方式实现
导入坐标同上
书写增强类同上
书写注解配置类启用自动代理
@Configuration
@ComponentScan("demo")
@EnableAspectJAutoProxy
public class AppConfig {
}