日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Aware接口

發布時間:2024/4/13 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Aware接口 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、Aware接口用來做什么

在使用Spring時,如果我們想在Bean中使用Spring底層的資源,例如:ApplicationContext,?StringValueResolver等時,該如何得到呢? Aware接口就提供給了我們這個功能.

package org.springframework.beans.factory;/*** A marker superinterface indicating that a bean is eligible to be notified by the* Spring container of a particular framework object through a callback-style method.* The actual method signature is determined by individual subinterfaces but should* typically consist of just one void-returning method that accepts a single argument.** <p>Note that merely implementing {@link Aware} provides no default functionality.* Rather, processing must be done explicitly, for example in a* {@link org.springframework.beans.factory.config.BeanPostProcessor}.* Refer to {@link org.springframework.context.support.ApplicationContextAwareProcessor}* for an example of processing specific {@code *Aware} interface callbacks.** @author Chris Beams* @author Juergen Hoeller* @since 3.1*/ public interface Aware {}

二、一個例子

Spring框架中的資源太多了,這里就以獲取ApplicationContextAware,EmbeddedValueResolverAware,?BeanNameAware為例,先感性了解一下XxxAware的作用,然后再去看看它是怎么實現的.

1. ApplicationContextAware接口

需要去實現setApplicationContext方法來完成對ApplicationContext的獲取(已經通過形參的方式提供了).

在對Bean進行初始化之前,會通過回調的方式調用setApplicationContext方法.

public interface ApplicationContextAware extends Aware {/*** Set the ApplicationContext that this object runs in.* Normally this call will be used to initialize the object.* <p>Invoked after population of normal bean properties but before an init callback such*/void setApplicationContext(ApplicationContext applicationContext) throws BeansException; }

2. EmbeddedValueResolverAware接口

需要去實現setEmbeddedValueResolver方法來完成對StringValueResolver的獲取(已經通過形參的方式提供了).

在對Bean進行初始化之前,會通過回調的方式調用setEmbeddedValueResolver方法.

public interface EmbeddedValueResolverAware extends Aware {/*** Set the StringValueResolver to use for resolving embedded definition values.*/void setEmbeddedValueResolver(StringValueResolver resolver);}

3. BeanNameAware接口

在對Bean注冊之后,會調用該方法.?name為這個bean在factory中的名字.

public interface BeanNameAware extends Aware {/*** Set the name of the bean in the bean factory that created this bean.* <p>Invoked after population of normal bean properties but before an*/void setBeanName(String name);}

對上面三個接口的使用如下:

package czf.bean;import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanNameAware; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.context.EmbeddedValueResolverAware; import org.springframework.stereotype.Component; import org.springframework.util.StringValueResolver;/*** @author czf* @Date 2020/4/27 8:52 下午*/ @Component() public class TryAware implements ApplicationContextAware, EmbeddedValueResolverAware, BeanNameAware {private ApplicationContext applicationContext;private StringValueResolver stringValueResolver;@Overridepublic void setBeanName(String name) {System.out.println("注入了"+name);}@Overridepublic void setApplicationContext(ApplicationContext applicationContext) throws BeansException {// 進行setthis.applicationContext = applicationContext;System.out.println("設置完畢applicationContext");}@Overridepublic void setEmbeddedValueResolver(StringValueResolver resolver) {// 進行setthis.stringValueResolver = resolver;// 對其使用System.out.println("解析:"+resolver.resolveStringValue("用戶名字:${os.masterName}, 年齡:#{20+2}, 系統名稱:${os.name}"));} }

三、通過XxxAwareProcessor回調實現

每一個XxxAware都會有對應的一個XxxAwareProcessor來對其進行回調,而XxxAwareProcessor實現了BeanPostProcessor接口,會在Bean的初始化前后來完成對XxxAware的回調. 下面以ApplicationContextAware接口為例子.

class ApplicationContextAwareProcessor implements BeanPostProcessor {private final ConfigurableApplicationContext applicationContext;private final StringValueResolver embeddedValueResolver;/*** Create a new ApplicationContextAwareProcessor for the given context.*/public ApplicationContextAwareProcessor(ConfigurableApplicationContext applicationContext) {this.applicationContext = applicationContext;this.embeddedValueResolver = new EmbeddedValueResolver(applicationContext.getBeanFactory());}/*** 重點!!!!* 會在Bean初始化之前調用這個方法!!!* 在這個方法中,調用invokeAwareInterfaces(bean)** 然后在invokeAwareInterfaces(bean)中,通過判斷bean是什么類型的,* 來對Aware進行強轉XxxAware,并調用其接口方法.** @param bean the new bean instance* @param beanName the name of the bean* @return* @throws BeansException*/@Override@Nullablepublic Object postProcessBeforeInitialization(final Object bean, String beanName) throws BeansException {AccessControlContext acc = null;if (System.getSecurityManager() != null &&(bean instanceof EnvironmentAware || bean instanceof EmbeddedValueResolverAware ||bean instanceof ResourceLoaderAware || bean instanceof ApplicationEventPublisherAware ||bean instanceof MessageSourceAware || bean instanceof ApplicationContextAware)) {acc = this.applicationContext.getBeanFactory().getAccessControlContext();}// 權限檢查if (acc != null) {AccessController.doPrivileged((PrivilegedAction<Object>) () -> {invokeAwareInterfaces(bean);return null;}, acc);}else {// 在這里~~~~~~~~~~~invokeAwareInterfaces(bean);}return bean;}/*** 回調實現邏輯:* 通過判斷Aware是什么類型,然后將Aware強轉成對應類型并進行方法的調用* @param bean*/private void invokeAwareInterfaces(Object bean) {if (bean instanceof Aware) {if (bean instanceof EnvironmentAware) {((EnvironmentAware) bean).setEnvironment(this.applicationContext.getEnvironment());}if (bean instanceof EmbeddedValueResolverAware) {((EmbeddedValueResolverAware) bean).setEmbeddedValueResolver(this.embeddedValueResolver);}if (bean instanceof ResourceLoaderAware) {((ResourceLoaderAware) bean).setResourceLoader(this.applicationContext);}if (bean instanceof ApplicationEventPublisherAware) {((ApplicationEventPublisherAware) bean).setApplicationEventPublisher(this.applicationContext);}if (bean instanceof MessageSourceAware) {((MessageSourceAware) bean).setMessageSource(this.applicationContext);}//spring幫你set一個applicationContext對象//所以當我們自己的一個對象實現了ApplicationContextAware對象只需要提供setter就能得到applicationContext對象if (bean instanceof ApplicationContextAware) {((ApplicationContextAware) bean).setApplicationContext(this.applicationContext);}}}@Overridepublic Object postProcessAfterInitialization(Object bean, String beanName) {return bean;}}

更清楚的調用鏈:

上圖倒數第二步中,遍歷Processor的樣子:
?

?

總結

以上是生活随笔為你收集整理的Aware接口的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 国产亚洲精品美女久久久久 | 欧美日韩不卡视频 | 精品视频一二三 | 99re免费视频精品全部 | 丁香花婷婷 | 在线观看h视频 | 久久久久国产精品无码免费看 | av在线资源站| h狠狠躁死你h高h | av中文网站| 久久久欧美精品sm网站 | 日韩夜夜高潮夜夜爽无码 | 人人干av| 天堂av一区| 台湾佬美性中文娱乐 | 欧美岛国国产 | 亚洲三级黄色 | 性欧美8khd高清极品 | 天天撸天天操 | 日韩在线观看一区 | 色婷婷综合在线 | www黄色com| www.avcao | 一区二区三区四区在线播放 | 中文字幕精品视频在线 | 给我免费观看片在线电影的 | 尤物视频最新网址 | 狠狠的干狠狠的操 | 91免费国产 | 人人狠狠 | 婷婷777 | 老女人综合网 | 国产毛片在线视频 | 私人午夜影院 | 一级成人免费视频 | 少妇色视频| 国产传媒欧美日韩 | 成人一级影片 | 永久免费未满视频 | 自拍视频一区 | 一区二区三区四区视频在线观看 | 亚洲欧美一区二区三 | 亚洲精品国产精品国自产网站 | 成年人免费观看视频网站 | 亚洲 欧美 激情 小说 另类 | 午夜影院在线观看免费 | 性xxxxbbbb | 国产啊啊啊啊 | 国产女人18水真多18精品一级做 | 国产sm在线观看 | 欧美日韩精品 | av中文字幕网 | 天天操穴 | 亚洲永久精品国产 | 国产精品无码久久久久久电影 | 精品国产99 | 欧洲亚洲精品 | 久久这里只有精品首页 | 日日爱669 | 亚洲视频黄 | 伊人免费视频 | 超碰av男人的天堂 | 国产高中女学生第一次 | 天天天天射 | 日韩欧美精品久久 | 乡村性满足hd | 91国产精品 | 夜夜夜综合 | 青青草手机在线 | 人人爱人人射 | 亚洲综合久久婷婷 | 日韩视频免费在线观看 | 99久久免费国产精精品 | 性免费视频 | 女人看黄色网 | 丰满人妻一区二区三区在线 | www.jizzjizz| 超碰美女在线 | 久久另类ts人妖一区二区 | 女人的毛片 | 亚洲制服一区二区 | 久久久香蕉网 | 天天干天天碰 | 精品乱码一区二区三四区视频 | 淫僧荡尼巨乳(h)小说 | 欧美.com| 中出少妇| 久久久久久激情 | 国产精品乱码久久久久久 | 精品999久久久| 日韩免费中文字幕 | 国产三级三级在线观看 | 亚洲欧美中文字幕5发布 | 国产精品高潮呻吟久久久 | 6080福利 | 久久成人一区 | 婷婷中文字幕在线 | 色图插插插 | 丰满少妇xoxoxo视频 |