AOP底层使用动态代理:
关于代理模式
AOP(术语)
连接点:类里面的方法可以被增强,这些方法称为连接点
切入点:实际被增强的方法称为切入点
通知(增强):实际增强的逻辑部分称为通知
通知有五种类型:前置通知、后置通知、环绕通知、异常通知、最终通知
切面:把通知应用到切入点的过程
AOP操作(准备)
Spring框架一般都是基于AspectJ实现AOP操作
1.什么是AspectJ?
AspectJ不是Spring组成部分,是一个独立的AOP框架,一般和Spring一起使用,进行AOP操作
2.基于AspectJ实现AOP操作:基于xml配置文件进行实现和基于注解方式实现
3.在项目工程里引入AOP相关依赖
4.切入点表达式:知道对哪个类里面的哪个方法进行增强
语法结构:execution([权限修饰符][返回类型][类全路径][方法名称]([参数列表]))
例子:execution(* com.keji.service.UserServiceImp.*(…))
导入AspectJ依赖后
代码步骤
方式一:使用原生spring API接口实现AOP(主要是Spring API接口实现)
1、创建接口:
public interface UserService {
public void add();
public void delete();
public void update();
public void query();
}
2、创建真实角色:
public class UserServiceImp implements UserService{
public void add() {
System.out.println("添加一个用户");
}
public void delete() {
System.out.println("删除一个用户");
}
public void update() {
System.out.println("修改一个用户!");
}
public void query() {
System.out.println("查询一个用户");
}
}
3、 创建前置通知和后置通知接口类:
public class BeforeAdvice 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 AfterAdvice implements AfterReturningAdvice {
//returnValue:返回结果
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println("执行了"+method.getName()+"的方法,返回值为"+returnValue);
}
}
4、基于xml配置文件进行实现aop操作:
<?xml version="1.0" encoding="UTF-8"?>
<!--suppress ALL -->
<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.keji.service.UserServiceImp"/>
<bean id="beforeAdvice" class="com.keji.log.BeforeAdvice"/>
<bean id="afterAdvice" class="com.keji.log.AfterAdvice"/>
<!--方式一:使用原生spring API接口-->
<!--配置aop:需导入aop约束-->
<aop:config>
<!--切入点-->
<aop:pointcut id="pointcut" expression="execution(* com.keji.service.UserServiceImp.*(..))"/>
<!--执行环绕增加-->
<aop:advisor advice-ref="beforeAdvice" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="afterAdvice" pointcut-ref="pointcut"/>
</aop:config>
</beans>
5、进行测试:
public class Test {
@org.junit.Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//动态代理代理的是接口
UserService userService = (UserService) context.getBean("userService");
userService.add();
}
}
测试结果:
方式二:自定义类实现AOP(主要是切面定义)
前两步遇上方法相同
3、创建自定义类:
public class DiyClass {
public void before(){
System.out.println("------------方法执行前---------");
}
public void after(){
System.out.println("------------方法执行后---------");
}
}
4、配置xml文件:
?xml version="1.0" encoding="UTF-8"?>
<!--suppress ALL -->
<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.keji.service.UserServiceImp"/>
<bean id="beforeAdvice" class="com.keji.log.BeforeAdvice"/>
<bean id="afterAdvice" class="com.keji.log.AfterAdvice"/>
<!--方法二:自定义类-->
<bean id="diy" class="com.keji.diy.DiyClass"/>
<aop:config>
<!--自定义切面 ref所要引用的类-->
<aop:aspect ref="diy">
<!--切入点-->
<aop:pointcut id="pointcut" expression="execution(* com.keji.service.UserServiceImp.*(..))"/>
<!--通知-->
<aop:before method="before" pointcut-ref="pointcut"/>
<aop:after method="after" pointcut-ref="pointcut"/>
</aop:aspect>
</aop:config>
</beans>
测试类与之前相同,测试结果:
方式三:使用注解实现
前两步与方法一相同
3、创建注解类:
@Aspect//标注这个类是一个切面
public class AnnotationAop {
@Before("execution(* com.keji.service.UserServiceImp.*(..))")
public void before(){
System.out.println("----前置通知----");
}
@After("execution(* com.keji.service.UserServiceImp.*(..))")
public void after(){
System.out.println("----后置通知----");
}
@Around("execution(* com.keji.service.UserServiceImp.*(..))")
public void around(ProceedingJoinPoint jp) throws Throwable {
System.out.println("---环绕前---");
Signature signature = jp.getSignature();//获得签名
System.out.println("签名为:"+signature);
Object proceed = jp.proceed();//执行方法
System.out.println("---环绕后---");
System.out.println(proceed);
}
}
4、在xml配置文件中配置开启注解支持
<bean id="annotationAop" class="com.keji.diy.AnnotationAop"/>
<!--开启注解-->
<aop:aspectj-autoproxy/>
测试类相同,测试结果: