cglib代理解析
jdk动态代理
动态代理技术在Spring中进行了封装,封装出来的类叫做ProxyFactory,表示是创建一个代理对象的一个工厂,比jdk动态代理和cglib代理更加方便,比如:
public class UserService {
public void test(){
System.out.println("test...");
throw new RuntimeException("111");
}
}
public class Demo {
public static void main(String[] args) {
UserService userService = new UserService();
ProxyFactory proxyFactory = new ProxyFactory();
proxyFactory.setTarget(userService);
proxyFactory.addAdvice((MethodInterceptor) invocation -> {
System.out.println("before...");
Object proceed = invocation.proceed();
System.out.println("after...");
return proceed;
});
UserService proxy = (UserService) proxyFactory.getProxy();
proxy.test();
}
}
通过ProxyFactory,我们可以不在关心到底是用cglib还是jdk动态代理了,ProxyFactory会帮我们去判断,如果UserService实现了接口,那么ProxyFactory底层就会用jdk动态代理,如果没有实现接口,就会用cglib技术.
BeforeAdvice:方法之前执行
public class MethodBeforeAdviceImpl implements MethodBeforeAdvice {
@Override
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println("方法执行前执行...");
}
}
public class Demo {
public static void main(String[] args) {
UserService userService = new UserService();
ProxyFactory proxyFactory = new ProxyFactory();
proxyFactory.setTarget(userService);
proxyFactory.addAdvice(new MethodBeforeAdviceImpl());
UserService proxy = (UserService) proxyFactory.getProxy();
proxy.test();
}
}
AfterReturningAdvice:方法return后执行
public class AfterReturningAdviceImpl implements AfterReturningAdvice {
@Override
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println("方法return后执行");
}
}
public class Demo {
public static void main(String[] args) {
UserService userService = new UserService();
ProxyFactory proxyFactory = new ProxyFactory();
proxyFactory.setTarget(userService);
proxyFactory.addAdvice(new AfterReturningAdviceImpl());
UserService proxy = (UserService) proxyFactory.getProxy();
proxy.test();
}
}
ThrowsAdvice:方法抛出异常后执行
public class ThrowsAdviceImpl implements ThrowsAdvice {
public void afterThrowing(Method method, Object[] args, Object target, RuntimeException ex) {
System.out.println("方法抛出异常后执行");
}
}
public class UserService {
public void test(){
System.out.println("test...");
throw new RuntimeException();
}
}
public class Demo {
public static void main(String[] args) {
UserService userService = new UserService();
ProxyFactory proxyFactory = new ProxyFactory();
proxyFactory.setTarget(userService);
proxyFactory.addAdvice(new ThrowsAdviceImpl());
UserService proxy = (UserService) proxyFactory.getProxy();
proxy.test();
}
}
ThrowsAdvice这个接口上没有任何方法,因为方法是由反射调用的.我们定义可以自己定义方法afterThrowing,只需要在参数里面加入异常参数就可以了,只要代码中抛出我们指定的异常就会执行afterThrowing方法.如果不是对应的异常将不会执行afterThrowing.
AroundAdvice:功能最强大的Advice,可以自定义执行顺序
public class AroundAdviceImpl implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
System.out.println("around before ...");
Object proceed = invocation.proceed();
System.out.println("around after ...");
return proceed;
}
}
public class Demo {
public static void main(String[] args) {
UserService userService = new UserService();
ProxyFactory proxyFactory = new ProxyFactory();
proxyFactory.setTarget(userService);
proxyFactory.addAdvice(new AroundAdviceImpl());
UserService proxy = (UserService) proxyFactory.getProxy();
proxy.test();
}
}
跟Advice类似的还有一个Advisor的概念,一个Advisor是由一个Pointcut和一个Advice组成的,通过Pointcut可以指定要需要被代理的逻辑,比如一个UserSevice类中有两个方法,按上面的例子,这两个方法都会被代理,被增强,那么我们现在可以通过Advisor,来控制到具体代理哪一个方法,比如:
public class UserService {
public void test(){
System.out.println("test...");
}
public void test1(){
System.out.println("test1...");
}
}
public class Demo {
public static void main(String[] args) {
UserService userService = new UserService();
ProxyFactory proxyFactory = new ProxyFactory();
proxyFactory.setTarget(userService);
proxyFactory.addAdvisor(new PointcutAdvisor() {
@Override
public Pointcut getPointcut() {
return new StaticMethodMatcherPointcut() {
@Override
public boolean matches(Method method, Class<?> targetClass) {
// 只代理test方法
return method.getName().equals("test");
}
};
}
@Override
public Advice getAdvice() {
return (MethodInterceptor) invocation -> {
System.out.println("before....");
Object res = invocation.proceed();
System.out.println("after....");
return res;
};
}
@Override
public boolean isPerInstance() {
return false;
}
});
UserService proxy = (UserService) proxyFactory.getProxy();
proxy.test();
proxy.test1();
}
}
上面示例代码表示,产生的代理对象,只有在执行test方法时才会被增强,会执行额外的逻辑,而在执行其他方法时是不会增强的.
上面介绍了Spring中提供了ProxyFactory,Advisor,Advice,PointCut等技术来实现代理对象的创建,但是我们在使用Spring时,我们并不会直接这么去使用ProxyFactory,比如说,我们希望ProxyFactory所产生的代理对象能直接就是Bean,能直接从Spring容器中得到UserService的代理对象,而这些Spring都是支持的,只不过,作为开发者的我们肯定得告诉Spring,那些类需要被代理,代理逻辑是什么
@Bean
public ProxyFactoryBean factoryBean() {
UserService userService = new UserService();
ProxyFactoryBean proxyFactoryBean = new ProxyFactoryBean();
proxyFactoryBean.setTarget(userService);
proxyFactoryBean.addAdvice((MethodInterceptor) invocation -> {
System.out.println("before...");
Object res = invocation.proceed();
System.out.println("after...");
return res;
});
return proxyFactoryBean;
}
通过这种方法定义一个UserService的Bean,并且是经过了AOP的,但是这种方式只能针对某一个Bean,它是一个FactoryBean,所以利用了FactoryBean技术,简介的将UserService的代理对象作为了Bean.
ProxyFactoryBean还有额外的功能,比如可以把某个Advice或Advisor定义为Bean,然后再ProxyFactoryBean中进行设置.
@Bean
public MethodInterceptor methodInterceptor() {
return invocation -> {
System.out.println("before...");
Object res = invocation.proceed();
System.out.println("after...");
return res;
};
}
@Bean
public ProxyFactoryBean factoryBean() {
UserService userService = new UserService();
ProxyFactoryBean proxyFactoryBean = new ProxyFactoryBean();
proxyFactoryBean.setTarget(userService);
proxyFactoryBean.setInterceptorNames("methodInterceptor");
return proxyFactoryBean;
}
@Bean
public MethodInterceptor methodInterceptor() {
return invocation -> {
System.out.println("before...");
Object res = invocation.proceed();
System.out.println("after...");
return res;
};
}
@Bean
public BeanNameAutoProxyCreator beanNameAutoProxyCreator(){
BeanNameAutoProxyCreator beanNameAutoProxyCreator = new BeanNameAutoProxyCreator();
beanNameAutoProxyCreator.setBeanNames("user*");
beanNameAutoProxyCreator.setInterceptorNames("methodInterceptor");
beanNameAutoProxyCreator.setProxyTargetClass(true);
return beanNameAutoProxyCreator;
}
通过BeanNameAutoProxyCreator可以对批量的Bean进行AOP,并且指定了代理逻辑,制定了一个interceptorName,也就是一个Advice,前提条件是这个Advice也得是一个Bean,这样Spring才能找到的,但是BeanNameAutoProxyCreator的缺点也很明显,他只能根据beanName来指定想要代理的bean.
@Bean
public DefaultPointcutAdvisor defaultPointcutAdvisor(){
NameMatchMethodPointcut pointcut = new NameMatchMethodPointcut();
pointcut.addMethodName("test");
DefaultPointcutAdvisor res = new DefaultPointcutAdvisor();
res.setPointcut(pointcut);
res.setAdvice(new AroundAdviceImpl());
return res;
}
@Bean
public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator(){
return new DefaultAdvisorAutoProxyCreator();
}
通过DefaultAdvisorAutoProxyCreator会直接去找所有Advisor类型的Bean,根据Advisor中PointCut和Advice信息,确定要代理的Bean以及代理逻辑.
@Component
@Aspect
public class Advice {
@Before("execution(public void com.fanqiechaodan.service.UserService.test())")
public void before(JoinPoint joinPoint){
System.out.println("before........");
}
}
通过上面这个类,我们直接定义好了所要代理的方法(通过一个表达式),以及代理逻辑(被@Before修饰的方法),简单明了,这样对于Spring来说,它要做的就是来解析这些注解了,解析之后得到对应的Pointcut对象,Advice对象,生成Advisor对象,扔进ProxyFactory中,进而产生对应的代理对象.
上面说到的Aspject中的注解,其中有五个是用来定义Advice的,表示代理逻辑,以及执行时机:
Spring自己也提供了类似的执行实际的实现类:
Spring会把五个注解解析为对应的Advice类: