您好,登錄后才能下訂單哦!
這篇文章主要講解了“Spring中AOP創建代理的方法”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“Spring中AOP創建代理的方法”吧!
protected Object wrapIfNecessary(Object bean, String beanName, Object cacheKey) { //1.尋找增強器 Object[] specificInterceptors = getAdvicesAndAdvisorsForBean(bean.getClass(), beanName, null); if (specificInterceptors != DO_NOT_PROXY) { //2.創建代理 Object proxy = createProxy(bean.getClass(), beanName, specificInterceptors, new SingletonTargetSource(bean)); return proxy; } }
protected Object[] getAdvicesAndAdvisorsForBean(Class beanClass, String beanName, TargetSource targetSource) { //第六篇AOP尋找增強器已經分析了該步驟 List advisors = findEligibleAdvisors(beanClass, beanName); if (advisors.isEmpty()) { return DO_NOT_PROXY; } return advisors.toArray(); }
protected Object createProxy(Class<?> beanClass, String beanName, Object[] specificInterceptors, TargetSource targetSource) { //對于代理類的創建及處理,交給了ProxyFactory去處理,此函數中只是做一些準備工作 ProxyFactory proxyFactory = new ProxyFactory(); //獲取當前類中相關屬性 proxyFactory.copyFrom(this); if (!shouldProxyTargetClass(beanClass, beanName)) { //形如interface com.lwh.spring.JdkProxy.PersonService Class<?>[] targetInterfaces = ClassUtils.getAllInterfacesForClass(beanClass, this.proxyClassLoader); for (Class<?> targetInterface : targetInterfaces) { //添加代理接口 proxyFactory.addInterface(targetInterface); } } //將攔截器封裝為增強器,此處的攔截器類型就是增強器 Advisor[] advisors = buildAdvisors(beanName, specificInterceptors); for (Advisor advisor : advisors) { //并添加到ProxyFactory中 proxyFactory.addAdvisor(advisor); } //SingletonTargetSource for target object [com.lwh.spring.JdkProxy.PersonServiceImpl@1130520d] proxyFactory.setTargetSource(targetSource); //定制代理,空實現,由子類實現 customizeProxyFactory(proxyFactory); proxyFactory.setFrozen(this.freezeProxy); if (advisorsPreFiltered()) { proxyFactory.setPreFiltered(true); } //創建代理 return proxyFactory.getProxy(this.proxyClassLoader); }
protected Advisor[] buildAdvisors(String beanName, Object[] specificInterceptors) { Advisor[] commonInterceptors = resolveInterceptorNames(); List<Object> allInterceptors = new ArrayList<Object>(); if (specificInterceptors != null) { //加入攔截器 allInterceptors.addAll(Arrays.asList(specificInterceptors)); if (commonInterceptors != null) { if (this.applyCommonInterceptorsFirst) { allInterceptors.addAll(0, Arrays.asList(commonInterceptors)); } else { allInterceptors.addAll(Arrays.asList(commonInterceptors)); } } } Advisor[] advisors = new Advisor[allInterceptors.size()]; for (int i = 0; i < allInterceptors.size(); i++) { //將攔截器轉化為Advisor advisors[i] = this.advisorAdapterRegistry.wrap(allInterceptors.get(i)); } return advisors; }
public Advisor wrap(Object adviceObject) throws UnknownAdviceTypeException { if (adviceObject instanceof Advisor) { return (Advisor) adviceObject; } if (!(adviceObject instanceof Advice)) { throw new UnknownAdviceTypeException(adviceObject); } Advice advice = (Advice) adviceObject; if (advice instanceof MethodInterceptor) { return new DefaultPointcutAdvisor(advice); } for (AdvisorAdapter adapter : this.adapters) { if (adapter.supportsAdvice(advice)) { return new DefaultPointcutAdvisor(advice); } } throw new UnknownAdviceTypeException(advice); }
return proxyFactory.getProxy(this.proxyClassLoader); public Object getProxy(ClassLoader classLoader) { return createAopProxy().getProxy(classLoader); } protected final synchronized AopProxy createAopProxy() { if (!this.active) { activate(); } return getAopProxyFactory().createAopProxy(this); }
//此函數真正完成了代理的創建 public AopProxy createAopProxy(AdvisedSupport config) throws AopConfigException { //三個條件影響Spring的判斷 //1.optimize屬性,不推薦用戶使用這個設置 //2.proxytargetclass屬性,之前分析過 //3.是否存在代理接口 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()) { //使用JDK動態代理 return new JdkDynamicAopProxy(config); } return CglibProxyFactory.createCglibProxy(config); } else { //目標對象實現了接口,默認使用JDK動態代理 return new JdkDynamicAopProxy(config); } }
public Object getProxy(ClassLoader classLoader) { //先是創建代理,再是獲取代理 return createAopProxy().getProxy(classLoader); } public Object getProxy(ClassLoader classLoader) { //獲取代理接口,此處其實就是JDK的動態代理實現了,建議復習下JDK的動態代理 Class[] proxiedInterfaces = AopProxyUtils.completeProxiedInterfaces(this.advised); findDefinedEqualsAndHashCodeMethods(proxiedInterfaces); //返回代理對象 return Proxy.newProxyInstance(classLoader, proxiedInterfaces, this); }
JDK動態代理的關鍵就是實現InvocationHandler方法并實現其invoke方法,而此處的JdkDynamicAopProxy 實現了InvocationHandler接口,所以其中一定有invoke方法. 看筆記<JDK動態代理>得知,獲取到的是代理對象 PersonService personService = ctx.getBean("personService", PersonService.class); 此處實際調用的是生成的代理類中的sayHello方法,看下圖,因為是之前做的筆記,所以包名不一致,但意思很明顯,在代理類中sayHello方法 又會調用InvocationHandler中的invoke方法,此h對象,即InvocationHandler是在Proxy.newProxyInstance時設置進去的,所以下面調用 sayHello方法時先會進入JdkDynamicAopProxy的invoke方法personService.sayHello();
//獲取代理對象 PersonService personService = ctx.getBean("personService", PersonService.class); //方法調用 personService.sayHello();
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { MethodInvocation invocation; Object oldProxy = null; boolean setProxyContext = false; //SingletonTargetSource for target object [com.lwh.spring.JdkProxy.PersonServiceImpl@6986852] TargetSource targetSource = this.advised.targetSource; Class targetClass = null; Object target = null; try { //刪去了部分代碼 Object retVal; //SingletonTargetSource for target object [com.lwh.spring.JdkProxy.PersonServiceImpl@6986852] target = targetSource.getTarget(); if (target != null) { //class com.lwh.spring.JdkProxy.PersonServiceImpl targetClass = target.getClass(); } //獲取當前方法的攔截器鏈 List<Object> chain = this.advised.getInterceptorsAndDynamicInterceptionAdvice(method, targetClass); if (chain.isEmpty()) { //如果鏈為空,直接調用切點方法 retVal = AopUtils.invokeJoinpointUsingReflection(target, method, args); } else { //將攔截器封裝在ReflectiveMethodInvocation中,以便于使用其proceed方法進行鏈接調用 invocation = new ReflectiveMethodInvocation(proxy, target, method, args, targetClass, chain); //執行攔截器鏈調用 retVal = invocation.proceed(); } Class<?> returnType = method.getReturnType(); if (retVal != null && retVal == target && returnType.isInstance(proxy) && !RawTargetAccess.class.isAssignableFrom(method.getDeclaringClass())) { retVal = proxy; } else if (retVal == null && returnType != Void.TYPE && returnType.isPrimitive()) { } return retVal; } finally { } }
public Object proceed() throws Throwable { // We start with an index of -1 and increment early. //執行完所有增強方法后執行切點方法,維護了一個currentInterceptorIndex計數器,遞增 if (this.currentInterceptorIndex == this.interceptorsAndDynamicMethodMatchers.size() - 1) { return invokeJoinpoint(); } //List interceptorsAndDynamicMethodMatchers,記錄了要執行的增強方法,通過下標獲取要執行的增強方法 Object interceptorOrInterceptionAdvice = this.interceptorsAndDynamicMethodMatchers.get(++this.currentInterceptorIndex); if (interceptorOrInterceptionAdvice instanceof InterceptorAndDynamicMethodMatcher) { // Evaluate dynamic method matcher here: static part will already have // been evaluated and found to match. InterceptorAndDynamicMethodMatcher dm = (InterceptorAndDynamicMethodMatcher) interceptorOrInterceptionAdvice; if (dm.methodMatcher.matches(this.method, this.targetClass, this.arguments)) { return dm.interceptor.invoke(this); } else { // Dynamic matching failed. // Skip this interceptor and invoke the next in the chain. return proceed(); } } else { // It's an interceptor, so we just invoke it: The pointcut will have // been evaluated statically before this object was constructed. return ((MethodInterceptor) interceptorOrInterceptionAdvice).invoke(this); } }
感謝各位的閱讀,以上就是“Spring中AOP創建代理的方法”的內容了,經過本文的學習后,相信大家對Spring中AOP創建代理的方法這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。