Spring学习笔记
AOP
什么是AOP?
AOP(Aspect Oriented Programming)意为:面向切面编程,通过预编译的方式和运行期动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发的一个热点,也是Spring框架的一个重要内容,是函数式编程的一种衍生泛型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各个部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。

AOP在Spring中的作用
提供声明式事务;允许用户自定义切面
- 连接点(Joinpoint): 所谓连接点是指那些被拦截到的点。在spring中,这些点指的是方法,因为spring只支持方法类型的连接点,也可以理解连接点为:目标类上哪些有可能被增强的方法。
- 切点(Pointcut):可以理解为查询条件。一个target(目标类)的所有方法都是连接点,切点可以通过查询条件定位特定的连接点。
- 增强(Advice):织入目标类连接点上的一段程序代码。既包含连接点上的执行逻辑(横切逻辑、增强逻辑)又包含定位连接点的方位信息,before、after、around等。增强默认织入目标类的所有方法中。
- 目标对象(Target):增强逻辑织入的目标类。
- 代理(Proxy):一个类被AOP植入增强后,被产生一个结果代理类。
- 织入(Weaving):将通知(增强)应用到连接点上,生成代理的过程。
- 切面(Aspect):由切点和增强组成。
- 引介(Introduction):引介是一种特殊的通知在不修改类代码的前提下, Introduction可以在运行期为类动态地添加一些方法或Field。

SpringAOP中,通过Advice定义横切逻辑,Spring中支持5种类型的Advice:
前置增强
BeforeAdvice 代表前置增强,因为spring只支持方法级的增强,所以MethodBeforeAdvice 是目前可用前置增强,表示在目标方法执行前实施增强。
后置增强
AfterAdvice 代表后增强,表示目标方法在执行后实施增强
环绕增强
MethodInterceptor 代表环绕增强,表示目标方法执行前后实施增强
异常抛出增强
ThrowsAdvice 代表抛出异常增强,表示目标方法抛出异常后实施增强
引介增强
IntroductionInterceptor 代表引介增强,表示在目标类中添加一些新的方法和属性
及AOP在不改变原来代码的情况下,去增加新的功能。
使用Spring实现AOP
使用AOP,需要导入一个依赖包!
1 2 3 4 5 6
| <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.5</version> </dependency>
|
方式一:使用Spring的API接口
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
| <?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:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="userService" class="com.demo.service.UserServiceImpl"/> <bean id="log" class="com.demo.log.Log"/> <bean id="afterLog" class="com.demo.log.AfterLog"/>
<aop:config> <aop:pointcut id="pointcut" expression="execution(* com.demo.service.UserServiceImpl.*(..))"/>
<aop:advisor advice-ref="log" pointcut-ref="pointcut"/> <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"></aop:advisor> </aop:config> </beans>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
public class Log implements MethodBeforeAdvice { public void before(Method method, Object[] objects, Object o) throws Throwable { System.out.println(o.getClass().getName()+"的"+method.getName()+"被执行了");
} }
|
测试方法
1 2 3 4 5 6 7 8 9 10 11 12
|
public class MyTest { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); UserService userService = (UserService) context.getBean("userService"); userService.delete(); } }
|
方式二:自定义来实现AOP【主要是切面定义】
DiyPointCut.java
1 2 3 4 5 6 7 8 9 10 11 12 13
|
public class DiyPointCut { public void before(){ System.out.println("==========方法执行前"); }
public void after(){ System.out.println("==========方法执行后"); } }
|
xml
1 2 3 4 5 6 7 8 9 10
| <bean id="diy" class="com.demo.diy.DiyPointCut"/> <aop:config> <aop:aspect ref="diy"> <aop:pointcut id="point" expression="execution(* com.demo.service.UserServiceImpl.*(..))"/> <aop:before method="before" pointcut-ref="point"/> <aop:after method="after" pointcut-ref="point"/> </aop:aspect> </aop:config>
|
方式三:使用注解实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
@Aspect
public class AnnotationPointCut { @Before("execution(* com.demo.service.UserServiceImpl.*(..))") public void before(){ System.out.println("----------方法执行前"); } @After("execution(* com.demo.service.UserServiceImpl.*(..))") public void after(){ System.out.println("---------方法执行后"); } }
|
需要开启注解支持
1 2
| <aop:aspectj-autoproxy/>
|