目录
AOP(面向切面编程)
通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术
AOP中的名词
- Aspect(切面):切入点+通知 (即invoke方法).
- Advice(通知/增强):增强的代码
- Target(目标对象):被代理对象
- Proxy(代理):将通知织入到目标对象之后,形成代理对象
- PointCut(切入点):目标对象,已经增强的方法。
- JoinPoint(连接点):在目标对象中,所有可以增强的方法。
- WeAVing(织入):将通知应用到切入点的过程
Spring中支持5种类型的Advice
- 前置通知
- 后置通知
- 环绕通知
- 异常抛出通知
- 最终通知
使用Spring实现AOP
【重点】使用AOP织入,需要导入jar包
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
代理接口
public interface UserService {
public void add();
}
接口实现
public class UserServiceImpl implements UserService {
public void add() {
System.out.println("增加一个用户!");
}
}
方式一:使用Spring的API接口
通知API接口:
- 前置通知:MethodBeforeAdvice 方法:before()
- 后置通知:AfterReturningAdvice 方法:afterReturning()
- 环绕通知:MethodInterceptor 方法:invoke()
- 异常通知:ThrowsAdvice 方法:无
实现后置通知接口
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 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()+"被执行了!");
}
}
applicationContext.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.cf.service.UserServiceImpl"/>
<bean id="beforeLog" class="com.cf.log.BeforeLog"/>
<bean id="afterLog" class="com.cf.log.AfterLog"/>
<!--方式一:使用原生Spring API接口-->
<!--配置aop:需要导入aop的约束-->
<aop:config>
<!--切入点:expression:表达式,execution【要执行的位置 】(修饰词 返回值 包名.类名/接口名.方法名(参数列表) )-->
<aop:pointcut id="pointcut" expression="execution(public * com.cf.service.UserServiceImpl.*(..))"/>
<!--执行环绕增加-->
<aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="beforeLog" pointcut-ref="pointcut"/>
</aop:config>
</beans>
1、execution(): 表达式主体 (必须加上execution)。
2、第一个*号:表示返回值类型,号表示所有的类型。
3、包名:表示需要拦截的包名,后面的两个句点表示当前包和当前包的所有子包,cn.smd.service.impl包、子孙包下所有类的方法。
4、第二个号:表示类名,号表示所有的类。
5、(…):最后这个星号表示方法名,*号表示所有的方法,后面括弧里面表示方法的参数,两个句点表示任何参数。
方式二:自定义类实现AOP【主要是切面定义】
自定义类
public class DiyPointCut {
public void before(){
System.out.println("=====方法执行前=====");
}
public void after(){
System.out.println("=====方法执行后=====");
}
}
applicationContext.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">
<!--方式二:自定义类实现AOP-->
<bean id="diy" class="com.cf.diy.DiyPointCut"/>
<aop:config>
<!--自定义切面,ref 要引用的类-->
<aop:aspect ref="diy">
<!--切入点-->
<aop:pointcut id="point" expression="execution(* com.cf.service.UserServiceImpl.*(..))"/>
<!--通知-->
<aop:before method="before" pointcut-ref="point"/>
<aop:after method="after" pointcut-ref="point"/>
</aop:aspect>
</aop:config>
</beans>
<aop:advisor />与<aop:aspect >区别:
<aop:advisor />大多用于事务管理。
<aop:aspect >大多用于日志、缓存。
方式三:注解实现
切面类
//标注这个类是一个切面
@Aspect
public class AnnotationPointCut {
@Before("execution(* com.cf.service.UserServiceImpl.*(..))")
public void before(){
System.out.println("=====方法执行前=====");
}
@After("execution(* com.cf.service.UserServiceImpl.*(..))")
public void after(){
System.out.println("=====方法执行后=====");
}
//在环绕增强中,我们可以给定一个参数,代表我们要获取处理切入的点
@Around("execution(* com.cf.service.UserServiceImpl.*(..))")
public void around(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("环绕前");
Object proceed = joinPoint.proceed();
System.out.println("环绕后");
}
}
applicationContext.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 id="annotationPointCut" class="com.cf.diy.AnnotationPointCut"/>
<!--开启注解支持 JDK(默认proxy-target-class="false") cglib(proxy-target-class="true")-->
<aop:aspectj-autoproxy />
</beans>
测试
public class Mytest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//动态代理代理的是接口:注意点
UserService userService = context.getBean("userService", UserService.class);
userService.add();
}
}