{"id":10008049,"date":"2023-07-06T15:02:41","date_gmt":"2023-07-06T07:02:41","guid":{"rendered":"https:\/\/ityet.com\/?p=10008049"},"modified":"2023-07-06T15:02:41","modified_gmt":"2023-07-06T07:02:41","slug":"springioc%e5%ae%b9%e5%99%a8%e4%b9%8baware-%e4%ba%ac%e4%b8%9c%e4%ba%91%e6%8a%80%e6%9c%af%e5%9b%a2%e9%98%9f","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10008049","title":{"rendered":"SpringIoc\u5bb9\u5668\u4e4bAware | \u4eac\u4e1c\u4e91\u6280\u672f\u56e2\u961f"},"content":{"rendered":"<h2>1 \u524d\u8a00<\/h2>\n<p>Aware\u662fSpring\u63d0\u4f9b\u7684\u4e00\u4e2a\u6807\u8bb0\u8d85\u63a5\u53e3\uff0c\u6307\u793abean\u6709\u8d44\u683c\u901a\u8fc7\u56de\u8c03\u6837\u5f0f\u7684\u65b9\u6cd5\u7531Spring\u5bb9\u5668\u901a\u77e5\u7279\u5b9a\u7684\u6846\u67b6\u5bf9\u8c61\uff0c\u4ee5\u83b7\u53d6\u5230\u5bb9\u5668\u4e2d\u7279\u6709\u5bf9\u8c61\u7684\u5b9e\u4f8b\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002\u5b9e\u9645\u7684\u65b9\u6cd5\u7b7e\u540d\u7531\u5404\u4e2a\u5b50\u63a5\u53e3\u786e\u5b9a\uff0c\u4f46\u901a\u5e38\u53ea\u5305\u542b\u4e00\u4e2a\u63a5\u53d7\u5355\u4e2a\u53c2\u6570\u7684void\u8fd4\u56de\u65b9\u6cd5\u3002<\/p>\n<h2>2 Spring\u4e2d9\u4e2aAware\u5185\u7f6e\u5b9e\u73b0<\/h2>\n<pre><code>|--Aware\n    |--BeanNameAware\n    |--BeanClassLoaderAware\n    |--BeanFactoryAware\n    |--EnvironmentAware\n    |--EmbeddedValueResolverAware\n    |--ResourceLoaderAware\n    |--ApplicationEventPublisherAware\n    |--MessageSourceAware\n    |--ApplicationContextAware\n\n\n<\/code><\/pre>\n<p>9\u4e2a\u5185\u7f6e\u5b9e\u73b0\u53c8\u5206\u4e24\u7c7b\uff0c\u524d\u4e09\u4e2a\u4e3a\u76f4\u63a5\u8c03\u7528\uff0c\u540e6\u4e2a\u901a\u8fc7ApplicationContextAwareProcessor\u540e\u7f6e\u5904\u7406\u5668\uff0c\u95f4\u63a5\u56de\u8c03<\/p>\n<h3>2.1 BeanNameAware<\/h3>\n<pre><code>public interface BeanNameAware extends Aware {\n\n       \uff0f**\n        *\u8bbe\u7f6e\u521b\u5efa\u6b64bean\u7684bean\u5de5\u5382\u4e2d\u7684bean\u7684\u540d\u79f0\u3002\n        *\u5728\u666e\u901abean\u5c5e\u6027\u586b\u5145\u4e4b\u540e\u4f46\u5728\n        *\u521d\u59cb\u5316\u4e4b\u524d\u56de\u8c03\uff0c\u5982{@link InitializingBean#afterPropertiesSet()}\n        *\u6216\u81ea\u5b9a\u4e49\u521d\u59cb\u5316\u65b9\u6cd5\u3002\n        * @param name\u5de5\u5382\u4e2dbean\u7684\u540d\u79f0\u3002\n        *\u6ce8\u610f\uff0c\u6b64\u540d\u79f0\u662f\u5de5\u5382\u4e2d\u4f7f\u7528\u7684\u5b9e\u9645bean\u540d\u79f0\uff0c\u8fd9\u53ef\u80fd\n        *\u4e0e\u6700\u521d\u6307\u5b9a\u7684\u540d\u79f0\u4e0d\u540c:\u7279\u522b\u662f\u5bf9\u4e8e\u5185\u90e8bean\n        * names\uff0c\u5b9e\u9645\u7684bean\u540d\u79f0\u53ef\u4ee5\u901a\u8fc7\u6dfb\u52a0\n        *\u201c#\u2026\u201d\u540e\u7f00\u3002\u4f7f\u7528{@link BeanFactoryUtils#originalBeanName(String)}\n        *\u65b9\u6cd5\u63d0\u53d6\u539f\u59cbbean\u540d\u79f0(\u4e0d\u5e26\u540e\u7f00)\uff0c\u5982\u679c\u9700\u8981\u7684\u8bdd\u3002\n        * \/\n    void setBeanName(String name);\n\n}\n\n\n<\/code><\/pre>\n<p>\u5b9e\u73b0BeanNameAware\u63a5\u53e3\u9700\u8981\u5b9e\u73b0setBeanName()\u65b9\u6cd5\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u53ea\u662f\u7b80\u5355\u7684\u8fd4\u56de\u6211\u4eec\u5f53\u524d\u7684beanName\uff0c\u8fd9\u4e2a\u63a5\u53e3\u8868\u9762\u4e0a\u7684\u4f5c\u7528\u5c31\u662f\u8ba9\u5b9e\u73b0\u8fd9\u4e2a\u63a5\u53e3\u7684bean\u77e5\u9053\u81ea\u5df1\u5728spring\u5bb9\u5668\u91cc\u7684\u540d\u5b57\uff0c\u800c\u4e14\u5b98\u65b9\u7684\u610f\u601d\u662f\u8fd9\u4e2a\u63a5\u53e3\u66f4\u591a\u7684\u4f7f\u7528\u5728spring\u7684\u6846\u67b6\u4ee3\u7801\u4e2d\uff0c\u5b9e\u9645\u5f00\u53d1\u73af\u5883\u5e94\u8be5\u4e0d\u5efa\u8bae\u4f7f\u7528\uff0c\u56e0\u4e3aspring\u8ba4\u4e3abean\u7684\u540d\u5b57\u4e0ebean\u7684\u8054\u7cfb\u5e76\u4e0d\u662f\u5f88\u6df1\uff0c\uff08\u7684\u786e\uff0c\u629b\u5f00spring API\u800c\u8a00\uff0c\u6211\u4eec\u5982\u679c\u83b7\u53d6\u4e86\u8be5bean\u7684\u540d\u5b57\uff0c\u5176\u5b9e\u610f\u4e49\u4e0d\u662f\u5f88\u5927\uff0c\u6211\u4eec\u6ca1\u6709\u83b7\u53d6\u8be5bean\u7684class\uff0c\u53ea\u6709\u8be5bean\u7684\u540d\u5b57\uff0c\u6211\u4eec\u4e5f\u65e0\u4ece\u4e0b\u624b\uff0c\u76f8\u53cd\uff0c\u56e0\u4e3abean\u7684\u540d\u79f0\u5728spring\u5bb9\u5668\u4e2d\u53ef\u80fd\u662f\u8be5bean\u7684\u552f\u4e00\u6807\u8bc6\uff0c\u4e5f\u5c31\u662f\u8bf4\u518dbeanDefinitionMap\u4e2d\uff0ckey\u503c\u5c31\u662f\u8fd9\u4e2aname\uff0cspring\u53ef\u4ee5\u6839\u636e\u8fd9\u4e2akey\u503c\u83b7\u53d6\u8be5bean\u7684\u6240\u6709\u7279\u6027\uff09\u6240\u4ee5spring\u8bf4\u8fd9\u4e2a\u4e0d\u662f\u975e\u5fc5\u8981\u7684\u4f9d\u8d56\u3002<\/p>\n<h3>2.2 BeanClassLoaderAware<\/h3>\n<pre><code>public interface BeanClassLoaderAware extends Aware {\n\n   \uff0f**\n    *\u63d0\u4f9bbean {@link ClassLoader}\u7c7b\u52a0\u8f7d\u5668\u7684\u56de\u8c03\n    *\u4e00\u4e2abean\u5b9e\u4f8b\u5728\u5c5e\u6027\u7684\u586b\u5145\u4e4b\u540e\u4f46\u5728\u521d\u59cb\u5316\u56de\u8c03\u4e4b\u524d\u8c03\u7528\n    * {@link InitializingBean\n    * {@link InitializingBean#afterPropertiesSet()}\n    *\u65b9\u6cd5\u6216\u81ea\u5b9a\u4e49\u521d\u59cb\u5316\u65b9\u6cd5\u3002\n    * @param\u7c7b\u52a0\u8f7d\u5668\u62e5\u6709\u7684\u7c7b\u52a0\u8f7d\u5668;\u53ef\u80fd\u662f{@code null}\u5728\u4f8b\u5982\uff0c\u5fc5\u987b\u4f7f\u7528\u9ed8\u8ba4\u7684{@code ClassLoader}\n    * \u83b7\u53d6\u7684{@code ClassLoader}\n    * {@link org.springframework.util.ClassUtils#getDefaultClassLoader()}\n    * \/\n   void setBeanClassLoader(ClassLoader classLoader);\n\n}\n\n\n<\/code><\/pre>\n<p>\u5728bean\u5c5e\u6027\u586b\u5145\u4e4b\u540e\u521d\u59cb\u5316\u4e4b\u524d\uff0c\u63d0\u4f9b\u7c7b\u52a0\u5236\u5668\u7684\u56de\u8c03\u3002\u8ba9\u53d7\u7ba1Bean\u672c\u8eab\u77e5\u9053\u5b83\u662f\u7531\u54ea\u4e00\u7c7b\u88c5\u8f7d\u5668\u8d1f\u8d23\u88c5\u8f7d\u7684\u3002<\/p>\n<h3>2.3 BeanFactoryAware<\/h3>\n<pre><code>public interface BeanFactoryAware extends Aware {\n\n   \uff0f**\n    * \u4e3abean\u5b9e\u4f8b\u63d0\u4f9b\u6240\u5c5e\u5de5\u5382\u7684\u56de\u8c03\u3002\n    * \u5728\u666e\u901abean\u5c5e\u6027\u586b\u5145\u4e4b\u540e\u8c03\u7528\u4f46\u5728\u521d\u59cb\u5316\u56de\u8c03\u4e4b\u524d\uff0c\u5982\n    * {@link InitializingBean#afterPropertiesSet()}\u6216\u81ea\u5b9a\u4e49\u521d\u59cb\u5316\u65b9\u6cd5\u3002\n    * @param beanFactory\u62e5\u6709beanFactory(\u975e\u7a7a)\u3002bean\u53ef\u4ee5\u7acb\u5373\u8c03\u7528\u5de5\u5382\u4e0a\u7684\u65b9\u6cd5\u3002\n    * @\u5728\u521d\u59cb\u5316\u9519\u8bef\u65f6\u629b\u51faBeansException\n    * @\u53c2\u89c1BeanInitializationException\n    * \/\n   void setBeanFactory(BeanFactory beanFactory) throws BeansException;\n\n}\n\n\n<\/code><\/pre>\n<p>\u5728bean\u5c5e\u6027\u586b\u5145\u4e4b\u540e\u521d\u59cb\u5316\u4e4b\u524d\uff0c\u63d0bean\u5de5\u5382\u7684\u56de\u8c03\u3002\u5b9e\u73b0 BeanFacto\u03b7Aware \u63a5\u53e3\u7684 bean \u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee Spring \u5bb9\u5668\uff0c\u88ab\u5bb9\u5668\u521b\u5efa\u4ee5\u540e\uff0c\u5b83\u4f1a\u62e5\u6709\u4e00\u4e2a\u6307\u5411 Spring \u5bb9\u5668\u7684\u5f15\u7528\uff0c\u53ef\u4ee5\u5229\u7528\u8be5bean\u6839\u636e\u4f20\u5165\u53c2\u6570\u52a8\u6001\u83b7\u53d6\u88abspring\u5de5\u5382\u52a0\u8f7d\u7684bean<\/p>\n<h3>2.4 EnvironmentAware<\/h3>\n<pre><code>public interface EnvironmentAware extends Aware {\n\n   \/**\n    * \u8bbe\u7f6e\u8be5\u5bf9\u8c61\u8fd0\u884c\u7684{@code\u73af\u5883}\u3002\n    *\/\n   void setEnvironment(Environment environment);\n\n}\n\n\n<\/code><\/pre>\n<p>\u8bbe\u7f6e\u8be5\u5bf9\u8c61\u8fd0\u884c\u7684\u3002\u6240\u6709\u6ce8\u518c\u5230 Spring\u5bb9\u5668\u5185\u7684 bean\uff0c\u53ea\u8981\u8be5bean \u5b9e\u73b0\u4e86 EnvironmentAware\u63a5\u53e3\uff0c\u5e76\u4e14\u8fdb\u884c\u91cd\u5199\u4e86setEnvironment\u65b9\u6cd5\u7684\u60c5\u51b5\u4e0b\uff0c\u90a3\u4e48\u5728\u5de5\u7a0b\u542f\u52a8\u65f6\u5c31\u53ef\u4ee5\u83b7\u53d6\u5f97 application.properties \u7684\u914d\u7f6e\u6587\u4ef6\u914d\u7f6e\u7684\u5c5e\u6027\u503c\uff0c\u8fd9\u6837\u5c31\u4e0d\u7528\u6211\u4eec\u5c06\u9b54\u6cd5\u503c\u5199\u5230\u4ee3\u7801\u91cc\u9762\u4e86\u3002<\/p>\n<h3>2.5 EmbeddedValueResolverAware<\/h3>\n<pre><code>public interface EmbeddedValueResolverAware extends Aware {\n\n   \/**\n    * \u8bbe\u7f6eStringValueResolver\u7528\u4e8e\u89e3\u6790\u5d4c\u5165\u7684\u5b9a\u4e49\u503c\u3002\n    *\/\n   void setEmbeddedValueResolver(StringValueResolver resolver);\n\n}\n\n\n<\/code><\/pre>\n<p>\u5728\u57fa\u4e8eSpring\u83b7\u53d6properties\u6587\u4ef6\u5c5e\u6027\u503c\u7684\u65f6\u5019\uff0c\u4e00\u822c\u4f7f\u7528@Value\u7684\u65b9\u5f0f\u6ce8\u5165\u914d\u7f6e\u6587\u4ef6\u5c5e\u6027\u503c\uff0c\u4f46\u662f@Value\u5fc5\u987b\u8981\u5728Spring\u7684Bean\u751f\u547d\u5468\u671f\u7ba1\u7406\u4e0b\u624d\u80fd\u4f7f\u7528\uff0c\u6bd4\u5982\u7c7b\u88ab@Controller\u3001@Service\u3001@Component\u7b49\u6ce8\u89e3\u6807\u6ce8\u3002\u5982\u6709\u7684\u62bd\u8c61\u7c7b\u4e2d\uff0c\u57fa\u4e8eSpring\u89e3\u6790@Value\u7684\u65b9\u5f0f\uff0c\u4f7f\u7528EmbeddedValueResolverAware\u89e3\u6790\u914d\u7f6e\u6587\u4ef6\u6765\u5b9e\u73b0\u3002<\/p>\n<h3>2.6 ResourceLoaderAware<\/h3>\n<pre><code>public interface ResourceLoaderAware extends Aware {\n\n   \uff0f**\n    *\u8bbe\u7f6e\u8be5\u5bf9\u8c61\u8fd0\u884c\u7684ResourceLoader\u3002\u8fd9\u53ef\u80fd\u662f\u4e00\u4e2aResourcePatternResolver\uff0c\u5b83\u53ef\u4ee5\u88ab\u68c0\u67e5\n    *\u901a\u8fc7{@code instanceof ResourcePatternResolver}\u3002\u53e6\u8bf7\u53c2\u9605\n    * {@code ResourcePatternUtils\u3002getResourcePatternResolver}\u65b9\u6cd5\u3002\n    * &lt;p&gt;\u5728\u586b\u5145\u666e\u901abean\u5c5e\u6027\u4e4b\u540e\u4f46\u5728init\u56de\u8c03\u4e4b\u524d\u8c03\u7528\n    *\u50cfInitializingBean\u7684{@code afterPropertiesSet}\u6216\u81ea\u5b9a\u4e49\u521d\u59cb\u5316\u65b9\u6cd5\u3002\n    *\u5728ApplicationContextAware\u7684{@code setApplicationContext}\u4e4b\u524d\u8c03\u7528\u3002\n    * @param resourceLoader\u8be5\u5bf9\u8c61\u4f7f\u7528\u7684resourceLoader\u5bf9\u8c61\n    * @ @ springframework.core. io.support.resourcepatternresolver\n    * @ @ resourcepatternutils #\u83b7\u53d6resourcepatternresolver\n    * \/\n   void setResourceLoader(ResourceLoader resourceLoader);\n\n}\n\n\n<\/code><\/pre>\n<p>ResourceLoaderAware \u662f\u7279\u6b8a\u7684\u6807\u8bb0\u63a5\u53e3\uff0c\u5b83\u5e0c\u671b\u62e5\u6709\u4e00\u4e2a ResourceLoader \u5f15\u7528\u7684\u5bf9\u8c61\u3002\u5f53\u5b9e\u73b0\u4e86 ResourceLoaderAware\u63a5\u53e3\u7684\u7c7b\u90e8\u7f72\u5230application context(\u6bd4\u5982\u53d7Spring\u7ba1\u7406\u7684bean)\u4e2d\u65f6\uff0c\u5b83\u4f1a\u88abapplication context\u8bc6\u522b\u4e3a ResourceLoaderAware\u3002 \u63a5\u7740application context\u4f1a\u8c03\u7528setResourceLoader(ResourceLoader)\u65b9\u6cd5\uff0c\u5e76\u628a\u81ea\u8eab\u4f5c\u4e3a\u53c2\u6570\u4f20\u5165\u8be5\u65b9\u6cd5(\u8bb0\u4f4f\uff0c\u6240\u6709Spring\u91cc\u7684application context\u90fd\u5b9e\u73b0\u4e86ResourceLoader\u63a5\u53e3)\u3002<\/p>\n<p>\u65e2\u7136 ApplicationContext \u5c31\u662fResourceLoader\uff0c\u90a3\u4e48\u8be5bean\u5c31\u53ef\u4ee5\u5b9e\u73b0 ApplicationContextAware\u63a5\u53e3\u5e76\u76f4\u63a5\u4f7f\u7528\u6240\u63d0\u4f9b\u7684application context\u6765\u8f7d\u5165\u8d44\u6e90\uff0c\u4f46\u662f\u901a\u5e38\u66f4\u9002\u5408\u4f7f\u7528\u7279\u5b9a\u7684\u6ee1\u8db3\u6240\u6709\u9700\u8981\u7684 ResourceLoader \u5b9e\u73b0\u3002 \u8fd9\u6837\u4e00\u6765\uff0c\u4ee3\u7801\u53ea\u9700\u8981\u4f9d\u8d56\u4e8e\u53ef\u4ee5\u770b\u4f5c\u8f85\u52a9\u63a5\u53e3\u7684\u8d44\u6e90\u8f7d\u5165\u63a5\u53e3\uff0c\u800c\u4e0d\u7528\u4f9d\u8d56\u4e8e\u6574\u4e2aSpring ApplicationContext \u63a5\u53e3\u3002<\/p>\n<h3>2.7 ApplicationEventPublisherAware<\/h3>\n<pre><code>public interface ApplicationEventPublisherAware extends Aware {\n\n   \uff0f**\n    *\u8bbe\u7f6e\u8be5\u5bf9\u8c61\u8fd0\u884c\u7684ApplicationEventPublisher\u3002\n    * &lt;p&gt;\u5728\u666e\u901abean\u5c5e\u6027\u586b\u5145\u4e4b\u540e\u4f46\u5728init\u4e4b\u524d\u8c03\u7528\u50cfInitializingBean\u7684afterPropertiesSet\u6216\u81ea\u5b9a\u4e49\u521d\u59cb\u5316\u65b9\u6cd5\u3002\n    *\u5728ApplicationContextAware\u7684setApplicationContext\u4e4b\u524d\u8c03\u7528\u3002\n    *\u8be5\u5bf9\u8c61\u4f7f\u7528\u7684\u4e8b\u4ef6\u53d1\u5e03\u8005\n    * \/\n   void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher);\n\n}\n\n\n<\/code><\/pre>\n<p>ApplicationEventPublisherAware \u662f\u7531 Spring \u63d0\u4f9b\u7684\u7528\u4e8e\u4e3a Service \u6ce8\u5165 ApplicationEventPublisher \u4e8b\u4ef6\u53d1\u5e03\u5668\u7684\u63a5\u53e3\uff0c\u4f7f\u7528\u8fd9\u4e2a\u63a5\u53e3\uff0c\u6211\u4eec\u81ea\u5df1\u7684 Service \u5c31\u62e5\u6709\u4e86\u53d1\u5e03\u4e8b\u4ef6\u7684\u80fd\u529b\u3002<\/p>\n<h3>2.8 MessageSourceAware<\/h3>\n<pre><code>public interface MessageSourceAware extends Aware {\n\n   \uff0f**\n    *\u8bbe\u7f6e\u8be5\u5bf9\u8c61\u8fd0\u884c\u7684MessageSource\u3002\n    * &lt;p&gt;\u5728\u666e\u901abean\u5c5e\u6027\u586b\u5145\u4e4b\u540e\u4f46\u5728init\u4e4b\u524d\u8c03\u7528\u50cfInitializingBean\u7684afterPropertiesSet\u6216\u81ea\u5b9a\u4e49\u521d\u59cb\u5316\u65b9\u6cd5\u3002\n    *\u5728ApplicationContextAware\u7684setApplicationContext\u4e4b\u524d\u8c03\u7528\u3002\n    * @param messageSource\u6d88\u606f\u6e90\n    * \/\n   void setMessageSource(MessageSource messageSource);\n\n}\n\n\n<\/code><\/pre>\n<p>\u83b7\u5f97message source\u8fd9\u6837\u53ef\u4ee5\u83b7\u5f97\u6587\u672c\u4fe1\u606f\uff0c\u4f7f\u7528\u573a\u666f\u5982\u4e3a\u4e86\u56fd\u9645\u5316\u3002<\/p>\n<h3>2.9 ApplicationContextAware<\/h3>\n<pre><code>public interface ApplicationContextAware extends Aware {\n\n   \uff0f**\n    *\u8bbe\u7f6e\u8be5\u5bf9\u8c61\u8fd0\u884c\u7684ApplicationContext\u3002\u901a\u5e38\u8fd9\u4e2a\u8c03\u7528\u5c06\u7528\u4e8e\u521d\u59cb\u5316\u5bf9\u8c61\u3002\n    * &lt;p&gt;\u5728\u666e\u901abean\u5c5e\u6027\u586b\u5145\u4e4b\u540e\u4f46\u5728init\u56de\u8c03\u4e4b\u524d\u8c03\u7528\n    *\u4f5c\u4e3a{@link org.springframework.beans.factory.InitializingBean#afterPropertiesSet()}\n    *\u6216\u81ea\u5b9a\u4e49\u521d\u59cb\u5316\u65b9\u6cd5\u3002\u5728{@link ResourceLoaderAware#setResourceLoader}\u4e4b\u540e\u8c03\u7528\uff0c\n    * {@link ApplicationEventPublisherAware#setApplicationEventPublisher}\u548c\n    * {@link MessageSourceAware}\uff0c\u5982\u679c\u9002\u7528\u3002\n    * @param applicationContext\u8be5\u5bf9\u8c61\u5c06\u4f7f\u7528\u7684applicationContext\u5bf9\u8c61\n    * @\u5728\u4e0a\u4e0b\u6587\u521d\u59cb\u5316\u9519\u8bef\u65f6\u629b\u51faApplicationContextException\u5982\u679c\u7531\u5e94\u7528\u7a0b\u5e8f\u4e0a\u4e0b\u6587\u65b9\u6cd5\u629b\u51fa\uff0c\u5219\u629b\u51faBeansException\n    * @see org.springframework.beans.factory.BeanInitializationException\n    * \/\n   void setApplicationContext(ApplicationContext applicationContext) throws BeansException;\n\n}\n\n\n<\/code><\/pre>\n<p>ApplicationContextAware\u7684\u4f5c\u7528\u662f\u53ef\u4ee5\u65b9\u4fbf\u83b7\u53d6Spring\u5bb9\u5668ApplicationContext\uff0c\u4ece\u800c\u53ef\u4ee5\u83b7\u53d6\u5bb9\u5668\u5185\u7684Bean\u3002ApplicationContextAware\u63a5\u53e3\u53ea\u6709\u4e00\u4e2a\u65b9\u6cd5\uff0c\u5982\u679c\u5b9e\u73b0\u4e86\u8fd9\u4e2a\u65b9\u6cd5\uff0c\u90a3\u4e48Spring\u521b\u5efa\u8fd9\u4e2a\u5b9e\u73b0\u7c7b\u7684\u65f6\u5019\u5c31\u4f1a\u81ea\u52a8\u6267\u884c\u8fd9\u4e2a\u65b9\u6cd5\uff0c\u628aApplicationContext\u6ce8\u5165\u5230\u8fd9\u4e2a\u7c7b\u4e2d\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0cspring \u5728\u542f\u52a8\u7684\u65f6\u5019\u5c31\u9700\u8981\u5b9e\u4f8b\u5316\u8fd9\u4e2a class\uff08\u5982\u679c\u662f\u61d2\u52a0\u8f7d\u5c31\u662f\u4f60\u9700\u8981\u7528\u5230\u7684\u65f6\u5019\u5b9e\u4f8b\u5316\uff09\uff0c\u5728\u5b9e\u4f8b\u5316\u8fd9\u4e2a class \u7684\u65f6\u5019\uff0c\u53d1\u73b0\u5b83\u5305\u542b\u8fd9\u4e2a ApplicationContextAware \u63a5\u53e3\u7684\u8bdd\uff0csping \u5c31\u4f1a\u8c03\u7528\u8fd9\u4e2a\u5bf9\u8c61\u7684 setApplicationContext \u65b9\u6cd5\uff0c\u628a applicationContext Set \u8fdb\u53bb\u4e86\u3002<\/p>\n<h2>3 Spring\u4e2d\u8c03\u7528\u65f6\u673a<\/h2>\n<p>Aware\u63a5\u53e3\u7531Spring\u5728AbstractAutowireCapableBeanFactory.initializeBean(beanName, bean,mbd)\u65b9\u6cd5\u4e2d\u901a\u8fc7\u8c03\u7528invokeAwareMethods(beanName, bean)\u65b9\u6cd5\u548capplyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName)\u89e6\u53d1Aware\u65b9\u6cd5\u7684\u8c03\u7528<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043976986\" alt=\"image.png\" title=\"image.png\" loading=\"lazy\"><\/p>\n<h3>3.1 invokeAwareMethods<\/h3>\n<pre><code>private void invokeAwareMethods(final String beanName, final Object bean) {\n   if (bean instanceof Aware) {\n      if (bean instanceof BeanNameAware) {\n         ((BeanNameAware) bean).setBeanName(beanName);\n      }\n      if (bean instanceof BeanClassLoaderAware) {\n         ((BeanClassLoaderAware) bean).setBeanClassLoader(getBeanClassLoader());\n      }\n      if (bean instanceof BeanFactoryAware) {\n         ((BeanFactoryAware) bean).setBeanFactory(AbstractAutowireCapableBeanFactory.this);\n      }\n   }\n}\n\n\n<\/code><\/pre>\n<p>\u5224\u65ad\u5e76\u76f4\u63a5\u56de\u8c03<\/p>\n<h3>3.2 applyBeanPostProcessorsBeforeInitialization<\/h3>\n<pre><code>public Object applyBeanPostProcessorsBeforeInitialization(Object existingBean, String beanName)\n      throws BeansException {\n\n   Object result = existingBean;\n   for (BeanPostProcessor beanProcessor : getBeanPostProcessors()) {\n      result = beanProcessor.postProcessBeforeInitialization(result, beanName);\n      if (result == null) {\n         return result;\n      }\n   }\n   return result;\n}\n\n\n<\/code><\/pre>\n<p>\u901a\u8fc7ApplicationContextAwareProcessor.postProcessBeforeInitialization(Object bean, String beanName)\u95f4\u63a5\u8c03\u7528\uff0c\u5e76\u5728\u65b9\u6cd5invokeAwareInterfaces\u4e2d\u8fdb\u884c\u56de\u8c03\u3002<\/p>\n<pre><code>public Object postProcessBeforeInitialization(final Object bean, String beanName) throws BeansException {\n   AccessControlContext acc = null;\n\n   if (System.getSecurityManager() != null &amp;&amp;\n         (bean instanceof EnvironmentAware || bean instanceof EmbeddedValueResolverAware ||\n               bean instanceof ResourceLoaderAware || bean instanceof ApplicationEventPublisherAware ||\n               bean instanceof MessageSourceAware || bean instanceof ApplicationContextAware)) {\n      acc = this.applicationContext.getBeanFactory().getAccessControlContext();\n   }\n\n   if (acc != null) {\n      AccessController.doPrivileged(new PrivilegedAction&lt;Object&gt;() {\n         @Override\n         public Object run() {\n            invokeAwareInterfaces(bean);\n            return null;\n         }\n      }, acc);\n   }\n   else {\n      invokeAwareInterfaces(bean);\n   }\n\n   return bean;\n}\n\nprivate void invokeAwareInterfaces(Object bean) {\n   if (bean instanceof Aware) {\n      if (bean instanceof EnvironmentAware) {\n         ((EnvironmentAware) bean).setEnvironment(this.applicationContext.getEnvironment());\n      }\n      if (bean instanceof EmbeddedValueResolverAware) {\n         ((EmbeddedValueResolverAware) bean).setEmbeddedValueResolver(\n               new EmbeddedValueResolver(this.applicationContext.getBeanFactory()));\n      }\n      if (bean instanceof ResourceLoaderAware) {\n         ((ResourceLoaderAware) bean).setResourceLoader(this.applicationContext);\n      }\n      if (bean instanceof ApplicationEventPublisherAware) {\n         ((ApplicationEventPublisherAware) bean).setApplicationEventPublisher(this.applicationContext);\n      }\n      if (bean instanceof MessageSourceAware) {\n         ((MessageSourceAware) bean).setMessageSource(this.applicationContext);\n      }\n      if (bean instanceof ApplicationContextAware) {\n         ((ApplicationContextAware) bean).setApplicationContext(this.applicationContext);\n      }\n   }\n}\n\n\n<\/code><\/pre>\n<h2>4 \u603b\u7ed3<\/h2>\n<p>\u901a\u8fc7\u4e0a\u9762\u7684\u5206\u6790\uff0c\u53ef\u4ee5\u77e5\u9053Spring\u751f\u547d\u5468\u671f\u4e2d\u7684\u521d\u59cb\u5316\u65b9\u6cd5\u91cc\uff0c\u5728\u771f\u6b63\u6267\u884c\u521d\u59cb\u5316\u65b9\u6cd5\u4e4b\u524d\uff0c\u5206\u522b\u901a\u8fc7invokeAwareMethods\u65b9\u6cd5\u548c\u540e\u7f6e\u5904\u7406\u5668ApplicationContextAwareProcessor\u6765\u89e6\u53d1Aware\u7684\u8c03\u7528\uff0c\u90a3\u4e48\uff0cSpring\u4e3a\u4ec0\u4e48\u8981\u4f7f\u7528\u4e24\u79cd\u65b9\u5f0f\u800c\u4e0d\u4f7f\u7528\u5176\u4e2d\u4e4b\u4e00\u5462\uff1f<\/p>\n<p>\u901a\u8fc7\u672c\u7ae0\u6211\u4eec\u4e86\u89e3\u4e869\u4e2d\u5185\u7f6e\u63a5\u53e3\u7684\u4f5c\u7528\uff0c\u4ee5\u53ca\u5b83\u4eec\u80fd\u591f\u83b7\u53d6\u5230\u7684\u4e0d\u540c\u4e0a\u4e0b\u6587\u4fe1\u606f\u3002<\/p>\n<blockquote>\n<p>\u4f5c\u8005\uff1a\u4eac\u4e1c\u96f6\u552e \u66fe\u767b\u5747<\/p>\n<p>\u6765\u6e90\uff1a\u4eac\u4e1c\u4e91\u5f00\u53d1\u8005\u793e\u533a<\/p>\n<\/blockquote>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000043976984\">https:\/\/segmentfault.com\/a\/1190000043976984<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1 \u524d\u8a00 Aware\u662fSpring\u63d0\u4f9b\u7684\u4e00\u4e2a\u6807\u8bb0\u8d85\u63a5\u53e3\uff0c\u6307\u793abean\u6709\u8d44\u683c\u901a\u8fc7\u56de\u8c03\u6837\u5f0f\u7684\u65b9\u6cd5\u7531Spring\u5bb9\u5668 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10008049","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10008049","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10008049"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10008049\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10008049"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10008049"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10008049"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}