{"id":10008029,"date":"2023-07-04T10:59:36","date_gmt":"2023-07-04T02:59:36","guid":{"rendered":"https:\/\/ityet.com\/?p=10008029"},"modified":"2023-07-04T10:59:36","modified_gmt":"2023-07-04T02:59:36","slug":"spring%e5%ae%b9%e5%99%a8%e8%8e%b7%e5%8f%96bean%e7%9a%849%e7%a7%8d%e6%96%b9%e5%bc%8f-%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=10008029","title":{"rendered":"Spring\u5bb9\u5668\u83b7\u53d6Bean\u76849\u79cd\u65b9\u5f0f | \u4eac\u4e1c\u4e91\u6280\u672f\u56e2\u961f"},"content":{"rendered":"<h2>1 \u524d\u8a00<\/h2>\n<p>\u968f\u7740SpringBoot\u7684\u666e\u53ca\uff0cSpring\u7684\u4f7f\u7528\u4e5f\u8d8a\u6765\u8d8a\u5e7f\uff0c\u5728\u67d0\u4e9b\u573a\u666f\u4e0b\uff0c\u6211\u4eec\u65e0\u6cd5\u901a\u8fc7\u6ce8\u89e3\u6216\u914d\u7f6e\u7684\u5f62\u5f0f\u76f4\u63a5\u83b7\u53d6\u5230\u67d0\u4e2aBean\u3002\u6bd4\u5982\uff0c\u5728\u67d0\u4e00\u4e9b\u5de5\u5177\u7c7b\u3001\u8bbe\u8ba1\u6a21\u5f0f\u5b9e\u73b0\u4e2d\u9700\u8981\u4f7f\u7528\u5230Spring\u5bb9\u5668\u7ba1\u7406\u7684Bean\uff0c\u6b64\u65f6\u5c31\u9700\u8981\u76f4\u63a5\u83b7\u53d6\u5230\u5bf9\u5e94\u7684Bean\u3002<\/p>\n<p>\u672c\u6587\u4e3a\u5927\u5bb6\u6574\u7406\u6c47\u603b\u4e86\u5e38\u89c1\u7684\u83b7\u53d6Bean\u7684\u65b9\u5f0f\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e9b\u4f18\u52a3\u5206\u6790\uff0c\u65b9\u4fbf\u5927\u5bb6\u5728\u4f7f\u7528\u5230\u65f6\u6709\u66f4\u597d\u7684\u9009\u62e9\u3002\u540c\u65f6\uff0c\u4e5f\u4f1a\u4e3a\u5927\u5bb6\u9002\u5f53\u7684\u666e\u53ca\u548c\u62d3\u5c55\u4e00\u4e9b\u76f8\u5173\u77e5\u8bc6\u3002<\/p>\n<h2>2 Spring\u7684IoC\u5bb9\u5668<\/h2>\n<p>\u5728Spring\u4e2d\uff0cBean\u7684\u5b9e\u4f8b\u5316\u3001\u5b9a\u4f4d\u3001\u914d\u7f6e\u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684\u5bf9\u8c61\u53ca\u5efa\u7acb\u5bf9\u8c61\u95f4\u7684\u4f9d\u8d56\u5173\u7cfb\uff0c\u90fd\u662f\u5728IoC\u5bb9\u5668\u4e2d\u8fdb\u884c\u7684\u3002\u56e0\u6b64\uff0c\u8981\u5728Spring\u4e2d\u83b7\u53d6Bean\uff0c\u672c\u8d28\u4e0a\u5c31\u662f\u4eceIoC\u5bb9\u5668\u5f53\u4e2d\u83b7\u53d6Bean\u3002<\/p>\n<p>\u5728Spring\u4e2d\uff0cBeanFactory\u662fIoC\u5bb9\u5668\u7684\u5b9e\u9645\u4ee3\u8868\u8005\uff0c\u8be5\u63a5\u53e3\u63d0\u4f9b\u4e86IoC\u5bb9\u5668\u6700\u57fa\u672c\u529f\u80fd\u3002\u540c\u65f6\uff0cSpring\u8fd8\u63d0\u4f9b\u4e86\u53e6\u5916\u4e00\u79cd\u7c7b\u578b\u7684\u5bb9\u5668\uff1aApplicationContext\u5bb9\u5668\u3002<\/p>\n<p>ApplicationContext\u5bb9\u5668\u5305\u62ecBeanFactory\u5bb9\u5668\u7684\u6240\u6709\u529f\u80fd\uff08BeanFactory\u7684\u5b50\u63a5\u53e3\uff09\uff0c\u63d0\u4f9b\u4e86\u66f4\u591a\u9762\u5411\u5e94\u7528\u7684\u529f\u80fd\uff0c\u5b83\u63d0\u4f9b\u4e86\u56fd\u9645\u5316\u652f\u6301\u548c\u6846\u67b6\u4e8b\u4ef6\u4f53\u7cfb\uff0c\u66f4\u6613\u4e8e\u521b\u5efa\u5b9e\u9645\u5e94\u7528\u3002<\/p>\n<p>\u4e00\u822c\u60c5\u51b5\uff0c\u6211\u4eec\u79f0BeanFactory\u4e3aIoC\u5bb9\u5668\uff0c\u79f0ApplicationContext\u4e3a\u5e94\u7528\u4e0a\u4e0b\u6587\u3002\u4f46\u6709\u65f6\u4e3a\u4e86\u65b9\u4fbf\uff0c\u4e5f\u5c06ApplicationContext\u79f0\u4e3aSpring\u5bb9\u5668\u3002<\/p>\n<p>\u901a\u5e38\u4e0d\u5efa\u8bae\u4f7f\u7528BeanFactory\uff0c\u4f46BeanFactory \u4ecd\u7136\u53ef\u4ee5\u7528\u4e8e\u8f7b\u91cf\u7ea7\u7684\u5e94\u7528\u7a0b\u5e8f\uff0c\u5982\u79fb\u52a8\u8bbe\u5907\u6216\u57fa\u4e8eapplet\u7684\u5e94\u7528\u7a0b\u5e8f\uff0c\u5176\u4e2d\u5b83\u7684\u6570\u636e\u91cf\u548c\u901f\u5ea6\u662f\u663e\u8457\u3002<\/p>\n<h3>2.1 BeanFactory\u4e0eApplicationContext\u7684\u533a\u522b<\/h3>\n<p>BeanFactory\u662fSpring\u6846\u67b6\u7684\u57fa\u7840\u8bbe\u65bd\uff0c\u9762\u5411Spring\u672c\u8eab\u3002ApplicationContext\u5219\u9762\u5411\u4f7f\u7528Spring\u6846\u67b6\u7684\u5f00\u53d1\u8005\uff0c\u51e0\u4e4e\u6240\u6709\u7684\u5e94\u7528\u573a\u666f\u90fd\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528ApplicationContext\uff0c\u800c\u975e\u5e95\u5c42\u7684BeanFactory\u3002<\/p>\n<p>\u53e6\u5916\uff0cApplicationContext\u7684\u521d\u59cb\u5316\u548cBeanFactory\u6709\u4e00\u4e2a\u91cd\u5927\u7684\u533a\u522b\uff1a<\/p>\n<p>BeanFactory\u5728\u521d\u59cb\u5316\u5bb9\u5668\u65f6\uff0c\u5e76\u672a\u5b9e\u4f8b\u5316Bean\uff0c\u76f4\u5230\u7b2c\u4e00\u6b21\u8bbf\u95ee\u67d0\u4e2aBean\u65f6\u624d\u5b9e\u4f8b\u76ee\u6807Bean\u3002\u8fd9\u6837\uff0c\u6211\u4eec\u5c31\u4e0d\u80fd\u53d1\u73b0\u4e00\u4e9b\u5b58\u5728\u7684Spring\u7684\u914d\u7f6e\u95ee\u9898\u3002\u5982\u679cBean\u7684\u67d0\u4e00\u4e2a\u5c5e\u6027\u6ca1\u6709\u6ce8\u5165\uff0cBeanFacotry\u52a0\u8f7d\u540e\uff0c\u76f4\u81f3\u7b2c\u4e00\u6b21\u4f7f\u7528\u8c03\u7528getBean\u65b9\u6cd5\u624d\u4f1a\u629b\u51fa\u5f02\u5e38\u3002<\/p>\n<p>\u800cApplicationContext\u5219\u5728\u521d\u59cb\u5316\u5e94\u7528\u4e0a\u4e0b\u6587\u65f6\u5c31\u5b9e\u4f8b\u5316\u6240\u6709\u5355\u5b9e\u4f8b\u7684Bean\uff0c\u76f8\u5bf9\u5e94\u7684\uff0cApplicationContext\u7684\u521d\u59cb\u5316\u65f6\u95f4\u4f1a\u6bd4BeanFactory\u957f\u4e00\u4e9b\u3002<\/p>\n<p>\u4e86\u89e3\u4e86\u4e0a\u8ff0\u7684\u57fa\u672c\u7406\u8bba\u77e5\u8bc6\u4e4b\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5c1d\u8bd5\u4eceIoC\u5bb9\u5668\u5f53\u4e2d\u83b7\u53d6Bean\u5bf9\u8c61\u4e86\u3002<\/p>\n<h2>3 Bean\u83b7\u53d6\u65b9\u5f0f<\/h2>\n<h3>3.1 \u65b9\u5f0f\u4e00\uff1a\u901a\u8fc7BeanFactory\u83b7\u53d6<\/h3>\n<p>\u901a\u8fc7BeanFactory\u6765\u83b7\u53d6Bean\u3002\u57fa\u4e8exml\u914d\u7f6e\u6587\u4ef6\u7684\u65f6\u4ee3\uff0c\u53ef\u4ee5\u901a\u8fc7\u5982\u4e0b\u65b9\u5f0f\u83b7\u5f97BeanFactory\uff0c\u518d\u901a\u8fc7BeanFactory\u6765\u83b7\u5f97\u5bf9\u5e94\u7684Bean\u3002<\/p>\n<pre><code>BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource(&quot;applicationContext.xml&quot;));\nUser user = (User) beanFactory.getBean(&quot;user&quot;);<\/code><\/pre>\n<p>\u6709\u4e00\u5b9a\u7f16\u7a0b\u5e74\u9f84\u7684\u7a0b\u5e8f\u5458\uff0c\u5e94\u8be5\u5bf9\u6b64\u8fd8\u6709\u4e00\u4e9b\u5370\u8c61\u3002\u8fd9\u79cd\u5199\u6cd5\u4f30\u8ba1\u4e5f\u53ea\u4f1a\u51fa\u73b0\u5728\u53e4\u8001\u7684\u9879\u76ee\u5f53\u4e2d\u3002\u9274\u4e8exml\u5f62\u5f0f\u914d\u7f6e\u6587\u4ef6\u5df2\u7ecf\u88ab\u57fa\u4e8e\u6ce8\u89e3\u5f62\u5f0f\u6240\u66ff\u4ee3\uff0c\u540c\u65f6XmlBeanFactory\u4e5f\u88ab\u6807\u6ce8\u4e3a\u5e9f\u5f03\u3002\u6b64\u79cd\u65b9\u5f0f\u4e0d\u63a8\u8350\u4f7f\u7528\u3002<\/p>\n<p>\u5176\u5b9e\uff0c\u4e0d\u63a8\u8350\u7684\u7406\u7531\u8fd8\u6709\u4e00\u4e2a\uff0c\u5728\u4e0a\u9762\u5df2\u7ecf\u63d0\u5230\uff0c\u5c3d\u91cf\u4e0d\u8981\u4f7f\u7528BeanFactory\uff0c\u800c\u5e94\u8be5\u4f7f\u7528ApplicationContext\u3002<\/p>\n<h3>3.2 \u65b9\u5f0f\u4e8c \uff1a\u901a\u8fc7BeanFactoryAware\u83b7\u53d6<\/h3>\n<p>\u5728\u4e0a\u9762\u7684\u65b9\u5f0f\u4e2d\uff0cXmlBeanFactory\u5df2\u7ecf\u88ab\u5e9f\u5f03\uff0c\u4f46\u53ef\u4ee5\u901a\u8fc7\u5176\u4ed6\u65b9\u5f0f\u6765\u83b7\u5f97BeanFactory\uff0c\u7136\u540e\u518d\u4eceBeanFactory\u4e2d\u83b7\u5f97\u6307\u5b9a\u7684Bean\u3002\u83b7\u53d6BeanFactory\u5b9e\u4f8b\u6700\u7b80\u5355\u7684\u65b9\u5f0f\u5c31\u662f\u5b9e\u73b0BeanFactoryAware\u63a5\u53e3\u3002<\/p>\n<p>BeanFactoryAware\u63a5\u53e3\u6e90\u7801\uff1a<\/p>\n<pre><code>public interface BeanFactoryAware extends Aware {\n\n \/**\n  * \u521d\u59cb\u5316\u56de\u8c03\u65b9\u6cd5\uff0cSpring\u4f1a\u81ea\u52a8\u5c06BeanFactory\u6ce8\u5165\u8fdb\u53bb\uff0c\u63a5\u6536\u4e4b\u540e\u5373\u53ef\u4f7f\u7528BeanFactory\n  *\/\n void setBeanFactory(BeanFactory beanFactory) throws BeansException;\n}<\/code><\/pre>\n<p>BeanFactoryAware\u5c5e\u4e8e  <br \/>org.springframework.beans.factory.Aware\u6839\u6807\u8bb0\u63a5\u53e3\uff0c\u4f7f\u7528setter\u6ce8\u5165\u6765\u5728\u5e94\u7528\u7a0b\u5e8f\u4e0a\u4e0b\u6587\u542f\u52a8\u671f\u95f4\u83b7\u53d6\u5bf9\u8c61\u3002Aware\u63a5\u53e3\u662f\u56de\u8c03\uff0c\u76d1\u542c\u5668\u548c\u89c2\u5bdf\u8005\u8bbe\u8ba1\u6a21\u5f0f\u7684\u6df7\u5408\uff0c\u5b83\u8868\u793aBean\u6709\u8d44\u683c\u901a\u8fc7\u56de\u8c03\u65b9\u5f0f\u88abSpring\u5bb9\u5668\u901a\u77e5\u3002<\/p>\n<p>\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre><code>@Component\npublic class BeanFactoryHelper implements BeanFactoryAware {\n\n private static BeanFactory beanFactory;\n\n \/**\n  * \u91cd\u5199 BeanFactoryAware \u63a5\u53e3\u7684\u65b9\u6cd5\n  * @param beanFactory \uff1a\u53c2\u6570\u8d4b\u503c\u7ed9\u672c\u5730\u5c5e\u6027\u4e4b\u540e\u5373\u53ef\u4f7f\u7528 BeanFactory\n  * @throws BeansException BeansException\n  *\/\n @Override\n public void setBeanFactory(BeanFactory beanFactory) throws BeansException {\n  BeanFactoryHelper.beanFactory = beanFactory;\n }\n \/**\n  * \u6839\u636e\u540d\u79f0\u83b7\u53d6\u5bb9\u5668\u4e2d\u7684\u5bf9\u8c61\u5b9e\u4f8b\n  * @param beanName \uff1a\u6ce8\u5165\u7684\u5b9e\u4f8b\u5fc5\u987b\u5df2\u7ecf\u5b58\u5728\u5bb9\u5668\u4e2d\uff0c\u5426\u5219\u629b\u5f02\u5e38\uff1aNoSuchBeanDefinitionException\n  * @return Object\n  *\/\n public static Object getBean(String beanName) {\n  return beanFactory.getBean(beanName);\n }\n \/**\n  * \u6839\u636e class \u83b7\u53d6\u5bb9\u5668\u4e2d\u7684\u5bf9\u8c61\u5b9e\u4f8b\n  * @param requiredType \uff1a\u88ab\u6ce8\u5165\u7684\u5fc5\u987b\u5df2\u7ecf\u5b58\u5728\u5bb9\u5668\u4e2d\uff0c\u5426\u5219\u629b\u5f02\u5e38\uff1aNoSuchBeanDefinitionException\n  * @param &lt;T&gt; Class\n  * @return \u5bf9\u8c61\n  *\/\n public static &lt;T&gt; T getBean(Class&lt;T&gt; requiredType) {\n  return beanFactory.getBean(requiredType);\n }\n \/**\n  * \u5224\u65ad spring \u5bb9\u5668\u4e2d\u662f\u5426\u5305\u542b\u6307\u5b9a\u540d\u79f0\u7684\u5bf9\u8c61\n  * @param beanName bean\u540d\u79f0\n  * @return \u662f\u5426\u5b58\u5728\n  *\/\n public static boolean containsBean(String beanName) {\n  return beanFactory.containsBean(beanName);\n }\n \/\/\u5176\u5b83\u9700\u6c42\u7686\u53ef\u53c2\u8003 BeanFactory \u63a5\u53e3\u548c\u5b83\u7684\u5b9e\u73b0\u7c7b\n}<\/code><\/pre>\n<p>\u5728\u4e0a\u8ff0\u5de5\u5177\u7c7b\u4e2d\uff0c\u4fbf\u662f\u57fa\u4e8eBeanFactoryAware\u7684\u7279\u6027\uff0c\u83b7\u5f97\u4e86BeanFactory\uff0c\u7136\u540e\u518d\u901a\u8fc7BeanFactory\u6765\u83b7\u5f97\u6307\u5b9a\u7684Bean\u3002<\/p>\n<p>\u8be5\u65b9\u6848\u6ee1\u8db3\u4e86\u83b7\u53d6Bean\u7684\u57fa\u672c\u9700\u6c42\uff0c\u4f46\u540c\u65f6\u5177\u6709\u4f7f\u7528BeanFactory\u7684\u7f3a\u70b9\u3002\u6839\u636e\u524d\u6587\u4ecb\u7ecd\u7684BeanFactory\u7279\u6027\uff0c\u53ef\u914c\u60c5\u4f7f\u7528\u3002<\/p>\n<p>\u4e0a\u9762\u63d0\u4f9b\u4e86\u4e24\u79cd\u57fa\u4e8eBeanFactory\u5bb9\u5668\u83b7\u5f97Bean\u7684\u65b9\u5f0f\uff0c\u4e0b\u9762\u5219\u901a\u8fc7ApplicationContext\u6765\u83b7\u53d6\u5bb9\u5668\u4e2d\u7684Bean\uff0c\u4e0d\u540c\u7684\u662f\u83b7\u53d6ApplicationContext\u7684\u65b9\u5f0f\u7684\u533a\u522b\u3002<\/p>\n<h3>3.3 \u65b9\u5f0f\u4e09\uff1a\u542f\u52a8\u83b7\u53d6ApplicationContext<\/h3>\n<p>\u5728\u9879\u76ee\u542f\u52a8\u65f6\u5148\u83b7\u53d6ApplicationContext\u5bf9\u8c61\uff0c\u7136\u540e\u5c06\u5176\u5b58\u50a8\u5728\u4e00\u4e2a\u5730\u65b9\uff0c\u4ee5\u4fbf\u540e\u7eed\u7528\u5230\u65f6\u8fdb\u884c\u4f7f\u7528\u3002<\/p>\n<p>\u8fd9\u91cc\u63d0\u4f9b\u4e24\u79cd\u573a\u666f\u7684\u83b7\u53d6\uff1a<\/p>\n<p>1.\u57fa\u4e8exml\u914d\u7f6ebean\u7684\u5f62\u5f0f\uff0c\u9002\u7528\u4e8e\u6bd4\u8f83\u53e4\u8001\u7684\u9879\u76ee\uff0c\u5df2\u7ecf\u5f88\u5c11\u4f7f\u7528\u4e86\uff1b<\/p>\n<p>2.\u57fa\u4e8eSpringBoot\u542f\u52a8\u65f6\u83b7\u53d6ApplicationContext\u5bf9\u8c61\uff1b<\/p>\n<p>\u57fa\u4e8exml\u7684\u5f62\u5f0f\u5b9e\u73b0\uff1a<\/p>\n<pre><code>\/\/ \u5176\u4e2dapplicationContext.xml \u4e3a\u914d\u7f6e\u5bb9\u5668\u7684xml\uff0c\u4e0d\u8fc7\u73b0\u5728\u4e00\u822c\u5f88\u5c11\u4f7f\u7528\u4e86\nApplicationContext ac = new FileSystemXmlApplicationContext(&quot;applicationContext.xml&quot;);<\/code><\/pre>\n<p>\u8fd9\u91cc\u7b49\u4e8e\u76f4\u63a5\u521d\u59cb\u5316\u5bb9\u5668\uff0c\u5e76\u4e14\u83b7\u5f97\u5bb9\u5668\u7684\u5f15\u7528\u3002\u8fd9\u79cd\u65b9\u5f0f\u9002\u7528\u4e8e\u91c7\u7528Spring\u6846\u67b6\u7684\u72ec\u7acb\u5e94\u7528\u7a0b\u5e8f\uff0c\u9700\u8981\u7a0b\u5e8f\u901a\u8fc7\u914d\u7f6e\u6587\u4ef6\u624b\u5de5\u521d\u59cb\u5316Spring\u7684\u60c5\u51b5\u3002\u76ee\u524d\u5927\u591a\u6570Spring\u9879\u76ee\u5df2\u7ecf\u4e0d\u518d\u91c7\u7528xml\u914d\u7f6e\uff0c\u5f88\u5c11\u4f7f\u7528\u4e86\u3002<\/p>\n<p>\u57fa\u4e8eSpringBoot\u542f\u52a8\u5b9e\u73b0\uff1a<\/p>\n<pre><code>@SpringBootApplication\npublic class ExampleApplication {\n\n    public static void main(String[] args) {\n        \/\/ \u542f\u52a8\u65f6\uff0c\u4fdd\u5b58\u4e0a\u4e0b\u6587\uff0c\u5e76\u4fdd\u5b58\u4e3a\u9759\u6001\n        ConfigurableApplicationContext ac = SpringApplication.run(ExampleApplication.class, args);\n        SpringContextUtil.setApplicationContext(ac);\n    }\n}<\/code><\/pre>\n<p>\u5bf9\u5e94\u7684SpringContextUtil\u7c7b\u5982\u4e0b\uff1a<\/p>\n<pre><code>public class SpringContextUtil1 {\n\n    private static ApplicationContext ac;\n\n    public static &lt;T&gt;  T getBean(String beanName, Class&lt;T&gt; clazz) {\n        T bean = ac.getBean(beanName, clazz);\n        return bean;\n    }\n\n    public static void setApplicationContext(ApplicationContext applicationContext){\n        ac = applicationContext;\n    }\n}<\/code><\/pre>\n<p>\u4e24\u79cd\u65b9\u5f0f\u90fd\u662f\u5728\u542f\u52a8Spring\u9879\u76ee\u65f6\uff0c\u76f4\u63a5\u83b7\u53d6\u5230ApplicationContext\u7684\u5f15\u7528\uff0c\u7136\u540e\u5c06\u5176\u5b58\u50a8\u5230\u5de5\u5177\u7c7b\u5f53\u4e2d\u3002\u5728\u4f7f\u7528\u65f6\uff0c\u5219\u4ece\u5de5\u5177\u7c7b\u4e2d\u83b7\u53d6ApplicationContext\u5bb9\u5668\uff0c\u8fdb\u800c\u4ece\u4e2d\u83b7\u5f97Bean\u5bf9\u8c61\u3002<\/p>\n<h3>3.4 \u65b9\u5f0f\u56db\uff1a\u901a\u8fc7\u7ee7\u627fApplicationObjectSupport<\/h3>\n<p>\u6b64\u79cd\u65b9\u5f0f\u4f9d\u65e7\u662f\u5148\u83b7\u5f97ApplicationContext\u5bb9\u5668\uff0c\u7136\u540e\u4ece\u4e2d\u83b7\u53d6Bean\u5bf9\u8c61\uff0c\u53ea\u4e0d\u8fc7\u662f\u57fa\u4e8e\u7ee7\u627fApplicationObjectSupport\u7c7b\u5b9e\u73b0\u7684\u3002<\/p>\n<p>\u5177\u4f53\u5b9e\u73b0\u4ee3\u7801\uff1a<\/p>\n<pre><code>@Component\npublic class SpringContextUtil extends ApplicationObjectSupport {\n public &lt;T&gt; T getBean(Class&lt;T&gt; clazz) {\n  ApplicationContext ac = getApplicationContext();\n  if(ac == null){\n   return null;\n  }\n  return ac.getBean(clazz);\n }\n}<\/code><\/pre>\n<p>\u6ce8\u610f\uff0c\u8fd9\u91cc\u7684SpringContextUtil\u7c7b\u9700\u8981\u5b9e\u4f8b\u5316\u3002<\/p>\n<p>ApplicationObjectSupport\u7c7b\u56fe\u5165\u4e0b\uff0c\u6211\u4eec\u770b\u5230\u5b83\u5b9e\u73b0\u4e86ApplicationContextAware\u63a5\u53e3\uff0c\u5728Spring\u5bb9\u5668\u521d\u59cb\u5316\u8fc7\u7a0b\u4e2d\u56de\u8c03\u65b9\u6cd5setApplicationContext\u6765\u5b8c\u6210ApplicationContext\u7684\u8d4b\u503c\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043966511\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<h3>3.5 \u65b9\u5f0f\u4e94\uff1a\u901a\u8fc7\u7ee7\u627fWebApplicationObjectSupport<\/h3>\n<p>WebApplicationObjectSupport\u662fApplicationObjectSupport\u7684\u4e00\u4e2a\u5b9e\u73b0\u7c7b\uff0c\u63d0\u4f9b\u4e86Web\u76f8\u5173\u7684\u652f\u6301\u3002\u5b9e\u73b0\u539f\u7406\u4e0eApplicationObjectSupport\u4e00\u6837\u3002<\/p>\n<p>\u5177\u4f53\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code>@Component\npublic class SpringContextUtil extends WebApplicationObjectSupport {\n public &lt;T&gt; T getBean(Class&lt;T&gt; clazz) {\n  ApplicationContext ac = getApplicationContext();\n  if(ac == null){\n   return null;\n  }\n  return ac.getBean(clazz);\n }\n}<\/code><\/pre>\n<p>\u901a\u8fc7\u7c7b\u56fe\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u5b83\u662fApplicationObjectSupport\u7684\u5b9e\u73b0\u5b50\u7c7b\uff0c\u6b64\u65b9\u5f0f\u9664\u4e86\u7ee7\u627f\u5bf9\u8c61\u4e0d\u540c\u5916\uff0c\u6ca1\u6709\u5176\u4ed6\u533a\u522b\uff0c\u90fd\u662f\u57fa\u4e8egetApplicationContext\u65b9\u6cd5\u6765\u83b7\u53d6\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043966512\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<h3>3.6 \u65b9\u5f0f\u516d\uff1a\u901a\u8fc7WebApplicationContextUtils<\/h3>\n<p>Spring\u63d0\u4f9b\u4e86\u5de5\u5177\u7c7b  <br \/>WebApplicationContextUtils\uff0c\u901a\u8fc7\u8be5\u7c7b\u53ef\u83b7\u53d6WebApplicationContext\u5bf9\u8c61\u3002<\/p>\n<p>\u5177\u4f53\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code>public class SpringContextUtil2 {\n public static &lt;T&gt; T getBean(ServletContext request, String name, Class&lt;T&gt; clazz){\n  WebApplicationContext webApplicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(request);\n  \/\/ \u6216\u8005\n  WebApplicationContext webApplicationContext1 = WebApplicationContextUtils.getWebApplicationContext(request);\n\/\/        webApplicationContext1.getBean(name, clazz)\n  T bean = webApplicationContext.getBean(name, clazz);\n  return bean;\n }\n}<\/code><\/pre>\n<p>\u8fd9\u4e2a\u65b9\u6cd5\u5f88\u5e38\u89c1\u4e8eSpringMVC\u6784\u5efa\u7684Web\u9879\u76ee\u4e2d\uff0c\u9002\u7528\u4e8eWeb\u9879\u76ee\u7684B\/S\u7ed3\u6784\u3002<\/p>\n<h3>3.7 \u65b9\u5f0f\u4e03\uff1a\u901a\u8fc7ApplicationContextAware<\/h3>\n<p>\u901a\u8fc7\u5b9e\u73b0ApplicationContextAware\u63a5\u53e3\uff0c\u5728Spring\u5bb9\u5668\u542f\u52a8\u65f6\u5c06ApplicationContext\u6ce8\u5165\u8fdb\u53bb\uff0c\u4ece\u800c\u83b7\u53d6ApplicationContext\u5bf9\u8c61\uff0c\u8fd9\u79cd\u65b9\u6cd5\u4e5f\u662f\u5e38\u89c1\u7684\u83b7\u53d6Bean\u7684\u4e00\u79cd\u65b9\u5f0f\uff0c\u63a8\u8350\u4f7f\u7528\u3002<\/p>\n<p>\u5177\u4f53\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code>@Component\npublic class SpringContextUtil3 implements ApplicationContextAware {\n\n private static ApplicationContext ac;\n\n @Override\n public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {\n  ac = applicationContext;\n }\n\n public static &lt;T&gt; T getBean(Class&lt;T&gt; clazz) {\n  T bean = ac.getBean(clazz);\n  return bean;\n }\n\n}<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u4e0e\u524d\u9762\u901a\u8fc7BeanFactoryAware\u83b7\u5f97BeanFactory\u7684\u601d\u8def\u4e00\u81f4\u3002\u5176\u672c\u8d28\u548c\u56db\u3001\u4e94\u4e24\u79cd\u65b9\u5f0f\u4e00\u6837\u3002<\/p>\n<h3>3.8 \u65b9\u5f0f\u516b\uff1a\u901a\u8fc7ContextLoader<\/h3>\n<p>\u4f7f\u7528ContextLoader\u63d0\u4f9b\u7684  <br \/>getCurrentWebApplicationContext\u65b9\u6cd5\uff0c\u4e5f\u662f\u5e38\u7528\u7684\u83b7\u53d6WebApplicationContext\u7684\u4e00\u79cd\u65b9\u6cd5\u3002<\/p>\n<p>\u5177\u4f53\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code>WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext();\nwac.getBean(beanID);<\/code><\/pre>\n<p>\u8be5\u65b9\u6cd5\u5e38\u89c1\u4e8eSpringMVC\u5b9e\u73b0\u7684Web\u9879\u76ee\u4e2d\u3002\u8be5\u65b9\u5f0f\u662f\u4e00\u79cd\u4e0d\u4f9d\u8d56\u4e8eServlet\uff0c\u4e0d\u9700\u8981\u6ce8\u5165\u7684\u65b9\u5f0f\u3002\u4f46\u662f\u9700\u8981\u6ce8\u610f\u4e00\u70b9\uff0c\u5728\u670d\u52a1\u5668\u542f\u52a8\u65f6\u548cSpring\u5bb9\u5668\u521d\u59cb\u5316\u65f6\uff0c\u4e0d\u80fd\u901a\u8fc7\u8be5\u65b9\u6cd5\u83b7\u53d6Spring\u5bb9\u5668\u3002<\/p>\n<h3>3.9 \u65b9\u5f0f\u4e5d\uff1a\u901a\u8fc7BeanFactoryPostProcessor<\/h3>\n<p>Spring\u5de5\u5177\u7c7b\uff0c\u65b9\u4fbf\u5728\u975eSpring\u7ba1\u7406\u73af\u5883\u4e2d\u83b7\u53d6Bean\u3002<\/p>\n<pre><code>@Component\npublic final class SpringUtils implements BeanFactoryPostProcessor{\n    \n    \/** Spring\u5e94\u7528\u4e0a\u4e0b\u6587\u73af\u5883 *\/\n    private static ConfigurableListableBeanFactory beanFactory;\n\n    @Override\n    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException{\n        SpringUtilsS.beanFactory = beanFactory;\n    }\n\n    \/**\n     * \u83b7\u53d6\u5bf9\u8c61\n     *\n     * @param name\n     * @return Object \u4e00\u4e2a\u4ee5\u6240\u7ed9\u540d\u5b57\u6ce8\u518c\u7684bean\u7684\u5b9e\u4f8b\n     * @throws BeansException\n     *\n     *\/\n    @SuppressWarnings(&quot;unchecked&quot;)\n    public static &lt;T&gt; T getBean(String name) throws BeansException{\n        return (T) beanFactory.getBean(name);\n    }\n\n    \/**\n     * \u83b7\u53d6\u7c7b\u578b\u4e3arequiredType\u7684\u5bf9\u8c61\n     *\n     * @param clz\n     * @return\n     * @throws BeansException\n     *\n     *\/\n    public static &lt;T&gt; T getBean(Class&lt;T&gt; clz) throws BeansException{\n        T result = (T) beanFactory.getBean(clz);\n        return result;\n    }\n\n    \/**\n     * \u5982\u679cBeanFactory\u5305\u542b\u4e00\u4e2a\u4e0e\u6240\u7ed9\u540d\u79f0\u5339\u914d\u7684bean\u5b9a\u4e49\uff0c\u5219\u8fd4\u56detrue\n     *\n     * @param name\n     * @return boolean\n     *\/\n    public static boolean containsBean(String name){\n        return beanFactory.containsBean(name);\n    }\n\n    \/**\n     * \u5224\u65ad\u4ee5\u7ed9\u5b9a\u540d\u5b57\u6ce8\u518c\u7684bean\u5b9a\u4e49\u662f\u4e00\u4e2asingleton\u8fd8\u662f\u4e00\u4e2aprototype\u3002 \u5982\u679c\u4e0e\u7ed9\u5b9a\u540d\u5b57\u76f8\u5e94\u7684bean\u5b9a\u4e49\u6ca1\u6709\u88ab\u627e\u5230\uff0c\u5c06\u4f1a\u629b\u51fa\u4e00\u4e2a\u5f02\u5e38\uff08NoSuchBeanDefinitionException\uff09\n     *\n     * @param name\n     * @return boolean\n     * @throws NoSuchBeanDefinitionException\n     *\n     *\/\n    public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException{\n        return beanFactory.isSingleton(name);\n    }\n\n    \/**\n     * @param name\n     * @return Class \u6ce8\u518c\u5bf9\u8c61\u7684\u7c7b\u578b\n     * @throws NoSuchBeanDefinitionException\n     *\n     *\/\n    public static Class&lt;?&gt; getType(String name) throws NoSuchBeanDefinitionException{\n        return beanFactory.getType(name);\n    }\n\n    \/**\n     * \u5982\u679c\u7ed9\u5b9a\u7684bean\u540d\u5b57\u5728bean\u5b9a\u4e49\u4e2d\u6709\u522b\u540d\uff0c\u5219\u8fd4\u56de\u8fd9\u4e9b\u522b\u540d\n     *\n     * @param name\n     * @return\n     * @throws NoSuchBeanDefinitionException\n     *\n     *\/\n    public static String[] getAliases(String name) throws NoSuchBeanDefinitionException{\n        return beanFactory.getAliases(name);\n    }\n\n    \/**\n     * \u83b7\u53d6aop\u4ee3\u7406\u5bf9\u8c61\n     * \n     * @param invoker\n     * @return\n     *\/\n    @SuppressWarnings(&quot;unchecked&quot;)\n    public static &lt;T&gt; T getAopProxy(T invoker){\n        return (T) AopContext.currentProxy();\n    }\n}<\/code><\/pre>\n<p>\u5176\u4e2d  <br \/>ConfigurableListableBeanFactory\u63a5\u53e3\uff0c\u4e5f\u5c5e\u4e8eBeanFactory\u7684\u5b50\u63a5\u53e3\u3002<\/p>\n<h2>4 \u603b\u7ed3<\/h2>\n<p>\u5728\u672c\u6587\u4e2d\u4ecb\u7ecd\u4e869\u79cd\u4eceSpring\u5bb9\u5668\u4e2d\u83b7\u53d6Bean\u7684\u65b9\u6cd5\uff0c\u867d\u7136\u6bcf\u79cd\u65b9\u5f0f\u5b9e\u73b0\u5404\u6709\u4e0d\u540c\uff0c\u4f46\u4ece\u672c\u8d28\u4e0a\u6765\u8bb2\uff0c\u65e0\u975e\u5c31\u662f\u901a\u8fc7BeanFactory\u6216ApplicationContext\u83b7\u53d6Bean\uff0c\u53ea\u4e0d\u8fc7\u83b7\u53d6BeanFactory\u6216ApplicationContext\u5bb9\u5668\u7684\u65b9\u5f0f\u4e0d\u540c\u800c\u5df2\u3002 \u4e25\u683c\u6765\u8bf4ApplicationContext\u4e5f\u662fBeanFactory\u3002<\/p>\n<p>\u90a3\u4e48\uff0c\u4f60\u662f\u5426\u610f\u8bc6\u5230\uff0c\u5b66\u4e60\u4e00\u9879\u6280\u672f\u6216\u4e00\u4e2a\u5b9e\u73b0\u65b9\u5f0f\uff0c\u53ea\u8981\u628a\u63e1\u4f4f\u5b83\u7684\u6839\u672c\uff0c\u65e0\u8bba\u5f62\u5f0f\u5982\u4f55\u53d8\u5316\uff0c\u90fd\u4e07\u53d8\u4e0d\u79bb\u5176\u5b97\u3002\u800c\u8fd9\u91cc\u201c\u5b97\u201d\u5c31\u662fIoC\u5bb9\u5668\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\/1190000043966509\">https:\/\/segmentfault.com\/a\/1190000043966509<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1 \u524d\u8a00 \u968f\u7740SpringBoot\u7684\u666e\u53ca\uff0cSpring\u7684\u4f7f\u7528\u4e5f\u8d8a\u6765\u8d8a\u5e7f\uff0c\u5728\u67d0\u4e9b\u573a\u666f\u4e0b\uff0c\u6211\u4eec\u65e0\u6cd5\u901a\u8fc7\u6ce8\u89e3\u6216\u914d\u7f6e\u7684 [&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-10008029","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\/10008029","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=10008029"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10008029\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10008029"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10008029"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10008029"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}