转自:
Spring通知类型简介说明及使用ProxyFactoryBean创建AOP代理的示例分享
下文笔者讲述Spring中通知类型的相关说明及ProxyFactoryBean创建AOP代理的示例分享,如下所示
Spring通知类型
在Spring中,通知(Advice)是对目标切入点进行功能增强的操作
Spring AOP为通知(Advice)提供了org.aopalliance.aop.Advice接口
Spring连接点位置
名称 | 备注 |
org.springframework.aop.MethodBeforeAdvice(前置通知) | 在方法之前自动执行的通知称为前置通知,可以应用于权限管理等功能 |
org.springframework.aop.AfterReturningAdvice(后置通知) | 在方法之后自动执行的通知称为后置通知,可以应用于关闭流、上传文件、删除临时文件等功能 |
org.aopalliance.intercept.MethodInterceptor(环绕通知) | 在方法前后自动执行的通知称为环绕通知,可以应用于日志、事务管理等功能 |
org.springframework.aop.ThrowsAdvice(异常通知) | 在方法抛出异常时自动执行的通知称为异常通知,可以应用于处理异常记录日志等功能 |
org.springframework.aop.IntroductionInterceptor(引介通知) | 在目标类中添加一些新的方法和属性,可以应用于修改旧版本程序(增强类) |
声明式Spring AOP
Spring创建一个AOP代理的方法使用 org.springframework.aop.framework.ProxyFactoryBean 此类对应的切入点和通知提供完整的控制能力
ProxyFactoryBean类中的常用可配置属性
属性名 | 备注 |
target | 代理的目标对象 |
proxyInterfaces | 代理要实现的接口 |
proxyTargetClass | 是否对类代理而不是接口,设置为 true 时,使用 CGLIB 代理 |
interceptorNames | 需要植入目标Advice |
singleton | 返回的代理是否为单例,默认为true(返回单实例) |
optimize | 当设置为true时,强制使用 CGLIB |
例:
下文采用示例的方式讲述spring创建AOP的示例
1.导入JAR包
创建spring 项目,并将以下jar包放入到lib目录中 spring-aop-3.2.13.RELEASE.jar: Spring为AOP提供的实现,在Spring包中已经提供 com.springsource.org.aopalliance-1.0.0.jar: AOP提供规范,可在Spring 的官网网址 https://repo.spring.io/webapp/#/search/quick/ 中进行搜索并下载
创建切面类 MyAspect
在src目录下创建一个名为 com.java265.factorybean 的包,在该包下创建切面类 MyAspect
package com.java265.factorybean; import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; //需要实现接口,确定哪个通知,及告诉Spring应该执行哪个方法 public class MyAspect implements MethodInterceptor { public Object invoke(MethodInvocation mi) throws Throwable { System.out.println("方法执行之前"); // 执行目标方法 Object obj = mi.proceed(); System.out.println("方法执行之后"); return obj; } } //MyAspect 类实现MethodInterceptor 接口 //并实现了接口的 invoke() 方法 //MethodInterceptor 接口是Spring AOP的JAR 包提供的 //invoke()方法用于确定目标方法 //Spring要在目标方法前后执行哪些方法
3.创建 Spring 配置文件
在 com.java265.factorybean 包下创建配置文件 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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--目标类 --> <bean id="customerDao" class="com.java265.dao.CustomerDaoImpl" /> <!-- 通知 advice --> <bean id="myAspect" class="com.java265.factorybean.MyAspect" /> <!--生成代理对象 --> <bean id="customerDaoProxy" class="org.springframework.aop.framework.ProxyFactoryBean"> <!--代理实现的接口 --> <property name="proxyInterfaces" value="com.java265.dao.CustomerDao" /> <!--代理的目标对象 --> <property name="target" ref="customerDao" /> <!--用通知增强目标 --> <property name="interceptorNames" value="myAspect" /> <!-- 如何生成代理,true:使用cglib; false :使用jdk动态代理 --> <property name="proxyTargetClass" value="true" /> </bean> </beans>
4.创建测试类
在 com.java265.factorybean 包下创建一个名为 FactoryBeanTest 的测试类,编辑后如下所示。
package com.java265.factorybean; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.java265.dao.CustomerDao; public class FactoryBeanTest { @Test public void test() { String xmlPath = "com/java265/factorybean/applicationContext.xml"; ApplicationContext applicationContext = new ClassPathXmlApplicationContext( xmlPath); CustomerDao customerDao = (CustomerDao) applicationContext .getBean("customerDaoProxy"); customerDao.add(); customerDao.update(); customerDao.delete(); customerDao.find(); } }
5.运行项目并查看结果