{"id":1043,"date":"2022-12-13T15:33:32","date_gmt":"2022-12-13T07:33:32","guid":{"rendered":"https:\/\/blog.frost-s.com\/?p=1043"},"modified":"2022-12-13T15:35:39","modified_gmt":"2022-12-13T07:35:39","slug":"spring-%e8%8e%b7%e5%8f%96bean%e7%9a%84%e5%85%ab%e7%a7%8d%e6%96%b9%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/blog.frost-s.com\/index.php\/2022\/12\/13\/spring-%e8%8e%b7%e5%8f%96bean%e7%9a%84%e5%85%ab%e7%a7%8d%e6%96%b9%e5%bc%8f\/","title":{"rendered":"Spring \u83b7\u53d6bean\u7684\u516b\u79cd\u65b9\u5f0f"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\" id=\"outline_1\">1\u3001\u5728\u521d\u59cb\u5316\u65f6\u4fdd\u5b58ApplicationContext\u5bf9\u8c61<\/h3>\n\n\n\n<p>\u9002\u7528\u4e8eSpring\u6846\u67b6\u7684\u72ec\u7acb\u5e94\u7528\u7a0b\u5e8f\uff0c\u987b\u8981\u7a0b\u5e8f\u901a\u8fc7\u914d\u7f6e\u6587\u4ef6\u521d\u59cb\u5316Spring\u3002<\/p>\n\n\n\n<p><code>applicationContext.xml<\/code>\u914d\u7f6e\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?xml&nbsp;version=\"1.0\"&nbsp;encoding=\"UTF-8\"?&gt;\n&lt;<strong>beans<\/strong>&nbsp;xmlns=\"http:\/\/www.springframework.org\/schema\/beans\"\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;xmlns:p=\"http:\/\/www.springframework.org\/schema\/p\"\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;xsi:schemaLocation=\"http:\/\/www.springframework.org\/schema\/beans&nbsp;http:\/\/www.springframework.org\/schema\/beans\/spring-beans.xsd\"&gt;\n&lt;<strong>bean<\/strong>&nbsp;id=\"test\"&nbsp;class=\"com.sxtx.bean.Test\"&gt;\n&lt;\/<strong>bean<\/strong>&gt;\n\n&lt;\/<strong>beans<\/strong>&gt;\n<\/code><\/pre>\n\n\n\n<p>\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@<strong>Test<\/strong>\npublic&nbsp;void&nbsp;<strong>test<\/strong>()&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;ApplicationContext&nbsp;applicationContext&nbsp;=&nbsp;new&nbsp;ClassPathXmlApplicationContext(\"applicationContext.xml\");\n&nbsp;&nbsp;&nbsp;&nbsp;\/\/ApplicationContext&nbsp;applicationContext&nbsp;=&nbsp;new&nbsp;FileSystemXmlApplicationContext(\"applicationContext.xml\");&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;<strong>Test<\/strong>&nbsp;<strong>test<\/strong>=&nbsp;(<strong>Test<\/strong>)&nbsp;applicationContext.getBean(\"test\");\n&nbsp;&nbsp;&nbsp;&nbsp;System.<strong>out<\/strong>.println(<strong>test<\/strong>);\n}\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"outline_2\">2\u3001\u901a\u8fc7Spring\u63d0\u4f9b\u7684\u5de5\u5177\u7c7b\u83b7\u53d6ApplicationContext\u5bf9\u8c61<\/h3>\n\n\n\n<p>\u9002\u5408\u4e8eSpring\u6846\u67b6\u7684B\/S\u7cfb\u7edf\uff0c\u901a\u8fc7<code>ServletContext<\/code>\u5bf9\u8c61\u83b7\u53d6<code>ApplicationContext<\/code>\u5bf9\u8c61\u3002\u7136\u540e\u5728\u901a\u8fc7\u5b83\u83b7\u53d6\u987b\u8981\u7684\u7c7b\u5b9e\u4f8b\u3002\u4ee5\u4e0b\u4e24\u4e2a\u5de5\u5177\u65b9\u5f0f\u7684\u5dee\u522b\u662f\uff0c\u524d\u8005\u5728\u83b7\u53d6\u5931\u8d25\u65f6\u629b\u51fa\u5f02\u5e38\u3002\u540e\u8005\u8fd4\u56denull\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ApplicationContext&nbsp;ac1&nbsp;=&nbsp;WebApplicationContextUtils.getRequiredWebApplicationContext(ServletContext&nbsp;sc);&nbsp;\nApplicationContext&nbsp;ac2&nbsp;=&nbsp;WebApplicationContextUtils.getWebApplicationContext(ServletContext&nbsp;sc);&nbsp;\nac1.getBean(\"beanId\");&nbsp;\nac2.getBean(\"beanId\");&nbsp;&nbsp;\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"outline_3\">3\u3001\u5b9e\u73b0\u63a5\u53e3ApplicationContextAware\uff08\u63a8\u8350\uff09<\/h3>\n\n\n\n<p>\u5b9e\u73b0\u8be5\u63a5\u53e3\u7684<code>setApplicationContext(ApplicationContext context)<\/code>\u65b9\u6cd5\uff0c\u5e76\u4fdd\u5b58<code>ApplicationContext<\/code>&nbsp;\u5bf9\u8c61\u3002Spring\u521d\u59cb\u5316\u65f6\uff0c\u626b\u63cf\u5230\u8be5\u7c7b\uff0c\u5c31\u4f1a\u901a\u8fc7\u8be5\u65b9\u6cd5\u5c06<code>ApplicationContext<\/code>\u5bf9\u8c61\u6ce8\u5165\u3002\u7136\u540e\u5728\u4ee3\u7801\u4e2d\u5c31\u53ef\u4ee5\u83b7\u53d6spring\u5bb9\u5668bean\u4e86\u3002<\/p>\n\n\n\n<p>\u4f8b\u5982\uff1a<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><code>User bean = SpringUtils.getBean(\u201cuser\u201d);<\/code><\/p>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>@Component\n<strong>public<\/strong>&nbsp;<strong>class<\/strong>&nbsp;<strong>SpringUtils<\/strong>&nbsp;<strong>implements<\/strong>&nbsp;<strong>ApplicationContextAware<\/strong>&nbsp;{\n&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;<strong>private<\/strong>&nbsp;<strong>static<\/strong>&nbsp;<strong>ApplicationContext<\/strong>&nbsp;applicationContext;\n\n&nbsp;&nbsp;&nbsp;&nbsp;@Override\n&nbsp;&nbsp;&nbsp;&nbsp;<strong>public<\/strong>&nbsp;void&nbsp;<strong>setApplicationContext<\/strong>(<strong>ApplicationContext<\/strong>&nbsp;applicationContext)&nbsp;throws&nbsp;<strong>BeansException<\/strong>&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>SpringUtils<\/strong>.applicationContext&nbsp;=&nbsp;applicationContext;\n&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;<strong>public<\/strong>&nbsp;<strong>static<\/strong>&nbsp;&lt;T&gt;&nbsp;T&nbsp;<strong>getBean<\/strong>(String&nbsp;beanName)&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>if<\/strong>(applicationContext.<strong>containsBean<\/strong>(beanName)){\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>return<\/strong>&nbsp;(T)&nbsp;applicationContext.<strong>getBean<\/strong>(beanName);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<strong>else<\/strong>{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>return<\/strong>&nbsp;null;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;<strong>public<\/strong>&nbsp;<strong>static<\/strong>&nbsp;&lt;T&gt;&nbsp;<strong>Map<\/strong>&lt;<strong>String<\/strong>,&nbsp;T&gt;&nbsp;<strong>getBeansOfType<\/strong>(Class&lt;T&gt;&nbsp;baseType){\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>return<\/strong>&nbsp;applicationContext.<strong>getBeansOfType<\/strong>(baseType);\n&nbsp;&nbsp;&nbsp;&nbsp;}\n}\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"outline_4\">4\u3001\u7ee7\u627f\u81ea\u62bd\u8c61\u7c7bApplicationObjectSupport<\/h3>\n\n\n\n<p>\u8c03\u7528\u7236\u7c7b\u7684<code>getApplicationContext()<\/code>\u65b9\u6cd5\uff0c\u83b7\u53d6Spring\u5bb9\u5668\u5bf9\u8c61\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Service\n<strong>public<\/strong>&nbsp;<strong>class<\/strong>&nbsp;<strong>SpringContextHelper<\/strong>&nbsp;<strong>extends<\/strong>&nbsp;<strong>ApplicationObjectSupport<\/strong>&nbsp;{\n\n&nbsp;&nbsp;&nbsp;&nbsp;<strong>public<\/strong>&nbsp;<strong>Object<\/strong>&nbsp;<strong>getBean<\/strong>(String&nbsp;beanName)&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>return<\/strong>&nbsp;<strong>getApplicationContext<\/strong>().<strong>getBean<\/strong>(beanName);\n&nbsp;&nbsp;&nbsp;&nbsp;}\n}\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"outline_5\">5\u3001\u7ee7\u627f\u81ea\u62bd\u8c61\u7c7bWebApplicationObjectSupport<\/h3>\n\n\n\n<p>\u8c03\u7528<code>getWebApplicationContext()<\/code>\u83b7\u53d6<code>WebApplicationContext<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Service\n<strong>public<\/strong>&nbsp;<strong>class<\/strong>&nbsp;<strong>SpringContextHelper<\/strong>&nbsp;<strong>extends<\/strong>&nbsp;<strong>WebApplicationObjectSupport<\/strong>&nbsp;{\n\n&nbsp;&nbsp;&nbsp;&nbsp;<strong>public<\/strong>&nbsp;<strong>Object<\/strong>&nbsp;<strong>getBean<\/strong>(String&nbsp;beanName)&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>return<\/strong>&nbsp;<strong>getApplicationContext<\/strong>().<strong>getBean<\/strong>(beanName);\n&nbsp;&nbsp;&nbsp;&nbsp;}\n}\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"outline_6\">6\u3001\u4f7f\u7528BeanFactory\u76f4\u63a5\u83b7\u53d6\uff08\u4e0d\u63a8\u8350\uff09<\/h3>\n\n\n\n<p>\u4f7f\u7528<code>BeanFactory<\/code>\u4ece\u5de5\u5382\u4e2d\u76f4\u63a5\u83b7\u53d6Bean\u5b9e\u4f8b\uff0c\u4f46\u662f<code>XmlBeanFactory<\/code>\u7c7b\u5df2\u7ecf\u5e9f\u5f03\uff0c\u56e0\u6b64\u4e0d\u5efa\u8bae\u4f7f\u7528\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@<strong>Test<\/strong>\npublic&nbsp;void&nbsp;<strong>test<\/strong>()&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;BeanFactory&nbsp;beanFactory&nbsp;=&nbsp;new&nbsp;XmlBeanFactory(new&nbsp;ClassPathResource(\"applicationContext.xml\"));\n&nbsp;&nbsp;&nbsp;&nbsp;<strong>Test<\/strong>&nbsp;<strong>test<\/strong>=&nbsp;(<strong>Test<\/strong>)&nbsp;beanFactory.getBean(\"test\");\n&nbsp;&nbsp;&nbsp;&nbsp;System.<strong>out<\/strong>.println(<strong>test<\/strong>);\n}\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"outline_7\">7\u3001\u4f7f\u7528ContextLoader\u63d0\u4f9b\u7684getCurrentWebApplicationContext\u65b9\u6cd5<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>@Test\npublic&nbsp;void&nbsp;test()&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;MockServletContext&nbsp;sc&nbsp;=&nbsp;<strong>new<\/strong>&nbsp;MockServletContext(\"\");\n&nbsp;&nbsp;&nbsp;&nbsp;sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM,&nbsp;\"\/applicationContext.xml\");\n&nbsp;&nbsp;&nbsp;&nbsp;ServletContextListener&nbsp;listener&nbsp;=&nbsp;<strong>new<\/strong>&nbsp;ContextLoaderListener();\n&nbsp;&nbsp;&nbsp;&nbsp;ServletContextEvent&nbsp;event&nbsp;=&nbsp;<strong>new<\/strong>&nbsp;ServletContextEvent(sc);\n&nbsp;&nbsp;&nbsp;&nbsp;listener.contextInitialized(event);\n&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;WebApplicationContext&nbsp;wac&nbsp;=&nbsp;ContextLoader.getCurrentWebApplicationContext();\n&nbsp;&nbsp;&nbsp;&nbsp;Test&nbsp;test=&nbsp;(Test)&nbsp;wac.getBean(\"test\");\n&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(test);\n}\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"outline_8\">8\u3001\u5b9e\u73b0\u63a5\u53e3BeanFactoryPostProcessor<\/h3>\n\n\n\n<p>spring\u5de5\u5177\u7c7b \u65b9\u4fbf\u5728\u975espring\u7ba1\u7406\u73af\u5883\u4e2d\u83b7\u53d6bean<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Component\n<strong>public<\/strong>&nbsp;<strong>final<\/strong>&nbsp;<strong>class<\/strong>&nbsp;<strong>SpringUtilsS<\/strong>&nbsp;<strong>implements<\/strong>&nbsp;<strong>BeanFactoryPostProcessor<\/strong>\n{\n&nbsp;&nbsp;&nbsp;&nbsp;\/**&nbsp;Spring\u5e94\u7528\u4e0a\u4e0b\u6587\u73af\u5883&nbsp;*\/\n&nbsp;&nbsp;&nbsp;&nbsp;<strong>private<\/strong>&nbsp;<strong>static<\/strong>&nbsp;ConfigurableListableBeanFactory&nbsp;beanFactory;\n\n&nbsp;&nbsp;&nbsp;&nbsp;@Override\n&nbsp;&nbsp;&nbsp;&nbsp;<strong>public<\/strong>&nbsp;<strong>void<\/strong>&nbsp;<strong>postProcessBeanFactory<\/strong>(ConfigurableListableBeanFactory&nbsp;beanFactory)&nbsp;<strong>throws<\/strong>&nbsp;BeansException\n&nbsp;&nbsp;&nbsp;&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SpringUtilsS.beanFactory&nbsp;=&nbsp;beanFactory;\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n&nbsp;&nbsp;&nbsp;&nbsp;\/**\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;\u83b7\u53d6\u5bf9\u8c61\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;<strong>@param<\/strong>&nbsp;name\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;<strong>@return<\/strong>&nbsp;Object&nbsp;\u4e00\u4e2a\u4ee5\u6240\u7ed9\u540d\u5b57\u6ce8\u518c\u7684bean\u7684\u5b9e\u4f8b\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;<strong>@throws<\/strong>&nbsp;BeansException\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*\/\n&nbsp;&nbsp;&nbsp;&nbsp;@SuppressWarnings(\"unchecked\")\n&nbsp;&nbsp;&nbsp;&nbsp;<strong>public<\/strong>&nbsp;<strong>static<\/strong>&nbsp;&lt;T&gt;&nbsp;T&nbsp;<strong>getBean<\/strong>(String&nbsp;name)&nbsp;<strong>throws<\/strong>&nbsp;BeansException\n&nbsp;&nbsp;&nbsp;&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>return<\/strong>&nbsp;(T)&nbsp;beanFactory.getBean(name);\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n&nbsp;&nbsp;&nbsp;&nbsp;\/**\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;\u83b7\u53d6\u7c7b\u578b\u4e3arequiredType\u7684\u5bf9\u8c61\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;<strong>@param<\/strong>&nbsp;clz\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;<strong>@return<\/strong>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;<strong>@throws<\/strong>&nbsp;BeansException\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*\/\n&nbsp;&nbsp;&nbsp;&nbsp;<strong>public<\/strong>&nbsp;<strong>static<\/strong>&nbsp;&lt;T&gt;&nbsp;T&nbsp;<strong>getBean<\/strong>(Class&lt;T&gt;&nbsp;clz)&nbsp;<strong>throws<\/strong>&nbsp;BeansException\n&nbsp;&nbsp;&nbsp;&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;T&nbsp;result&nbsp;=&nbsp;(T)&nbsp;beanFactory.getBean(clz);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>return<\/strong>&nbsp;result;\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n&nbsp;&nbsp;&nbsp;&nbsp;\/**\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;\u5982\u679cBeanFactory\u5305\u542b\u4e00\u4e2a\u4e0e\u6240\u7ed9\u540d\u79f0\u5339\u914d\u7684bean\u5b9a\u4e49\uff0c\u5219\u8fd4\u56detrue\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;<strong>@param<\/strong>&nbsp;name\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;<strong>@return<\/strong>&nbsp;boolean\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*\/\n&nbsp;&nbsp;&nbsp;&nbsp;<strong>public<\/strong>&nbsp;<strong>static<\/strong>&nbsp;boolean&nbsp;<strong>containsBean<\/strong>(String&nbsp;name)\n&nbsp;&nbsp;&nbsp;&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>return<\/strong>&nbsp;beanFactory.containsBean(name);\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n&nbsp;&nbsp;&nbsp;&nbsp;\/**\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;\u5224\u65ad\u4ee5\u7ed9\u5b9a\u540d\u5b57\u6ce8\u518c\u7684bean\u5b9a\u4e49\u662f\u4e00\u4e2asingleton\u8fd8\u662f\u4e00\u4e2aprototype\u3002&nbsp;\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&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;<strong>@param<\/strong>&nbsp;name\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;<strong>@return<\/strong>&nbsp;boolean\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;<strong>@throws<\/strong>&nbsp;NoSuchBeanDefinitionException\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*\/\n&nbsp;&nbsp;&nbsp;&nbsp;<strong>public<\/strong>&nbsp;<strong>static<\/strong>&nbsp;boolean&nbsp;<strong>isSingleton<\/strong>(String&nbsp;name)&nbsp;<strong>throws<\/strong>&nbsp;NoSuchBeanDefinitionException\n&nbsp;&nbsp;&nbsp;&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>return<\/strong>&nbsp;beanFactory.isSingleton(name);\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n&nbsp;&nbsp;&nbsp;&nbsp;\/**\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;<strong>@param<\/strong>&nbsp;name\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;<strong>@return<\/strong>&nbsp;Class&nbsp;\u6ce8\u518c\u5bf9\u8c61\u7684\u7c7b\u578b\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;<strong>@throws<\/strong>&nbsp;NoSuchBeanDefinitionException\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*\/\n&nbsp;&nbsp;&nbsp;&nbsp;<strong>public<\/strong>&nbsp;<strong>static<\/strong>&nbsp;Class&lt;?&gt;&nbsp;getType(String&nbsp;name)&nbsp;<strong>throws<\/strong>&nbsp;NoSuchBeanDefinitionException\n&nbsp;&nbsp;&nbsp;&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>return<\/strong>&nbsp;beanFactory.getType(name);\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n&nbsp;&nbsp;&nbsp;&nbsp;\/**\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;\u5982\u679c\u7ed9\u5b9a\u7684bean\u540d\u5b57\u5728bean\u5b9a\u4e49\u4e2d\u6709\u522b\u540d\uff0c\u5219\u8fd4\u56de\u8fd9\u4e9b\u522b\u540d\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;<strong>@param<\/strong>&nbsp;name\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;<strong>@return<\/strong>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;<strong>@throws<\/strong>&nbsp;NoSuchBeanDefinitionException\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*\/\n&nbsp;&nbsp;&nbsp;&nbsp;<strong>public<\/strong>&nbsp;<strong>static<\/strong>&nbsp;String&#91;]&nbsp;getAliases(String&nbsp;name)&nbsp;<strong>throws<\/strong>&nbsp;NoSuchBeanDefinitionException\n&nbsp;&nbsp;&nbsp;&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>return<\/strong>&nbsp;beanFactory.getAliases(name);\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n&nbsp;&nbsp;&nbsp;&nbsp;\/**\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;\u83b7\u53d6aop\u4ee3\u7406\u5bf9\u8c61\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;<strong>@param<\/strong>&nbsp;invoker\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;<strong>@return<\/strong>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*\/\n&nbsp;&nbsp;&nbsp;&nbsp;@SuppressWarnings(\"unchecked\")\n&nbsp;&nbsp;&nbsp;&nbsp;<strong>public<\/strong>&nbsp;<strong>static<\/strong>&nbsp;&lt;T&gt;&nbsp;T&nbsp;<strong>getAopProxy<\/strong>(T&nbsp;invoker)\n&nbsp;&nbsp;&nbsp;&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>return<\/strong>&nbsp;(T)&nbsp;AopContext.currentProxy();\n&nbsp;&nbsp;&nbsp;&nbsp;}\n}\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"outline_9\">\u6269\u5c55<\/h2>\n\n\n\n<p><code>BeanFactory<\/code>\u548c<code>ApplicationContext<\/code>\u662fSpring\u7684\u4e24\u5927\u6838\u5fc3\u63a5\u53e3\uff0c\u90fd\u53ef\u4ee5\u5f53\u505aSpring\u7684\u5bb9\u5668\u3002\u5176\u4e2d<code>ApplicationContext<\/code>\u662f<code>BeanFactory<\/code>\u7684\u5b50\u63a5\u53e3\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"outline_10\">BeanFactory<\/h3>\n\n\n\n<p>\uff081\uff09\u3001\u662fSpring\u91cc\u9762\u6700\u5e95\u5c42\u7684\u63a5\u53e3\uff08\u6700\u539f\u59cb\u7684\u63a5\u53e3\uff09\uff0c\u5305\u542b\u4e86\u5404\u79cdBean\u7684\u5b9a\u4e49\uff0c\u8bfb\u53d6bean\u914d\u7f6e\u6587\u6863\uff0c\u7ba1\u7406bean\u7684\u52a0\u8f7d\u3001\u5b9e\u4f8b\u5316\uff0c\u63a7\u5236bean\u7684\u751f\u547d\u5468\u671f\uff0c\u7ef4\u62a4bean\u4e4b\u95f4\u7684\u4f9d\u8d56\u5173\u7cfb\u3002<\/p>\n\n\n\n<p>\uff082\uff09\u3001\u91c7\u7528\u7684\u662f\u5ef6\u8fdf\u52a0\u8f7d\u5f62\u5f0f\u6765\u6ce8\u5165Bean\u7684\uff0c\u5373\u53ea\u6709\u5728\u4f7f\u7528\u5230\u67d0\u4e2aBean\u65f6(\u8c03\u7528<code>getBean()<\/code>)\uff0c\u624d\u5bf9\u8be5Bean\u8fdb\u884c\u52a0\u8f7d\u5b9e\u4f8b\u5316\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\uff0c<code>BeanFacotry<\/code>\u52a0\u8f7d\u540e\uff0c\u76f4\u81f3\u7b2c\u4e00\u6b21\u4f7f\u7528\u8c03\u7528getBean\u65b9\u6cd5\u624d\u4f1a\u629b\u51fa\u5f02\u5e38\u3002<\/p>\n\n\n\n<p>\uff083\uff09<code>BeanFactory<\/code>\u901a\u5e38\u4ee5\u7f16\u7a0b\u7684\u65b9\u5f0f\u88ab\u521b\u5efa\u3002<\/p>\n\n\n\n<p>\uff084\uff09<code>BeanFactory<\/code>\u548c<code>ApplicationContext<\/code>\u90fd\u652f\u6301<code>BeanPostProcessor<\/code>\u3001<code>BeanFactoryPostProcessor<\/code>\u7684\u4f7f\u7528\uff0c\u4f46\u4e24\u8005\u4e4b\u95f4\u7684\u533a\u522b\u662f\uff1a<code>BeanFactory<\/code>\u9700\u8981\u624b\u52a8\u6ce8\u518c\uff0c\u800c<code>ApplicationContext<\/code>\u5219\u662f\u81ea\u52a8\u6ce8\u518c\u3002<\/p>\n\n\n\n<p>\uff085\uff09 \u5360\u7528\u5185\u5b58\u5c0f\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"outline_11\">ApplicationContext<\/h3>\n\n\n\n<p>1\u3001<code>ApplicationContext<\/code>\u63a5\u53e3\u4f5c\u4e3a<code>BeanFactory<\/code>\u7684\u6d3e\u751f\uff0c\u9664\u4e86\u63d0\u4f9b<code>BeanFactory<\/code>\u6240\u5177\u6709\u7684\u529f\u80fd\u5916\uff0c\u8fd8\u63d0\u4f9b\u4e86\u66f4\u5b8c\u6574\u7684\u6846\u67b6\u529f\u80fd\uff1a<\/p>\n\n\n\n<ul>\n<li>\u7ee7\u627f<code>MessageSource<\/code>\uff0c\u56e0\u6b64\u652f\u6301\u56fd\u9645\u5316\u3002<\/li>\n\n\n\n<li>\u7edf\u4e00\u7684\u8d44\u6e90\u6587\u4ef6\u8bbf\u95ee\u65b9\u5f0f\u3002<\/li>\n\n\n\n<li>\u63d0\u4f9b\u5728\u76d1\u542c\u5668\u4e2d\u6ce8\u518cbean\u7684\u4e8b\u4ef6\u3002<\/li>\n\n\n\n<li>\u540c\u65f6\u52a0\u8f7d\u591a\u4e2a\u914d\u7f6e\u6587\u4ef6\u3002<\/li>\n\n\n\n<li>\u8f7d\u5165\u591a\u4e2a\uff08\u6709\u7ee7\u627f\u5173\u7cfb\uff09\u4e0a\u4e0b\u6587 \uff0c\u4f7f\u5f97\u6bcf\u4e00\u4e2a\u4e0a\u4e0b\u6587\u90fd\u4e13\u6ce8\u4e8e\u4e00\u4e2a\u7279\u5b9a\u7684\u5c42\u6b21\uff0c\u6bd4\u5982\u5e94\u7528\u7684web\u5c42\u3002<\/li>\n<\/ul>\n\n\n\n<p>2\u3001<code>ApplicationContext<\/code>\uff0c\u5b83\u662f\u5728\u5bb9\u5668\u542f\u52a8\u65f6\uff0c\u4e00\u6b21\u6027\u521b\u5efa\u4e86\u6240\u6709\u7684Bean\u3002\u8fd9\u6837\uff0c\u5728\u5bb9\u5668\u542f\u52a8\u65f6\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u53d1\u73b0Spring\u4e2d\u5b58\u5728\u7684\u914d\u7f6e\u9519\u8bef\uff0c\u8fd9\u6837\u6709\u5229\u4e8e\u68c0\u67e5\u6240\u4f9d\u8d56\u5c5e\u6027\u662f\u5426\u6ce8\u5165\u3002<code>ApplicationContext<\/code>\u542f\u52a8\u540e\u9884\u8f7d\u5165\u6240\u6709\u7684\u5355\u5b9e\u4f8bBean\uff0c\u901a\u8fc7\u9884\u8f7d\u5165\u5355\u5b9e\u4f8bbean \uff0c\u786e\u4fdd\u5f53\u4f60\u9700\u8981\u7684\u65f6\u5019\uff0c\u4f60\u5c31\u4e0d\u7528\u7b49\u5f85\uff0c\u56e0\u4e3a\u5b83\u4eec\u5df2\u7ecf\u521b\u5efa\u597d\u4e86\u3002<\/p>\n\n\n\n<p>3\u3001<code>ApplicationContext<\/code>&nbsp;\u5360\u7528\u5185\u5b58\u7a7a\u95f4\u5927\uff0c\u5f53\u7a0b\u5e8f\u7684\u914d\u7f6ebean\u7279\u522b\u591a\u65f6\uff0c\u7a0b\u5e8f\u542f\u52a8\u6162\u3002<\/p>\n\n\n\n<p>4\u3001<code>ApplicationContext<\/code>&nbsp;\u80fd\u4ee5\u7f16\u7a0b\u5f0f\u65b9\u5f0f\u521b\u5efa\uff0c\u8fd8\u80fd\u80fd\u4ee5\u58f0\u660e\u7684\u65b9\u5f0f\u521b\u5efa\uff0c\u5982\u4f7f\u7528<code>ContextLoader<\/code>\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1\u3001\u5728\u521d\u59cb\u5316\u65f6\u4fdd\u5b58ApplicationContext\u5bf9\u8c61 \u9002\u7528\u4e8eSpring\u6846\u67b6\u7684\u72ec\u7acb\u5e94\u7528\u7a0b\u5e8f\uff0c\u987b\u8981\u7a0b\u5e8f\u901a\u8fc7 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":975,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10,8],"tags":[40],"_links":{"self":[{"href":"https:\/\/blog.frost-s.com\/index.php\/wp-json\/wp\/v2\/posts\/1043"}],"collection":[{"href":"https:\/\/blog.frost-s.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.frost-s.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.frost-s.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.frost-s.com\/index.php\/wp-json\/wp\/v2\/comments?post=1043"}],"version-history":[{"count":2,"href":"https:\/\/blog.frost-s.com\/index.php\/wp-json\/wp\/v2\/posts\/1043\/revisions"}],"predecessor-version":[{"id":1046,"href":"https:\/\/blog.frost-s.com\/index.php\/wp-json\/wp\/v2\/posts\/1043\/revisions\/1046"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.frost-s.com\/index.php\/wp-json\/wp\/v2\/media\/975"}],"wp:attachment":[{"href":"https:\/\/blog.frost-s.com\/index.php\/wp-json\/wp\/v2\/media?parent=1043"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.frost-s.com\/index.php\/wp-json\/wp\/v2\/categories?post=1043"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.frost-s.com\/index.php\/wp-json\/wp\/v2\/tags?post=1043"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}