文章目录
AOP (面向切面编程),本质是动态代理,使用AOP技术,可以降低业务逻辑各部分之间的耦合度,提高程序的可重用性,可用于事务管理、日志输出、拦截器、权限验证等。
1 动态代理
程序在运行期间,动态的生成代理对象,代理对象的方法执行时,去调用目标对象的方法,从而实现功能的增强。
1.1 JDK动态代理
java.lang.reflect.Proxy,是基于接口的动态代理(代理对象->目标接口->目标对象)。
//目标接口Target,目标类TargetImp
public class TargetImp implements Target {
@Override
public void printSome() {
System.err.println("目标对象TargetImp");
}
}
//代理类
public class DynamicProxy {
//获取代理对象,为目标接口Target做代理
public Target getProxy(){
Target proxy = (Target) Proxy.newProxyInstance(TargetImp.class.getClassLoader(), TargetImp.class.getInterfaces(),new InvocationHandler() {
@Override
public Object invoke(Object o, Method method, Object[] objects) throws Throwable {
System.err.println("在前面增加了。。。");
method.invoke(new TargetImp(),objects);//new TargetImp()创建被代理对象(目标对象)
System.err.println("在后面增加了。。。");
return null;
}
});
return proxy;
}
public static void main(String[] args) {
new DynamicProxy().getProxy().printSome();
}
}
测试结果:
1.2 CGLIB代理
net.sf.cglib.proxy,是基于父类的动态代理(代理对象->目标对象)。
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>3.3.0</version>
</dependency>
//目标类
public class Target {
public String saySome(){
System.out.println("cglib target......");
return "hello";
}
}
//代理类
public class CglibProxy{
//获取代理对象,为目标类Target做代理
public Target getProxy(){
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(Target.class);//设置父类,即被代理的类(目标类)
enhancer.setCallback(new MethodInterceptor(){
@Override
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
System.out.println("cglib前增强。。。");
Object invoke = method.invoke(new Target(),args);//new Target()创建被代理对象(目标对象)
System.out.println("cglib后增强。。。");
return invoke;
}
});
Target proxy = (Target)enhancer.create();//创建代理对象
return proxy;
}
public static void main(String[] args) {
String res = new CglibProxy().getProxy().saySome();
System.err.println("返回值:"+res);
}
}
测试结果:
2 AOP
Spring的AOP 实际上就是对第1节中动态代理的代码进行了封装,我们只需要配置相关的内容或使用一些注解,就能实现对目标类中方法的功能增强。Spring会根据目标类是否实现了接口来决定采用哪种动态代理的方式。
<!--aop包含在spring-context中-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.13</version>
</dependency>
<!--aspectj-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.13</version>
</dependency>
2.1 XML示例
2.1.1 程序实现
//目标接口Target,目标类TargetImp
public class TargetImp implements Target {
@Override
public void printSome() {
System.err.println("目标对象TargetImp");
}
}
//切面类
public class Aspect {
//增强方法
public void before(){
System.out.println("前置增强了");
}
}
2.1.2 增加配置
applicationContext.xml中配置如下:
<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="target" class="com.test.principal.impl.TargetImp"/>
<bean id="aspect" class="com.test.xml.Aspect"/>
<!--织入-->
<aop:config>
<!--切面对象aspect-->
<aop:aspect ref="aspect">
<!--目标接口Target的printSome方法前,执行切面对象aspect的before方法-->
<!--<aop:before method="before" pointcut="execution(public void com.test.principal.Target.printSome())"/>-->
<!--抽取公共切点-->
<aop:pointcut id="pc" expression="execution(public void com.test.principal.Target.printSome())"/>
<aop:before method="before" pointcut-ref="pc"/>
</aop:aspect>
</aop:config>
</beans>
2.1.3 测试
//测试
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(value = {"classpath:applicationContext.xml"})
public class TestSpring {
@Autowired
private Target target;
@Test
public void testTarget(){
target.printSome();
}
}
测试结果:
2.2 注解示例
2.2.1 配置文件
applicationContext.xml中配置:
<!--扫描com.test包下所有的类-->
<context:component-scan base-package="com.test"/>
<!--aop自动代理-->
<aop:aspectj-autoproxy/>
或者配置类添加:
@ComponentScan("com.test")
@EnableAspectJAutoProxy
public class ApplicationConfig {
}
2.2.2 程序实现
//目标类
@Component("target")
public class TargetImp implements Target {
@Override
public void printSome() {
System.err.println("目标对象TargetImp");
}
}
//切面类
@Component("myAspect")
@Aspect
public class AspectProxy {
//增强方法
//环绕增强时,必须有JoinPoint参数
//@Around("execution(* com.test.principal.Target.*(..))")
@Around("common()")
public void around(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("前增强");
pjp.proceed();//执行拦截到的方法
System.out.println("后增强");
}
//提取的公共切点
@Pointcut("execution(* com.test.principal.Target.*(..))")
public void common(){
}
}
2.2.3 测试
//@ContextConfiguration(value = {"classpath:applicationContext-aop.xml"})
@ContextConfiguration(classes = {ApplicationConfig.class})
@RunWith(SpringJUnit4ClassRunner.class)
public class TestSpring {
@Autowired
private Target target;
@Test
public void testTarget(){
target.printSome();
}
}
测试结果:
3 必知概念
3.1 基本概念
几个基本概念:
名称 | 说明 |
---|---|
Target | 目标对象 |
Proxy | 代理类 |
Joinpoint | 连接点,被拦截到的方法,环绕通知会用到 |
Pointcut | 切点,被增强的方法(目标对象中的方法) |
Advice | 通知,增强方法 |
Aspect | 切面,是切点和通知的结合。切面类中写增强方法(通知) |
Weaving | 织入,配置切面和目标对象关系,即创建代理对象的过程 |
3.2 通知类型
<aop:通知类型 method=“切面类中方法名(增强方法名)” pointcut=“切点表达式”></aop:通知类型>
标签 | 注解 | 说明 |
---|---|---|
<aop:before> | @Before | 前置通知,在被增强方法前执行 |
<aop:afer-returning> | @AfterReturning | 后置通知,在被增强方法后执行 |
<aop:throwing> | @AfterThrowing | 异常抛出通知,在被增强方法出现异常后执行 |
<aop:after> | @After | 最终通知,在被增强方法后执行,无论被增强方法是否有异常都会执行 |
<aop:around> | @Around | 环绕通知,在被增强方法之前和之后都执行,功能最全,但是非必要不使用 |
3.3 切点表达式
pointcut=“切点表达式”,@通知类型(“切点表达式”),<aop:pointcut expression=“切点表达式”>,@Pointcut(“切点表达式”),切点表达式的语法为execution([修饰符] 返回值类型 包名.类名.方法名(参数)),其中:
表达式元素 | 说明 |
---|---|
修饰符 | 可省略,execution(void com.test.principal.Target.printSome()) |
返回值类型 | 可为* ,代表任意类型,execution(* com.test.principal.Target.printSome()) |
包名 | 可为*,代表任意包 |
类名 | 可为*,代表任意类,execution(* com.test.principal..(…)) |
方法名 | 可为*,代表方法,execution(void com.test.principal.Target.*(…)) |
包名与类名之间的点. | 一个点 . 代表当前包下的类,两个点 … 表示当前包及其子包下的类,execution(* com.test.principal….(…)) |
参数 | 可为两个点 … 表示任意个数、任意类型的参数列表,execution(* ….*(…)) |