版权声明:本文为博主ExcelMann的原创文章,未经博主允许不得转载。
9、AOP
作者:ExcelMann,转载需注明。
9.1、什么是AOP
9.2、AOP在Spring中的作用
作用:
提供声明式事务;允许用户自定义切面;
一些名词的介绍(不用死记硬背):
在SpringAOP中,通过Advice
定义横切逻辑,Spring中支持5种类型的Advice:
即:AOP可以在不改动原有代码的基础上,去增加新的功能。
9.3、使用Spring实现AOP
【重点】使用AOP织入,需要先导入一个依赖包!
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
方式一:采用Spring的API接口实现
- xml文件
<?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
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--注册bean-->
<bean id="userService" class="com.excelman.service.UserServiceImpl"/>
<bean id="log" class="com.excelman.log.BeforeLog"/>
<bean id="afterLog" class="com.excelman.log.AfterLog"/>
<!--方式一:使用原生Spring AOP接口-->
<!--配置AOP:需要导入aop的约束-->
<aop:config>
<!--切入点:expression为表达式,execution(要执行的位置 * * * * *)-->
<aop:pointcut id="pointcut" expression="execution(* com.excelman.service.UserServiceImpl.*(..))"/>
<!--执行环绕增强-->
<aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
</aop:config>
</beans>
- Log类(需要加载的类)
public class BeforeLog implements MethodBeforeAdvice {
//method:要执行的目标对象的方法
//args:参数
//target:目标对象
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println(target.getClass().getName()+"的"+method.getName()+"被执行了");
}
}
public class AfterLog implements AfterReturningAdvice {
//returnValue:返回值
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println("执行了"+method.getName()+",返回了"+returnValue);
}
}
- 测试类
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//因为代理的是接口,所以最终返回的代理类是接口
UserService userService = (UserService) context.getBean("userService");
userService.add();
}
}
方式二:采用自定义的形式实现
- xml文件
<!--方式二:自定义类-->
<bean id="diy" class="com.excelman.diy.DiyPointCut"/>
<aop:config>
<!--自定义切面(也就是要插入的类),ref为要引用的类-->
<aop:aspect ref="diy">
<!--定义切入点-->
<aop:pointcut id="point" expression="execution(* com.excelman.service.UserServiceImpl.*(..))"/>
<!--通知(即类中要执行的方法)-->
<aop:before method="before" pointcut-ref="point"/>
<aop:after method="after" pointcut-ref="point"/>
</aop:aspect>
</aop:config>
- 切面(自定义的类)
public class DiyPointCut {
public void before(){
System.out.println("=======方法执行前=======");
}
public void after(){
System.out.println("=======方法执行后=======");
}
}
- 测试(同上)
方式三:采用注解的形式实现
- 自定义切面,且采用注解定义
//使用注解,标记这个类为一个切面
@Aspect
public class AnnotationPointCut {
//使用注解,标记该通知为before,且参数为切入点
@Before("execution(* com.excelman.service.UserServiceImpl.*(..))")
public void before(){
System.out.println("=========方法执行前========");
}
@After("execution(* com.excelman.service.UserServiceImpl.*(..)))")
public void after(){
System.out.println("=========方法执行后========");
}
//在环绕增强中,我们可以给定一个参数,代表我们要获取处理切入的点
@Around("execution(* com.excelman.service.UserServiceImpl.*(..)))")
public void around(ProceedingJoinPoint jp) throws Throwable {
System.out.println("环绕前");
Object proceed = jp.proceed(); //执行方法
System.out.println("环绕后");
}
}
- xml文件中,注册切面的bean,并且开启aop注解
<!--第三种方式:使用注解-->
<bean id="annotationPointCut" class="com.excelman.diy.AnnotationPointCut"/>
<!--aop开启注解支持-->
<aop:aspectj-autoproxy/>