Springboot原始碼分析之代理三板斧
阿新 • • 發佈:2019-08-24
摘要:
在Spring
的版本變遷過程中,註解發生了很多的變化,然而代理的設計也發生了微妙的變化,從Spring1.x
的ProxyFactoryBean
的硬編碼島Spring2.x
的Aspectj
註解,最後到了現在廣為熟知的自動代理。
說明:
ProxyConfig
代理的相關配置類AdvisedSupport
實現了Advised
,封裝了對Advice
和Advisor
的操作ProxyCreatorSupport
該類及其子類主要是利用代理工廠幫助建立jdk
或者cglib
的代理物件ProxyProcessorSupport
該類及其子類才是我們目前用得做多的,利用後置處理器來進行自動代理處理
ProxyFactoryBean
package com.github.dqqzj.springboot.aop; import org.springframework.aop.MethodBeforeAdvice; import org.springframework.aop.TargetSource; import org.springframework.aop.framework.ProxyFactoryBean; import org.springframework.aop.target.SingletonTargetSource; import org.springframework.context.annotation.Bean; import org.springframework.stereotype.Component; import java.lang.reflect.Method; /** * @author qinzhongjian * @date created in 2019-08-24 11:05 * @description: TODO * @since JDK 1.8.0_212-b10 */ @Component public class MyMethodBeforeAdvice implements MethodBeforeAdvice { @Override public void before(Method method, Object[] args, Object target) throws Throwable { if (!method.getName().equals("toString")) { System.out.println(target.getClass().getName() + "#" + method.getName()); } } /** * 代理的目標物件 效果同setTargetSource(@Nullable TargetSource targetSource) * TargetSource targetSource = new SingletonTargetSource(aopService); * 可以從容器獲取,也可以類似下面這樣直接new,使用區別需要熟悉spring機制。 * factoryBean.setTarget(new AopService()); * * 設定需要被代理的介面 效果同factoryBean.setProxyInterfaces(new Class[]{AopService.class}); * 若沒有實現介面,那就會採用cglib去代理 * 如果有介面不指定的話會代理所有的介面,否則代理指定的介面 * * setInterceptorNames方法原始碼中有這樣的一句話:Set the list of Advice/Advisor bean names. This must always be set * to use this factory bean in a bean factory. */ @Bean public ProxyFactoryBean proxyFactoryBean(AopService aopService) { ProxyFactoryBean factoryBean = new ProxyFactoryBean(); factoryBean.setTarget(aopService); //factoryBean.setInterfaces(AopService.class); factoryBean.setInterceptorNames("myMethodBeforeAdvice"); //是否強制使用cglib,預設是false的 //factoryBean.setProxyTargetClass(true); return factoryBean; } }
原始碼分析:
@Override @Nullable public Object getObject() throws BeansException { //根據我們配置的interceptorNames來獲取對應的Advisor並加入通知器執行鏈中 initializeAdvisorChain(); if (isSingleton()) { //生成singleton的代理物件,會利用DefaultAopProxyFactory去生成代理 //在內部如果你手動沒有去設定需要被代理的介面,Spring會代理你所有的實現介面。 return getSingletonInstance(); } else { if (this.targetName == null) { logger.warn("Using non-singleton proxies with singleton targets is often undesirable. " + "Enable prototype proxies by setting the 'targetName' property."); } //和單利非常類似 只不過沒有快取了 return newPrototypeInstance(); } } private synchronized void initializeAdvisorChain() throws AopConfigException, BeansException { if (this.advisorChainInitialized) { return; } if (!ObjectUtils.isEmpty(this.interceptorNames)) { // 最後一個不能是全域性的suffix *,除非我們指定了targetSource之類的 if (this.interceptorNames[this.interceptorNames.length - 1].endsWith(GLOBAL_SUFFIX) && this.targetName == null && this.targetSource == EMPTY_TARGET_SOURCE) { throw new AopConfigException("Target required after globals"); } for (String name : this.interceptorNames) { // 如國攔截器的名稱是以*結尾的,說明它要去全局裡面都搜尋出來 // 全域性:去自己容器以及父容器中找,型別為Advisor.class的,名稱是以這個名稱為開頭的prefix的Bean. if (name.endsWith(GLOBAL_SUFFIX)) { addGlobalAdvisor((ListableBeanFactory) this.beanFactory, name.substring(0, name.length() - GLOBAL_SUFFIX.length())); } // 一般的情況下我們都是精確匹配 else { Object advice; if (this.singleton || this.beanFactory.isSingleton(name)) { // 從容器裡獲取該bean advice = this.beanFactory.getBean(name); } // 原型處理 else { advice = new PrototypePlaceholderAdvisor(name); } addAdvisorOnChainCreation(advice, name); } } } this.advisorChainInitialized = true; } // 將advice物件新增到通知器鏈中 private void addAdvisorOnChainCreation(Object next, String name) { // 這裡呼叫namedBeanToAdvisor做了一下適配:成統一的Advisor Advisor advisor = namedBeanToAdvisor(next); addAdvisor(advisor); } //方法中首先會呼叫namedBeanToAdvisor(next)方法,將從ioc容器獲取的普通物件轉換成通知器Advisor物件 private Advisor namedBeanToAdvisor(Object next) { try { return this.advisorAdapterRegistry.wrap(next); } }
DefaultAdvisorAdapterRegistry
這個類還允許我們自定義介面卡,然後註冊到裡面就行。
@Override
public void registerAdvisorAdapter(AdvisorAdapter adapter) {
this.adapters.add(adapter);
}
ProxyFactoryBean脫離IoC容器使用
ProxyFactory
說明:這個類一般是spring
自己內部使用的,我們自定義的話很難與容器進行整合,它一般都是返回的原型模式代理
AspectJProxyFactory
小結:
根據以上案例可以發現 都是首先進行AdvisedSupport的準備,然後交給子類ProxyCreatorSupport根據條件
得到JDK或者CGLIB的AopProxy,當代理物件被呼叫的時候在invoke或者intercept方法中會呼叫ProxyCreatorSupport的getInterceptorsAndDynamicInterceptionAdvice方法去初始化advice和各個方法之間的對映關係並快取
同類方法代理不生效原因?
很多時候會發現代理方法和非代理方法在同一個類中呼叫不生效和呼叫順序有關係,我們進行重構程式碼來分析一下原因
public class AspectJProxyFactoryApplication {
public static void main(String[] args) {
AspectJProxyFactory proxyFactory = new AspectJProxyFactory(new AopService());
// 注意:此處得MyAspect類上面的@Aspect註解必不可少
proxyFactory.addAspect(MyAspect.class);
//proxyFactory.setProxyTargetClass(true);//是否需要使用CGLIB代理
AopService proxy = proxyFactory.getProxy();
proxy.test();
}
}
@Aspect
public class MyAspect {
//@Pointcut("execution(* com.github..aop.*.*(..))")
@Pointcut("execution(* com.github..aop.AopService.hello(..))")
private void pointcut() {
}
@Before("pointcut()")
public void before() {
System.out.println("-----------MyAspect#before-----------");
}
}
@Service
public class AopService {
public String hello() {
System.out.println("hello, AopService");
return "hello, AopService";
}
public String test() {
System.out.println("test");
return hello();
}
}
答案就是不會生效,究竟是什麼引起的呢?其實就是我上面的小結的最後一個知識點。
這個時候chain
沒有我們的通知器在裡面,
最終按照我們的程式執行,下面進行修改切點表示式,如果上面的例子看的諮詢的話下面就可以忽略了,主要就是是否增強就是第一個入口函式能否匹配上我們的切點表示式後續的根本不會關心你是否能匹配上。
@Aspect
public class MyAspect {
@Pointcut("execution(* com.github..aop.*.*(..))")
//@Pointcut("execution(* com.github..aop.AopService.hello(..))")
private void pointcut() {
}
@Before("pointcut()")
public void before() {
System.out.println("-----------MyAspect#before-----------");
}
}
處理完後就會按照下面程式碼正常流程執行完
if (this.currentInterceptorIndex == this.interceptorsAndDynamicMethodMatchers.size() - 1) {
return invokeJoinpoint();
}
如果同一個類的方法呼叫都想讓通知器生效怎麼辦?這個就必須要讓通知新增到執行鏈中才行,根據上面所講的內容就可以達到這個目的