{"id":12135,"date":"2023-11-28T22:44:41","date_gmt":"2023-11-28T14:44:41","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=12135"},"modified":"2023-12-11T15:29:30","modified_gmt":"2023-12-11T07:29:30","slug":"spring-zhong-beanfactory-shi-xian-you-na-xie","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/11\/28\/spring-zhong-beanfactory-shi-xian-you-na-xie\/","title":{"rendered":"Spring \u4e2d BeanFactory \u5b9e\u73b0\uff0c\u6709\u54ea\u4e9b\uff1f"},"content":{"rendered":"<p>Spring\u6846\u67b6\u63d0\u4f9b\u4e86\u591a\u79cd<code>BeanFactory<\/code>\u7684\u5b9e\u73b0\uff0c\u5176\u4e2d\u4e00\u4e9b\u4e3b\u8981\u7684\u5b9e\u73b0\u5305\u62ec\uff1a<\/p>\n<ol>\n<li>\n<strong>XmlBeanFactory\uff1a<\/strong><\/p>\n<ul>\n<li><code>XmlBeanFactory<\/code>\u662fSpring\u8f83\u65e9\u671f\u7684<code>BeanFactory<\/code>\u5b9e\u73b0\uff0c\u7528\u4e8e\u4eceXML\u914d\u7f6e\u6587\u4ef6\u4e2d\u8bfb\u53d6Bean\u7684\u5b9a\u4e49\u3002\u7531\u4e8e\u5b83\u5df2\u88ab\u5e9f\u5f03\uff0c\u63a8\u8350\u4f7f\u7528<code>ApplicationContext<\/code>\u53ca\u5176\u5b9e\u73b0\u7c7b\u4ee3\u66ff\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-java\">Resource resource = new ClassPathResource(&quot;beans.xml&quot;);\nBeanFactory beanFactory = new XmlBeanFactory(resource);\n<\/code><\/pre>\n<\/li>\n<li>\n<strong>DefaultListableBeanFactory\uff1a<\/strong><\/p>\n<ul>\n<li><code>DefaultListableBeanFactory<\/code>\u662f<code>BeanFactory<\/code>\u7684\u9ed8\u8ba4\u5b9e\u73b0\uff0c\u652f\u6301XML\u914d\u7f6e\u548c\u6ce8\u89e3\u914d\u7f6e\u3002\u5b83\u662fSpring\u4e2d\u6700\u5e38\u7528\u7684<code>BeanFactory<\/code>\u5b9e\u73b0\u4e4b\u4e00\uff0c\u4e5f\u662f<code>ApplicationContext<\/code>\u7684\u57fa\u7840\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-java\">DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();\nXmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory);\nreader.loadBeanDefinitions(new ClassPathResource(&quot;beans.xml&quot;));\n<\/code><\/pre>\n<\/li>\n<li>\n<strong>GenericApplicationContext\uff1a<\/strong><\/p>\n<ul>\n<li><code>GenericApplicationContext<\/code>\u662f<code>ApplicationContext<\/code>\u7684\u901a\u7528\u5b9e\u73b0\uff0c\u4e5f\u662f<code>DefaultListableBeanFactory<\/code>\u7684\u5b50\u7c7b\u3002\u5b83\u652f\u6301XML\u914d\u7f6e\u3001\u6ce8\u89e3\u914d\u7f6e\u548cJava\u914d\u7f6e\uff0c\u5e76\u4e14\u662fSpring\u4e2d\u6bd4\u8f83\u7075\u6d3b\u7684<code>ApplicationContext<\/code>\u5b9e\u73b0\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-java\">GenericApplicationContext context = new GenericApplicationContext();\nnew XmlBeanDefinitionReader(context).loadBeanDefinitions(&quot;beans.xml&quot;);\ncontext.refresh();\n<\/code><\/pre>\n<\/li>\n<li>\n<strong>AnnotationConfigApplicationContext\uff1a<\/strong><\/p>\n<ul>\n<li><code>AnnotationConfigApplicationContext<\/code>\u662f\u57fa\u4e8e\u6ce8\u89e3\u914d\u7f6e\u7684<code>ApplicationContext<\/code>\u5b9e\u73b0\u3002\u5b83\u5141\u8bb8\u901a\u8fc7Java\u7c7b\u8fdb\u884c\u914d\u7f6e\uff0c\u800c\u4e0d\u662f\u4f7f\u7528XML\u6587\u4ef6\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-java\">AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();\ncontext.register(AppConfig.class);\ncontext.refresh();\n<\/code><\/pre>\n<\/li>\n<li>\n<strong>ClassPathXmlApplicationContext\uff1a<\/strong><\/p>\n<ul>\n<li><code>ClassPathXmlApplicationContext<\/code>\u662f<code>ApplicationContext<\/code>\u7684\u4e00\u79cd\u5b9e\u73b0\uff0c\u7528\u4e8e\u4ece\u7c7b\u8def\u5f84\u52a0\u8f7dXML\u914d\u7f6e\u6587\u4ef6\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-java\">ApplicationContext context = new ClassPathXmlApplicationContext(&quot;beans.xml&quot;);\n<\/code><\/pre>\n<\/li>\n<li>\n<strong>FileSystemXmlApplicationContext\uff1a<\/strong><\/p>\n<ul>\n<li><code>FileSystemXmlApplicationContext<\/code>\u662f<code>ApplicationContext<\/code>\u7684\u4e00\u79cd\u5b9e\u73b0\uff0c\u7528\u4e8e\u4ece\u6587\u4ef6\u7cfb\u7edf\u52a0\u8f7dXML\u914d\u7f6e\u6587\u4ef6\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-java\">ApplicationContext context = new FileSystemXmlApplicationContext(&quot;path\/to\/beans.xml&quot;);\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p>\u8fd9\u4e9b<code>BeanFactory<\/code>\u7684\u5b9e\u73b0\u63d0\u4f9b\u4e86\u4e0d\u540c\u7684\u7279\u6027\u548c\u7528\u6cd5\uff0c\u53ef\u4ee5\u6839\u636e\u9879\u76ee\u7684\u9700\u6c42\u9009\u62e9\u9002\u5408\u7684\u5b9e\u73b0\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u901a\u5e38\u4f1a\u4f7f\u7528<code>ApplicationContext<\/code>\u7684\u67d0\u4e2a\u5b9e\u73b0\uff0c\u56e0\u4e3a\u5b83\u9664\u4e86\u63d0\u4f9b<code>BeanFactory<\/code>\u7684\u529f\u80fd\u5916\uff0c\u8fd8\u5305\u542b\u4e86\u66f4\u591a\u7684\u4f01\u4e1a\u7ea7\u7279\u6027\uff0c\u5982\u4e8b\u4ef6\u5904\u7406\u3001AOP\u3001\u4e8b\u52a1\u7ba1\u7406\u7b49\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Spring\u6846\u67b6\u63d0\u4f9b\u4e86\u591a\u79cdBeanFactory\u7684\u5b9e\u73b0\uff0c\u5176\u4e2d\u4e00\u4e9b\u4e3b\u8981\u7684\u5b9e\u73b0\u5305\u62ec\uff1a XmlBeanFactory [&hellip;]<\/p>\n","protected":false},"author":9,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[121],"tags":[],"class_list":["post-12135","post","type-post","status-publish","format-standard","hentry","category-spring"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/12135","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/comments?post=12135"}],"version-history":[{"count":2,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/12135\/revisions"}],"predecessor-version":[{"id":47858,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/12135\/revisions\/47858"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=12135"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=12135"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=12135"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}