本文也同步发布至简书,地址: https://www.jianshu.com/p/f70...
AOP设计模式一般运用在日志,校验等业务场景,本文将简单介绍基于Spring的AOP代理模式的运用。html
代理(Proxy)是一种提供了对目标对象另外的访问方式,即经过代理对象访问目标对象。这样作的好处是:能够在目标对象实现的基础上,加强额外的功能操做,即扩展目标对象的功能。
这里使用到编程中的一个思想:不要随意去修改别人已经写好的代码或者方法,若是需改修改,能够经过代理的方式来扩展该方法。java
静态代理在使用时,须要定义接口或者父类,被代理对象与代理对象一块儿实现相同的接口或者是继承相同父类。git
JDK动态代理有如下特色:
1.代理对象,不须要实现接口
2.代理对象的生成,是利用JDK的API,动态的在内存中构建代理对象(须要咱们指定建立代理对象/目标对象实现的接口的类型)
3.动态代理也叫作:JDK代理,接口代理github
Cglib代理,也叫做子类代理,它是在内存中构建一个子类对象从而实现对目标对象功能的扩展。spring
AOP实现的关键在于AOP框架自动建立的AOP代理,AOP代理主要分为静态代理和动态代理,静态代理的表明为AspectJ;而动态代理则以Spring AOP为表明。本文以Spring AOP的实现进行分析和介绍。express
Spring AOP使用的动态代理,所谓的动态代理就是说AOP框架不会去修改字节码,而是在内存中临时为方法生成一个AOP对象,这个AOP对象包含了目标对象的所有方法,而且在特定的切点作了加强处理,并回调原对象的方法。编程
Spring AOP中的动态代理主要有两种方式,JDK动态代理
和CGLIB动态代理
。JDK动态代理经过反射来接收被代理的类,而且要求被代理的类必须实现一个接口。JDK动态代理的核心是InvocationHandler
接口和Proxy
类。设计模式
若是目标类没有实现接口,那么Spring AOP会选择使用CGLIB来动态代理目标类。CGLIB(Code Generation Library),是一个代码生成的类库,能够在运行时动态的生成某个类的子类,注意,CGLIB是经过继承的方式作的动态代理,所以若是某个类被标记为final
,那么它是没法使用CGLIB作动态代理的。框架
注意:以上片断引用自文章 Spring AOP的实现原理,若有冒犯,请联系笔者删除之,谢谢!
Spring AOP判断是JDK代理仍是CGLib代理的源码以下(来自org.springframework.aop.framework.DefaultAopProxyFactory
):ide
@Override public AopProxy createAopProxy(AdvisedSupport config) throws AopConfigException { if (config.isOptimize() || config.isProxyTargetClass() || hasNoUserSuppliedProxyInterfaces(config)) { Class<?> targetClass = config.getTargetClass(); if (targetClass == null) { throw new AopConfigException("TargetSource cannot determine target class: " + "Either an interface or a target is required for proxy creation."); } if (targetClass.isInterface() || Proxy.isProxyClass(targetClass)) { return new JdkDynamicAopProxy(config); } return new ObjenesisCglibAopProxy(config); } else { return new JdkDynamicAopProxy(config); } }
由代码发现,若是配置proxyTargetClass = true
了而且目标类非接口的状况,则会使用CGLib代理,不然使用JDK代理。
Spring AOP的配置有两种方式,XML和注解方式。
首先须要引入AOP相关的DTD配置,以下:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd ">
而后须要引入AOP自动代理配置:
<!-- 自动扫描(自动注入) --> <context:component-scan base-package="org.landy" /> <!-- 指定proxy-target-class为true可强制使用cglib --> <aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>
Java配置类以下:
/** * 至关于Spring.xml配置文件的做用 * @author landyl * @create 2:44 PM 09/30/2018 */ @Configuration //@EnableLoadTimeWeaving(aspectjWeaving = EnableLoadTimeWeaving.AspectJWeaving.ENABLED) @EnableAspectJAutoProxy(proxyTargetClass = true) //@EnableAspectJAutoProxy @ComponentScan(basePackages = "org.landy") public class ApplicationConfigure { @Bean public ApplicationUtil getApplicationUtil() { return new ApplicationUtil(); } }
须要使用Spring AOP须要引入如下Jar包:
<properties> <spring.version>5.0.8.RELEASE</spring.version> <aspectj.version>1.8.7</aspectj.version> </properties> <!-- aspectjrt.jar包主要是提供运行时的一些注解,静态方法等等东西,一般咱们要使用aspectJ的时候都要使用这个包。 --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>${aspectj.version}</version> </dependency> <!-- aspectjweaverjar包主要是提供了一个java agent用于在类加载期间织入切面(Load time weaving)。 而且提供了对切面语法的相关处理等基础方法,供ajc使用或者供第三方开发使用。这个包通常咱们不须要显式引用,除非须要使用LTW。 --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>${aspectj.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> <scope>compile</scope> </dependency>
以上两种配置方式,单元测试须要注意一个地方就是引入配置的方式不同,区别以下:
XML方式
@ContextConfiguration(locations = { "classpath:spring.xml" }) //加载配置文件 @RunWith(SpringJUnit4ClassRunner.class) //使用junit4进行测试 public class SpringTestBase extends AbstractJUnit4SpringContextTests { }
@ContextConfiguration(classes = ApplicationConfigure.class) @RunWith(SpringJUnit4ClassRunner.class) //使用junit4进行测试 public class SpringTestBase extends AbstractJUnit4SpringContextTests { }
配置好了之后,之后全部的测试类都继承SpringTestBase
类便可。
本文将以校验某个业务逻辑为例说明Spring AOP代理模式的运用。
按照惯例,仍是以客户信息更新校验为例,假设有个校验类以下:
/** * @author landyl * @create 2:22 PM 09/30/2018 */ @Component public class CustomerUpdateRule implements UpdateRule { //利用自定义注解,进行AOP切面编程,进行其余业务逻辑的校验操做 @StatusCheck public CheckResult check(String updateStatus, String currentStatus) { System.out.println("CustomerUpdateRule:在此还有其余业务校验逻辑。。。。"+updateStatus + "____" + currentStatus); return new CheckResult(); } }
此时咱们须要定义一个注解StatusCheck
类,以下:
/** * @author landyl * @create 2:37 PM 09/23/2018 */ @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface StatusCheck { }
此注解仅为一个标记注解。最为主要的就是定义一个更新校验的切面类,定义好切入点。
@Component @Aspect public class StatusCheckAspect { private static final int VALID_UPDATE = Constants.UPDATE_STATUS_VALID_UPDATE; private static final Logger LOGGER = LoggerFactory.getLogger(StatusCheckAspect.class); //定义切入点:定义一个方法,用于声明切面表达式,通常地,该方法中再也不须要添加其余的代码 @Pointcut("execution(* org.landy.business.rules..*(..)) && @annotation(org.landy.business.rules.annotation.StatusCheck)") public void declareJoinPointExpression() {} /** * 前置通知 * @param joinPoint */ @Before("declareJoinPointExpression()") public void beforeCheck(JoinPoint joinPoint) { System.out.println("before statusCheck method start ..."); System.out.println(joinPoint.getSignature()); //得到自定义注解的参数 String methodName = joinPoint.getSignature().getName(); List<Object> args = Arrays.asList(joinPoint.getArgs()); System.out.println("The method " + methodName + " begins with " + args); System.out.println("before statusCheck method end ..."); } }
具体代码请参见github。
JDK动态代理必须实现一个接口,本文实现UpdateRule
为例,
public interface UpdateRule { CheckResult check(String updateStatus, String currentStatus); }
而且AOP须要作以下配置:
XML方式:
<!-- 指定proxy-target-class为true可强制使用cglib --> <aop:aspectj-autoproxy proxy-target-class="false"></aop:aspectj-autoproxy>
注解方式:
@Configuration @EnableAspectJAutoProxy @ComponentScan(basePackages = "org.landy") public class ApplicationConfigure { }
在测试类中,必须使用接口方式注入:
/** * @author landyl * @create 2:32 PM 09/30/2018 */ public class CustomerUpdateRuleTest extends SpringTestBase { @Autowired private UpdateRule customerUpdateRule; //JDK代理方式必须以接口方式注入 @Test public void customerCheckTest() { System.out.println("proxy class:" + customerUpdateRule.getClass()); CheckResult checkResult = customerUpdateRule.check("2","currentStatus"); AssertUtil.assertTrue(checkResult.getCheckResult() == 0,"与预期结果不一致"); } }
测试结果以下:
proxy class:class com.sun.proxy.$Proxy34 2018-10-05 14:18:17.515 [main] INFO org.landy.business.rules.aop.StatusCheckAspect - Status check around method start .... before statusCheck method start ... CheckResult org.landy.business.rules.stategy.UpdateRule.check(String,String) The method check begins with [2, currentStatus] before statusCheck method end ... CustomerUpdateRule:在此还有其余业务校验逻辑。。。。2____currentStatus 2018-10-05 14:18:17.526 [main] INFO org.landy.business.rules.aop.StatusCheckAspect - execute the target method,the return result_msg:null 2018-10-05 14:18:17.526 [main] INFO org.landy.business.rules.aop.StatusCheckAspect - Status check around method end ....
以上结果说明它生成的代理类为$Proxy34,说明是JDK代理。
使用CGlib能够不用接口(经测试,用了接口好像也没问题)。在测试类中,必须使用实现类方式注入:
@Autowired private CustomerUpdateRule customerUpdateRule;
而且AOP须要作以下配置:
XML方式:
<!-- 指定proxy-target-class为true可强制使用cglib --> <aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>
注解方式:
@Configuration @EnableAspectJAutoProxy(proxyTargetClass = true) @ComponentScan(basePackages = "org.landy") public class ApplicationConfigure { }
不过发现我并未配置proxyTargetClass = true
也能够正常运行,有点奇怪。(按理说,默认是为false)
运行结果生成的代理类为:
proxy class:class org.landy.business.rules.stategy.CustomerUpdateRule$$EnhancerBySpringCGLIB$$d1075aca
说明是CGLib代理。
通过进一步测试,发现若是我实现接口UpdateRule
,可是注入方式使用类注入方式:
@Autowired private CustomerUpdateRule customerUpdateRule;
而且把proxyTargetClass
设置为false,则运行就报以下错误:
严重: Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@6a024a67] to prepare test instance [org.landy.business.rules.CustomerUpdateRuleTest@7fcf2fc1] org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.landy.business.rules.CustomerUpdateRuleTest': Unsatisfied dependency expressed through field 'customerUpdateRule'; nested exception is org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'customerUpdateRule' is expected to be of type 'org.landy.business.rules.stategy.CustomerUpdateRule' but was actually of type 'com.sun.proxy.$Proxy34' at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:586)
以上说明了一个问题,使用接口实现的方式则会被默认为JDK代理方式,若是须要使用CGLib代理,须要把proxyTargetClass
设置为true
。
为了再次验证Spring AOP如何选择JDK代理仍是CGLib代理,在此进行一个综合测试。
测试前提:
UpdateRule
接口测试类使用接口方式注入
@Autowired private UpdateRule customerUpdateRule; //JDK代理方式必须以接口方式注入
测试:
配置proxyTargetClass
为true
,运行结果以下:
customerCheckTest proxy class:class org.landy.business.rules.stategy.CustomerUpdateRule$$EnhancerBySpringCGLIB$$f5a34953 2018-10-05 15:28:42.820 [main] INFO org.landy.business.rules.aop.StatusCheckAspect - Status check around method start .... 2018-10-05 15:28:42.823 [main] INFO org.landy.business.rules.aop.StatusCheckAspect - Status check dynamic AOP,paramValues:2 AOP实际校验逻辑。。。。2----currentStatus before statusCheck method start ... target class:org.landy.business.rules.stategy.CustomerUpdateRule@7164ca4c
说明为CGLIb代理。
配置proxyTargetClass
为false
,运行结果以下:
proxy class:class com.sun.proxy.$Proxy34 2018-10-05 15:20:59.894 [main] INFO org.landy.business.rules.aop.StatusCheckAspect - Status check around method start .... before statusCheck method start ... target class:org.landy.business.rules.stategy.CustomerUpdateRule@ae3540e
说明为JDK代理。
以上测试说明,指定proxy-target-class为true可强制使用cglib。
若是使用JDK动态代理,未使用接口方式注入(或者使用接口实现,并未配置proxyTargetClass
为true),则会出现如下异常信息:
严重: Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@6a024a67] to prepare test instance [org.landy.business.rules.CustomerUpdateRuleTest@7fcf2fc1] org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.landy.business.rules.CustomerUpdateRuleTest': Unsatisfied dependency expressed through field 'customerUpdateRule'; nested exception is org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'customerUpdateRule' is expected to be of type 'org.landy.business.rules.stategy.CustomerUpdateRule' but was actually of type 'com.sun.proxy.$Proxy34'
与生成的代理类型不一致,有兴趣的同窗能够Debug DefaultAopProxyFactory
类中的createAopProxy
方法便可知道两种动态代理的区别。