通过配置文件的方式解决以下问题
配置方式的设计、配置文件(注解),Spring已经帮我们封装好了
xml方式配置AOP的步骤:
1、 导入AOP相关坐标;
2、准备目标类、准备增强类,并配置给Spring管理;
3、配置切点表达式(哪些方法被增强);
4、配置织入(切点被哪些通知方法增强,是前置增强还是后置增强)。
package com.luxifa.service;
public interface UserService {void show1();void show2();}
package com.luxifa.service.impl;
public class UserServiceImpl implements UserService {@Overridepublic void show1() {System.out.println("show1...")}@Overridevoid show2() {System.out.println("show2...")}}
package com.luxifa.advice;//增强类.内部提供增强方法
public class MyAdvice {public void beforeAdvice() {System.out.println("前置的增强...");}public void afterAdvice() {System.out.println("后置的增强...");}
}
测试类:
public class ApplicationContextTest {public static void main(String[] args) {ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");UserService userService = app.getBean(UserService.class);userService.show1();}
}
控制台打印:
前置的增强....
show1....
execution([访问修饰符]返回值类型 包名.类名.方法名(参数))
其中:
切点表达式举例:
//表示访问修饰符为public、无返回值、在com.luxifa.aop包下的TargetImpl类的无参方法show
execution(public void com.luxifa.aop.TargetImpl.show())//表示com.luxifa.aop包下的TargetImpl类的任意方法
execution(* com.luxifa.aop.TargetImpl.*.(..))//表示com.luxifa.aop包下的任意类的任意方法
execution(* com.luxifa.aop.*.*(..))//表示om.luxifa.aop包及其子包下的任意类的任意方法
execution(* com.luxifa.aop..*.*(..))//表示任意包中的任意类的任意方法
execution(* *..*.*(..))
AspectJ的通知由以下五种类型
通知名称 | 配置方式 | 执行时机 |
---|---|---|
前置通知 | aop:before | 目标方法执行之前执行 |
后置通知 | aop:after-returning | 目标方法执行之后执行,目标方法异常时,不再执行 |
环绕通知 | aop:around | 目标方法执行前后执行,目标方法异常时,环绕后方法不再执行 |
异常通知 | aop:after-throwing | 目标方法抛出异常时执行 |
最终通知 | aop:after | 不管目标方法是否有异常,最终都会执行 |
环绕通知:
package com.luxifa.service;
public interface UserService {void show1();void show2();}
package com.luxifa.service.impl;
public class UserServiceImpl implements UserService {@Overridepublic void show1() {System.out.println("show1...")}@Overridevoid show2() {System.out.println("show2...")}}
package com.luxifa.advice;//增强类.内部提供增强方法
public class MyAdvice {public void beforeAdvice() {System.out.println("前置的增强...");}public void afterReturningAdvice() {System.out.println("后置的增强...");}public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {System.out.println("环绕前的增强...");//执行目标方法Object res = proceedingJoinPoint.proceed();System.out.println("环绕后的增强...");return res;}
}
通知方法在被调用时,Spring可以为其传递一些必要的参数
参数类型 | 作用 |
---|---|
JoinPoint | 连接点对象,任何通知都可使用,可以获得当前目标对象、目标方法参数等信息 |
ProceedingJoinPoint | JoinPoint子类对象,主要是在环绕通知中执行proceed(),进而执行目标方法 |
Throwable | 异常对象,使用在异常通知类中,需要在配置文件中指出异常对象名称 |
JointPoint对象
public void 通知方法名称(JointPoint joinPoint) {//获得目标方法的参数System.out.println(joinPoint.getArgs());//获得目标对象System.out.println(joinPoint.getTarget());//获得精确的切点表达式信息System.out.println(joinPoint.getStaticPart());
}
ProceedingJoinPoint对象
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {//获得目标方法的参数System.out.println(joinPoint.getArgs());//获得目标对象System.out.println(joinPoint.getTarget());//获得精确的切点表达式信息System.out.println(joinPoint.getStaticPart());//执行目标方法Object result = joinPoint.proceed();//返回目标方法返回值return result;
}
Throwable 对象
public void afterThrowing(JointPoint joinPoint,Throwable th) {//获得异常信息System.out.println("异常对象是:"+th+"异常信息是:"+th.getMessage());
}
AOP配置的两种语法形式
AOP的xml有两种配置方式,如下:
Spring定义了一个Advice接口,实现了该接口的类都可以作为通知类出现
public interface Advice{
}
advisor需要的通知类需要实现Advice的子功能接口,例如:MethodBeforeAdvice、AfterReturningAdvice等,是通过实现的接口去确定具备哪些通知增强的。
AOP配置的两种语法形式不同点
语法形式不同:
可配置的切面数量不同:
使用场景不同:
两种生成动态代理对象的方式,一种是基于JDK,一种基于Chlib
代理技术 | 使用条件 | 配置方式 |
---|---|---|
JDK动态代理技术 | 目标类有接口,是基于接口动态生成实现类的代理对象 | 目标类有接口的情况下,默认方式 |
Cglib动态代理技术 | 目标类无接口且不能使用final修饰,是基于被代理对象动态生成子对象为代理对象 | 目标类无接口时,默认使用该方式;目标类有接口时,手动配置aop:config |
Cglib基于超类的动态代理
//目标对象
Target target = new Target();
//通知对象
Advices advices = new Advices();
//增强器对象
Enhancer enhancer = new Enhancer();
//增强器设置父类
enhancer.setSuperclass(Target.class);
//增强器设置回调
enhancer.setCallback((MethodInterceptor)(o.method.methodProxy)->{advice.before();Object object = method.invoke(target,Objects);advice.afterReturning();return result;
});
//创建代理对象
Target targetProxy = (Target)enhancer.create();
//测试
String result = targetProxy.show("路西法");
上一篇:句子扩充法