{"id":10008081,"date":"2023-07-12T11:00:40","date_gmt":"2023-07-12T03:00:40","guid":{"rendered":"https:\/\/ityet.com\/?p=10008081"},"modified":"2023-07-12T11:00:40","modified_gmt":"2023-07-12T03:00:40","slug":"spring-%e4%b8%ad%e7%9a%84%e7%88%b6%e5%ad%90%e5%ae%b9%e5%99%a8%e6%98%af%e5%92%8b%e5%9b%9e%e4%ba%8b%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10008081","title":{"rendered":"Spring \u4e2d\u7684\u7236\u5b50\u5bb9\u5668\u662f\u548b\u56de\u4e8b\uff1f"},"content":{"rendered":"<p>@[toc]<br \/>\u76f8\u4fe1\u6709\u5c0f\u4f19\u4f34\u4e5f\u542c\u8bf4\u8fc7\uff0c\u5728 SSM \u9879\u76ee\u4e2d\uff0cSpring \u5bb9\u5668\u662f\u7236\u5bb9\u5668\uff0cSpringMVC \u662f\u5b50\u5bb9\u5668\uff0c\u5b50\u5bb9\u5668\u53ef\u4ee5\u8bbf\u95ee\u7236\u5bb9\u5668\u7684 Bean\uff0c\u4f46\u662f\u7236\u5bb9\u5668\u4e0d\u80fd\u8bbf\u95ee\u5b50\u5bb9\u5668\u7684 Bean\u3002<\/p>\n<p>\u66f4\u8fd1\u4e00\u6b65\uff0c\u6709\u5c0f\u4f19\u4f34\u53ef\u80fd\u4e5f\u4e86\u89e3\u8fc7\uff0c\u4e0d\u7528\u7236\u5b50\u5bb9\u5668\uff0c\u5355\u7eaf\u5c31\u7528\u4e00\u4e2a SpringMVC \u5bb9\u5668\u4f3c\u4e4e\u4e5f\u53ef\u4ee5\uff0c\u9879\u76ee\u4e5f\u80fd\u8fd0\u884c\u3002<\/p>\n<p>\u90a3\u4e48\u73b0\u5728\u95ee\u9898\u6765\u4e86\uff1a\u65e2\u7136\u5355\u7eaf\u4e00\u4e2a SpringMVC \u5bb9\u5668\u5c31\u80fd\u4f7f\u9879\u76ee\u8dd1\u8d77\u6765\uff0c\u90a3\u6211\u4eec\u4e3a\u4ec0\u4e48\u8fd8\u8981\u7528\u7236\u5b50\u5bb9\u5668\uff1f\u7236\u5b50\u5bb9\u5668\u7684\u4f18\u52bf\u662f\u4ec0\u4e48\uff1f<\/p>\n<p>\u5e26\u7740\u8fd9\u4e2a\u95ee\u9898\uff0c\u4eca\u5929\u677e\u54e5\u6765\u548c\u5c0f\u4f19\u4f34\u4eec\u804a\u4e00\u804a\u7236\u5b50\u5bb9\u5668\u3002<\/p>\n<h2>1. \u7236\u5b50\u5bb9\u5668<\/h2>\n<p>\u9996\u5148\uff0c\u5176\u5b9e\u7236\u5b50\u8fd9\u79cd\u8bbe\u8ba1\u5f88\u5e38\u89c1\uff0c\u677e\u54e5\u8bb0\u5f97\u5728\u4e4b\u524d\u7684 Spring Security \u7684\u7cfb\u5217\u6587\u7ae0\u4e2d\uff0cSpring Security \u4e2d\u7684 AuthenticationManager \u5176\u5b9e\u4e5f\u662f\u7c7b\u4f3c\u7684\u8bbe\u8ba1\uff0c\u4f30\u8ba1\u90a3\u91cc\u5c31\u662f\u501f\u9274\u4e86 Spring \u4e2d\u7684\u7236\u5b50\u5bb9\u5668\u8bbe\u8ba1\u3002<\/p>\n<p>\u5f53\u4f7f\u7528\u4e86\u7236\u5b50\u5bb9\u5668\u4e4b\u540e\uff0c\u5982\u679c\u53bb\u7236\u5bb9\u5668\u4e2d\u67e5\u627e Bean\uff0c\u90a3\u4e48\u5c31\u5355\u7eaf\u7684\u5728\u7236\u5bb9\u5668\u4e2d\u67e5\u627e Bean\uff1b\u5982\u679c\u662f\u53bb\u5b50\u5bb9\u5668\u4e2d\u67e5\u627e Bean\uff0c\u90a3\u4e48\u5c31\u4f1a\u5148\u5728\u5b50\u5bb9\u5668\u4e2d\u67e5\u627e\uff0c\u627e\u5230\u4e86\u5c31\u8fd4\u56de\uff0c\u6ca1\u627e\u5230\u5219\u7ee7\u7eed\u53bb\u7236\u5bb9\u5668\u4e2d\u67e5\u627e\uff0c\u76f4\u5230\u627e\u5230\u4e3a\u6b62\uff08\u628a\u7236\u5bb9\u5668\u90fd\u627e\u5b8c\u4e86\u8fd8\u662f\u6ca1\u6709\u7684\u8bdd\uff0c\u90a3\u5c31\u53ea\u80fd\u629b\u5f02\u5e38\u51fa\u6765\u4e86\uff09\u3002<\/p>\n<h2>2. \u4e3a\u4ec0\u4e48\u9700\u8981\u7236\u5b50\u5bb9\u5668<\/h2>\n<h3>2.1 \u95ee\u9898\u5448\u73b0<\/h3>\n<p>\u4e3a\u4ec0\u4e48\u9700\u8981\u7236\u5b50\u5bb9\u5668\uff1f\u8001\u8001\u5b9e\u5b9e\u4f7f\u7528\u4e00\u4e2a\u5bb9\u5668\u4e0d\u884c\u5417\uff1f<\/p>\n<p>\u65e2\u7136 Spring \u5bb9\u5668\u4e2d\u6709\u7236\u5b50\u5bb9\u5668\uff0c\u90a3\u4e48\u8fd9\u4e2a\u73a9\u610f\u5c31\u5fc5\u7136\u6709\u5176\u4f7f\u7528\u573a\u666f\u3002<\/p>\n<p>\u677e\u54e5\u4e3e\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\u3002<\/p>\n<p>\u5047\u8bbe\u6211\u6709\u4e00\u4e2a\u591a\u6a21\u5757\u9879\u76ee\uff0c\u5176\u4e2d\u6709\u5546\u5bb6\u6a21\u5757\u548c\u5ba2\u6237\u6a21\u5757\uff0c\u5546\u5bb6\u6a21\u5757\u548c\u5ba2\u6237\u6a21\u5757\u4e2d\u90fd\u6709\u89d2\u8272\u7ba1\u7406 RoleService\uff0c\u9879\u76ee\u7ed3\u6784\u5982\u4e0b\u56fe\uff1a<\/p>\n<pre><code>\u251c\u2500\u2500 admin\n\u2502   \u251c\u2500\u2500 pom.xml\n\u2502   \u2514\u2500\u2500 src\n\u2502       \u251c\u2500\u2500 main\n\u2502       \u2502   \u251c\u2500\u2500 java\n\u2502       \u2502   \u2514\u2500\u2500 resources\n\u251c\u2500\u2500 consumer\n\u2502   \u251c\u2500\u2500 pom.xml\n\u2502   \u2514\u2500\u2500 src\n\u2502       \u251c\u2500\u2500 main\n\u2502       \u2502   \u251c\u2500\u2500 java\n\u2502       \u2502   \u2502   \u2514\u2500\u2500 org\n\u2502       \u2502   \u2502       \u2514\u2500\u2500 javaboy\n\u2502       \u2502   \u2502           \u2514\u2500\u2500 consumer\n\u2502       \u2502   \u2502               \u2514\u2500\u2500 RoleService.java\n\u2502       \u2502   \u2514\u2500\u2500 resources\n\u2502       \u2502       \u2514\u2500\u2500 consumer_beans.xml\n\u251c\u2500\u2500 merchant\n\u2502   \u251c\u2500\u2500 pom.xml\n\u2502   \u2514\u2500\u2500 src\n\u2502       \u251c\u2500\u2500 main\n\u2502       \u2502   \u251c\u2500\u2500 java\n\u2502       \u2502   \u2502   \u2514\u2500\u2500 org\n\u2502       \u2502   \u2502       \u2514\u2500\u2500 javaboy\n\u2502       \u2502   \u2502           \u2514\u2500\u2500 merchant\n\u2502       \u2502   \u2502               \u2514\u2500\u2500 RoleService.java\n\u2502       \u2502   \u2514\u2500\u2500 resources\n\u2502       \u2502       \u2514\u2500\u2500 merchant_beans.xml\n\u2514\u2500\u2500 pom.xml<\/code><\/pre>\n<p>\u73b0\u5728 consumer \u548c merchant \u4e2d\u90fd\u6709\u4e00\u4e2a RoleService \u7c7b\uff0c\u7136\u540e\u5728\u5404\u81ea\u7684\u914d\u7f6e\u6587\u4ef6\u4e2d\uff0c\u90fd\u5c06\u8be5\u7c7b\u6ce8\u518c\u5230 Spring \u5bb9\u5668\u4e2d\u3002<\/p>\n<p>org.javaboy.consumer.RoleService\uff1a<\/p>\n<pre><code class=\"java\">public class RoleService {\n    public String hello() {\n        return &quot;hello consumer&quot;;\n    }\n}<\/code><\/pre>\n<p>org.javaboy.merchant.RoleService\uff1a<\/p>\n<pre><code class=\"java\">public class RoleService {\n    public String hello() {\n        return &quot;hello merchant&quot;;\n    }\n}<\/code><\/pre>\n<p>consumer_beans.xml \u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"xml\">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;\n&lt;beans xmlns=&quot;http:\/\/www.springframework.org\/schema\/beans&quot;\n       xmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot;\n       xsi:schemaLocation=&quot;http:\/\/www.springframework.org\/schema\/beans http:\/\/www.springframework.org\/schema\/beans\/spring-beans.xsd&quot;&gt;\n\n    &lt;bean class=&quot;org.javaboy.consumer.RoleService&quot; id=&quot;roleService&quot;\/&gt;\n&lt;\/beans&gt;<\/code><\/pre>\n<p>merchant_beans.xml \u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"xml\">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;\n&lt;beans xmlns=&quot;http:\/\/www.springframework.org\/schema\/beans&quot;\n       xmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot;\n       xsi:schemaLocation=&quot;http:\/\/www.springframework.org\/schema\/beans http:\/\/www.springframework.org\/schema\/beans\/spring-beans.xsd&quot;&gt;\n\n    &lt;bean class=&quot;org.javaboy.merchant.RoleService&quot; id=&quot;roleService&quot;\/&gt;\n&lt;\/beans&gt;<\/code><\/pre>\n<p>\u5927\u5bb6\u6ce8\u610f\uff0c\u8fd9\u4e24\u4e2a Bean \u540c\u540d\u3002<\/p>\n<p>\u73b0\u5728\uff0c\u5728 admin \u6a21\u5757\u4e2d\uff0c\u540c\u65f6\u4f9d\u8d56 consumer \u548c merchant\uff0c\u540c\u65f6\u52a0\u8f7d\u8fd9\u4e24\u4e2a\u914d\u7f6e\u6587\u4ef6\uff0c\u90a3\u4e48\u80fd\u4e0d\u80fd\u540c\u65f6\u5411 Spring \u5bb9\u5668\u4e2d\u6ce8\u518c\u4e24\u4e2a\u6765\u81ea\u4e0d\u540c\u6a21\u5757\u7684\u540c\u540d Bean \u5462\uff1f<\/p>\n<p>\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"java\">ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext();\nctx.setConfigLocations(&quot;consumer_beans.xml&quot;, &quot;merchant_beans.xml&quot;);\nctx.refresh();\norg.javaboy.merchant.RoleService rs1 = ctx.getBean(org.javaboy.merchant.RoleService.class);\norg.javaboy.consumer.RoleService rs2 = ctx.getBean(org.javaboy.consumer.RoleService.class);<\/code><\/pre>\n<p>\u8fd9\u4e2a\u6267\u884c\u4e4b\u540e\u4f1a\u629b\u51fa\u5982\u4e0b\u95ee\u9898\uff1a<\/p>\n<p><img decoding=\"async\"   referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043999229\" alt=\"\" title=\"\"><\/p>\n<p>\u5c0f\u4f19\u4f34\u4eec\u770b\u5230\uff0c\u8fd9\u4e2a\u662f\u627e\u4e0d\u5230 <code>org.javaboy.consumer.RoleService<\/code> \u670d\u52a1\uff0c\u4f46\u662f\u53e6\u5916\u4e00\u4e2a RoleService \u5176\u5b9e\u662f\u627e\u5230\u4e86\uff0c\u56e0\u4e3a\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u540e\u9762\u5b9a\u4e49\u7684\u540c\u540d Bean \u628a\u524d\u9762\u7684\u8986\u76d6\u4e86\uff0c\u6240\u4ee5\u6709\u4e00\u4e2a Bean \u5c31\u627e\u4e0d\u5230\u4e86\u3002<\/p>\n<p>\u5982\u679c\u4e0d\u5141\u8bb8 Bean \u7684\u8986\u76d6\uff0c\u90a3\u4e48\u53ef\u4ee5\u8fdb\u884c\u5982\u4e0b\u914d\u7f6e\uff1a<\/p>\n<pre><code class=\"java\">ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext();\nctx.setConfigLocations(&quot;consumer_beans.xml&quot;, &quot;merchant_beans.xml&quot;);\nctx.setAllowBeanDefinitionOverriding(false);\nctx.refresh();<\/code><\/pre>\n<p>\u6b64\u65f6\u4e00\u542f\u52a8\u5c31\u76f4\u63a5\u62a5\u9519\u4e86\uff1a<\/p>\n<p><img decoding=\"async\"   referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043999230\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u610f\u601d\u4e5f\u8bf4\u7684\u6bd4\u8f83\u660e\u786e\u4e86\uff0cBean \u7684\u5b9a\u4e49\u51b2\u7a81\u4e86\uff0c\u6240\u4ee5\u5b9a\u4e49\u5931\u8d25\u3002<\/p>\n<p>\u90a3\u4e48\u6709\u6ca1\u6709\u529e\u6cd5\u80fd\u591f\u4f18\u96c5\u7684\u89e3\u51b3\u4e0a\u9762\u8fd9\u4e2a\u95ee\u9898\u5462\uff1f\u7b54\u6848\u5c31\u662f\u7236\u5b50\u5bb9\u5668\uff01<\/p>\n<h3>2.2 \u7236\u5b50\u5bb9\u5668<\/h3>\n<p>\u5bf9\u4e8e\u4e0a\u9762\u7684\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06 consumer \u548c merchant \u914d\u7f6e\u6210\u7236\u5b50\u5173\u7cfb\u6216\u8005\u5144\u5f1f\u5173\u7cfb\uff0c\u5c31\u80fd\u5f88\u597d\u7684\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u4e86\u3002<\/p>\n<h4>2.2.1 \u5144\u5f1f\u5173\u7cfb<\/h4>\n<p>\u5148\u6765\u770b\u5144\u5f1f\u5173\u7cfb\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"java\">ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext();\nClassPathXmlApplicationContext child1 = new ClassPathXmlApplicationContext(&quot;consumer_beans.xml&quot;);\nClassPathXmlApplicationContext child2 = new ClassPathXmlApplicationContext(&quot;merchant_beans.xml&quot;);\nchild1.setParent(ctx);\nchild2.setParent(ctx);\nctx.setAllowBeanDefinitionOverriding(false);\nctx.refresh();\norg.javaboy.consumer.RoleService rs1 = child1.getBean(org.javaboy.consumer.RoleService.class);\norg.javaboy.merchant.RoleService rs2 = child2.getBean(org.javaboy.merchant.RoleService.class);\nSystem.out.println(&quot;rs1.hello() = &quot; + rs1.hello());\nSystem.out.println(&quot;rs2.hello() = &quot; + rs2.hello());<\/code><\/pre>\n<p>\u5c0f\u4f19\u4f34\u4eec\u770b\u4e00\u4e0b\uff0c\u8fd9\u79cd\u9488\u5bf9 consumer \u548c merchant \u5206\u522b\u521b\u5efa\u4e86\u5bb9\u5668\uff0c\u8fd9\u79cd\u5bb9\u5668\u5173\u7cfb\u5c31\u662f\u5144\u5f1f\u5bb9\u5668\uff0c\u8fd9\u4e24\u4e2a\u5144\u5f1f\u6709\u4e00\u4e2a\u5171\u540c\u7684 parent \u5c31\u662f ctx\uff0c\u73b0\u5728\u53ef\u4ee5\u5728\u5404\u4e2a\u5bb9\u5668\u4e2d\u83b7\u53d6\u5230\u81ea\u5df1\u7684 Bean \u4e86\u3002<\/p>\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u4e0a\u9762\u8fd9\u79cd\u7ed3\u6784\u4e2d\uff0c\u5b50\u5bb9\u5668\u53ef\u4ee5\u83b7\u53d6\u5230 parent \u7684 Bean\uff0c\u4f46\u662f\u65e0\u6cd5\u83b7\u53d6\u5230\u5144\u5f1f\u5bb9\u5668\u7684 Bean\uff0c\u5373\u5982\u679c consumer \u4e2d\u5f15\u7528\u4e86 merchant \u4e2d\u7684 Bean\uff0c\u90a3\u4e48\u4e0a\u9762\u8fd9\u4e2a\u914d\u7f6e\u5c31\u6709\u95ee\u9898\u4e86\u3002<\/p>\n<h4>2.2.2 \u7236\u5b50\u5173\u7cfb<\/h4>\n<p>\u73b0\u5728\u5047\u8bbe\u7528 consumer \u505a parent \u5bb9\u5668\uff0cmerchant \u505a child \u5bb9\u5668\uff0c\u90a3\u4e48\u914d\u7f6e\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"java\">ClassPathXmlApplicationContext parent = new ClassPathXmlApplicationContext(&quot;consumer_beans.xml&quot;);\nClassPathXmlApplicationContext child = new ClassPathXmlApplicationContext(&quot;merchant_beans.xml&quot;);\nchild.setParent(parent);\nchild.refresh();\norg.javaboy.consumer.RoleService rs1 = parent.getBean(org.javaboy.consumer.RoleService.class);\norg.javaboy.merchant.RoleService rs2 = child.getBean(org.javaboy.merchant.RoleService.class);\norg.javaboy.consumer.RoleService rs3 = child.getBean(org.javaboy.consumer.RoleService.class);\nSystem.out.println(&quot;rs1.hello() = &quot; + rs1.hello());\nSystem.out.println(&quot;rs2.hello() = &quot; + rs2.hello());\nSystem.out.println(&quot;rs3.hello() = &quot; + rs3.hello());<\/code><\/pre>\n<p>\u9996\u5148\u521b\u5efa\u4e24\u4e2a\u5bb9\u5668\uff0c\u5206\u522b\u662f parent \u548c child\uff0c\u7136\u540e\u4e3a child \u5bb9\u5668\u8bbe\u7f6e parent\uff0c\u8bbe\u7f6e\u5b8c\u6210\u540e\u8bb0\u5f97\u8981\u5237\u65b0 child \u5bb9\u5668\u3002<\/p>\n<p>\u73b0\u5728\u6211\u4eec\u5c31\u53ef\u4ee5\u4ece parent \u5bb9\u5668\u4e2d\u53bb\u83b7\u53d6 parent \u5bb9\u5668\u4e2d\u539f\u672c\u5c31\u5b58\u5728\u7684 Bean\uff0c\u4e5f\u53ef\u4ee5\u4ece child \u5bb9\u5668\u4e2d\u53bb\u83b7\u53d6 child \u5bb9\u5668\u539f\u672c\u7684 Bean \u6216\u8005\u662f parent \u7684 Bean \u90fd\u53ef\u4ee5\u3002<\/p>\n<p>\u8fd9\u5c31\u662f\u7236\u5b50\u5bb9\u5668\u3002<\/p>\n<p><strong>\u7236\u5bb9\u5668\u548c\u5b50\u5bb9\u5668\u672c\u8d28\u4e0a\u662f\u76f8\u4e92\u9694\u79bb\u7684\u4e24\u4e2a\u4e0d\u540c\u7684\u5bb9\u5668\uff0c\u6240\u4ee5\u5141\u8bb8\u540c\u540d\u7684 Bean \u5b58\u5728\u3002\u5f53\u5b50\u5bb9\u5668\u8c03\u7528 getBean \u65b9\u6cd5\u53bb\u83b7\u53d6\u4e00\u4e2a Bean \u7684\u65f6\u5019\uff0c\u5982\u679c\u5f53\u524d\u5bb9\u5668\u6ca1\u627e\u5230\uff0c\u5c31\u4f1a\u53bb\u7236\u5bb9\u5668\u67e5\u627e\uff0c\u4e00\u76f4\u5f80\u4e0a\u627e\uff0c\u627e\u5230\u4e3a\u6b62\u3002<\/strong><\/p>\n<p>\u6838\u5fc3\u5c31\u662f BeanFactory\uff0c\u8fd9\u4e2a\u677e\u54e5\u4e4b\u524d\u6587\u7ae0\u5df2\u7ecf\u548c\u5c0f\u4f19\u4f34\u4eec\u4ecb\u7ecd\u8fc7\u4e86\uff08<a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=bLVkFq0PgxYkfUeiyJapSA%3D%3D.0WGfmipcPHJsIlvS4BviZkRI2boblMyzuBogShMlaAsPhA3grfBUtlDU0O7yQTy1AGxc7bb2tmLyXuxlWyprQg%3D%3D\" rel=\"noopener noreferrer\">BeanFactoryPostProcessor \u548c BeanPostProcessor \u6709\u4ec0\u4e48\u533a\u522b\uff1f<\/a>\uff09\uff0cBeanFactory \u6709\u4e00\u4e2a\u5b50\u7c7b HierarchicalBeanFactory\uff0c\u770b\u540d\u5b57\u5c31\u662f\u5e26\u6709\u5c42\u7ea7\u5173\u7cfb\u7684 BeanFactory\uff1a<\/p>\n<pre><code class=\"java\">public interface HierarchicalBeanFactory extends BeanFactory {\n\n    \/**\n     * Return the parent bean factory, or {@code null} if there is none.\n     *\/\n    @Nullable\n    BeanFactory getParentBeanFactory();\n\n    \/**\n     * Return whether the local bean factory contains a bean of the given name,\n     * ignoring beans defined in ancestor contexts.\n     * &lt;p&gt;This is an alternative to {@code containsBean}, ignoring a bean\n     * of the given name from an ancestor bean factory.\n     * @param name the name of the bean to query\n     * @return whether a bean with the given name is defined in the local factory\n     * @see BeanFactory#containsBean\n     *\/\n    boolean containsLocalBean(String name);\n\n}<\/code><\/pre>\n<p>\u53ea\u8981\u662f HierarchicalBeanFactory \u7684\u5b50\u7c7b\u5c31\u80fd\u914d\u7f6e\u7236\u5b50\u5173\u7cfb\u3002\u7236\u5b50\u5173\u7cfb\u56fe\u5982\u4e0b\uff1a<\/p>\n<p><img decoding=\"async\"   referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043999231\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<h3>2.3 \u7279\u6b8a\u60c5\u51b5<\/h3>\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5e76\u4e0d\u662f\u6240\u6709\u7684\u83b7\u53d6 Bean \u7684\u65b9\u6cd5\u90fd\u652f\u6301\u7236\u5b50\u5173\u7cfb\u67e5\u627e\uff0c\u6709\u7684\u65b9\u6cd5\u53ea\u80fd\u5728\u5f53\u524d\u5bb9\u5668\u4e2d\u67e5\u627e\uff0c\u5e76\u4e0d\u4f1a\u53bb\u7236\u5bb9\u5668\u4e2d\u67e5\u627e\uff1a<\/p>\n<pre><code class=\"java\">ClassPathXmlApplicationContext parent = new ClassPathXmlApplicationContext(&quot;consumer_beans.xml&quot;);\nClassPathXmlApplicationContext child = new ClassPathXmlApplicationContext(&quot;merchant_beans.xml&quot;);\nchild.setParent(parent);\nchild.refresh();\nString[] names1 = child.getBeanNamesForType(org.javaboy.merchant.RoleService.class);\nString[] names2 = child.getBeanNamesForType(org.javaboy.consumer.RoleService.class);\nSystem.out.println(&quot;names1 = &quot; + Arrays.toString(names1));\nSystem.out.println(&quot;names2 = &quot; + Arrays.toString(names2));<\/code><\/pre>\n<p>\u5982\u4e0a\uff0c\u6839\u636e\u7c7b\u578b\u53bb\u67e5\u627e Bean \u540d\u79f0\u7684\u65f6\u5019\uff0c\u6211\u4eec\u6240\u7528\u7684\u662f getBeanNamesForType \u65b9\u6cd5\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u662f\u7531 ListableBeanFactory \u63a5\u53e3\u63d0\u4f9b\u7684\uff0c\u800c\u8be5\u63a5\u53e3\u548c HierarchicalBeanFactory \u63a5\u53e3\u5e76\u65e0\u7ee7\u627f\u5173\u7cfb\uff0c\u6240\u4ee5 getBeanNamesForType \u65b9\u6cd5\u5e76\u4e0d\u652f\u6301\u53bb\u7236\u5bb9\u5668\u4e2d\u67e5\u627e Bean\uff0c\u5b83\u53ea\u5728\u5f53\u524d\u5bb9\u5668\u4e2d\u67e5\u627e Bean\u3002<\/p>\n<p>\u4f46\u662f\uff01\u5982\u679c\u4f60\u786e\u5b9e\u6709\u9700\u6c42\uff0c\u5e0c\u671b\u80fd\u591f\u6839\u636e\u7c7b\u578b\u67e5\u627e Bean \u540d\u79f0\uff0c\u5e76\u4e14\u8fd8\u80fd\u591f\u81ea\u52a8\u53bb\u7236\u5bb9\u5668\u4e2d\u67e5\u627e\uff0c\u90a3\u4e48\u53ef\u4ee5\u4f7f\u7528 Spring \u7ed9\u6211\u4eec\u63d0\u4f9b\u7684\u5de5\u5177\u7c7b\uff0c\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"java\">ClassPathXmlApplicationContext parent = new ClassPathXmlApplicationContext(&quot;consumer_beans.xml&quot;);\nClassPathXmlApplicationContext child = new ClassPathXmlApplicationContext();\nchild.setParent(parent);\nchild.refresh();\nString[] names = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(child, org.javaboy.consumer.RoleService.class);\nfor (String name : names) {\n    System.out.println(&quot;name = &quot; + name);\n}<\/code><\/pre>\n<p>\u4e0d\u8fc7\u8fd9\u4e2a\u67e5\u627e\uff0c\u5bf9\u4e8e\u7236\u5b50\u5bb9\u5668\u4e2d\u540c\u540d\u7684 Bean \u662f\u67e5\u627e\u4e0d\u51fa\u6765\u540d\u5b57\u7684\u3002<\/p>\n<h3>2.4 Spring \u548c SpringMVC<\/h3>\n<p>\u4e0a\u9762\u7684\u5185\u5bb9\u7406\u89e3\u4e86\uff0cSpring \u548c SpringMVC \u4e4b\u95f4\u7684\u5173\u7cfb\u5c31\u597d\u7406\u89e3\u4e86\uff0cSpring \u662f\u7236\u5bb9\u5668\uff0cSpringMVC \u5219\u662f\u5b50\u5bb9\u5668\u3002<\/p>\n<p>\u5728 SpringMVC \u4e2d\uff0c\u521d\u59cb\u5316 DispatcherServlet \u7684\u65f6\u5019\uff0c\u4f1a\u521b\u5efa\u51fa SpringMVC \u5bb9\u5668\uff0c\u5e76\u4e14\u4e3a SpringMVC \u5bb9\u5668\u8bbe\u7f6e parent\uff0c\u76f8\u5173\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<p>FrameworkServlet#initWebApplicationContext\uff1a<\/p>\n<pre><code class=\"java\">protected WebApplicationContext initWebApplicationContext() {\n    WebApplicationContext rootContext =\n            WebApplicationContextUtils.getWebApplicationContext(getServletContext());\n    WebApplicationContext wac = null;\n    if (this.webApplicationContext != null) {\n        \/\/ A context instance was injected at construction time -&gt; use it\n        wac = this.webApplicationContext;\n        if (wac instanceof ConfigurableWebApplicationContext cwac &amp;&amp; !cwac.isActive()) {\n            \/\/ The context has not yet been refreshed -&gt; provide services such as\n            \/\/ setting the parent context, setting the application context id, etc\n            if (cwac.getParent() == null) {\n                \/\/ The context instance was injected without an explicit parent -&gt; set\n                \/\/ the root application context (if any; may be null) as the parent\n                cwac.setParent(rootContext);\n            }\n            configureAndRefreshWebApplicationContext(cwac);\n        }\n    }\n    if (wac == null) {\n        \/\/ No context instance was injected at construction time -&gt; see if one\n        \/\/ has been registered in the servlet context. If one exists, it is assumed\n        \/\/ that the parent context (if any) has already been set and that the\n        \/\/ user has performed any initialization such as setting the context id\n        wac = findWebApplicationContext();\n    }\n    if (wac == null) {\n        \/\/ No context instance is defined for this servlet -&gt; create a local one\n        wac = createWebApplicationContext(rootContext);\n    }\n    return wac;\n}<\/code><\/pre>\n<p>\u8fd9\u91cc\u7684 rootContext \u5c31\u662f\u7236\u5bb9\u5668\uff0cwac \u5c31\u662f\u5b50\u5bb9\u5668\uff0c\u65e0\u8bba\u54ea\u79cd\u65b9\u5f0f\u5f97\u5230\u7684\u5b50\u5bb9\u5668\uff0c\u90fd\u4f1a\u5c1d\u8bd5\u7ed9\u5176\u8bbe\u7f6e\u4e00\u4e2a\u7236\u5bb9\u5668\u3002<\/p>\n<p>\u5982\u679c\u6211\u4eec\u5728\u4e00\u4e2a Web \u9879\u76ee\u4e2d\uff0c\u4e0d\u5355\u72ec\u914d\u7f6e Spring \u5bb9\u5668\uff0c\u76f4\u63a5\u914d\u7f6e SpringMVC \u5bb9\u5668\uff0c\u7136\u540e\u5c06\u6240\u6709\u7684 Bean \u5168\u90e8\u90fd\u626b\u63cf\u5230 SpringMVC \u5bb9\u5668\u4e2d\uff0c\u8fd9\u6837\u505a\u662f\u6ca1\u6709\u95ee\u9898\u7684\uff0c\u9879\u76ee\u662f\u53ef\u4ee5\u6b63\u5e38\u8fd0\u884c\u7684\u3002\u4f46\u662f\u4e00\u822c\u9879\u76ee\u4e2d\u6211\u4eec\u8fd8\u662f\u4f1a\u628a\u8fd9\u4e24\u4e2a\u5bb9\u5668\u5206\u5f00\uff0c\u5206\u5f00\u6709\u5982\u4e0b\u51e0\u4e2a\u597d\u5904\uff1a<\/p>\n<ol>\n<li>\u65b9\u4fbf\u7ba1\u7406\uff0cSpringMVC \u4e3b\u8981\u5904\u7406\u63a7\u5236\u5c42\u76f8\u5173\u7684 Bean\uff0c\u5982 Controller\u3001\u89c6\u56fe\u89e3\u6790\u5668\u3001\u53c2\u6570\u5904\u7406\u5668\u7b49\u7b49\uff0c\u800c Spring \u5c42\u5219\u4e3b\u8981\u63a7\u5236\u4e1a\u52a1\u5c42\u76f8\u5173\u7684 Bean\uff0c\u5982 Service\u3001Mapper\u3001\u6570\u636e\u6e90\u3001\u4e8b\u52a1\u3001\u6743\u9650\u7b49\u7b49\u76f8\u5173\u7684 Bean\u3002<\/li>\n<li>\u5bf9\u4e8e\u65b0\u624b\u800c\u8a00\uff0c\u4e24\u4e2a\u5bb9\u5668\u5206\u5f00\u914d\u7f6e\uff0c\u53ef\u4ee5\u66f4\u597d\u7684\u7406\u89e3 Controller\u3001Service \u4ee5\u53ca Dao \u5c42\u7684\u5173\u7cfb\uff0c\u4e5f\u53ef\u4ee5\u907f\u514d\u5199\u51fa\u6765\u5728 Service \u5c42\u6ce8\u5165 Controller \u8fd9\u79cd\u8352\u5510\u4ee3\u7801\u3002<\/li>\n<\/ol>\n<p>\u53e6\u5916\u518d\u989d\u5916\u8bf4\u4e00\u53e5\uff0c\u6709\u7684\u5c0f\u4f19\u4f34\u53ef\u80fd\u4f1a\u95ee\uff0c\u5982\u679c\u5168\u90e8 Bean \u90fd\u626b\u63cf\u5230 Spring \u5bb9\u5668\u4e2d\u4e0d\u7528 SpringMVC \u5bb9\u5668\u884c\u4e0d\u884c\uff1f\u8fd9\u5176\u5b9e\u4e5f\u53ef\u4ee5\uff01\u4f46\u662f\u9700\u8981\u4e00\u4e9b\u989d\u5916\u7684\u914d\u7f6e\uff0c\u8fd9\u4e2a\u677e\u54e5\u4e0b\u7bc7\u6587\u7ae0\u518d\u6765\u548c\u5c0f\u4f19\u4f34\u4eec\u7ec6\u8ff0\u3002<\/p>\n<h2>3. \u5c0f\u7ed3<\/h2>\n<p>\u597d\u5566\uff0cSpring \u5bb9\u5668\u4e2d\u7684\u7236\u5b50\u5bb9\u5668\u73b0\u5728\u5927\u5bb6\u5e94\u8be5\u660e\u767d\u4e86\u5427\uff1f\u53ef\u4ee5\u7ed9\u975e ListableBeanFactory \u5bb9\u5668\u8bbe\u7f6e\u7236\u5bb9\u5668\uff0c\u7236\u5bb9\u5668\u4e0d\u53ef\u4ee5\u8bbf\u95ee\u5b50\u5bb9\u5668\u7684 Bean\uff0c\u4f46\u662f\u5b50\u5bb9\u5668\u53ef\u4ee5\u8bbf\u95ee\u7236\u5bb9\u5668\u7684 Bean\u3002<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000043999227\">https:\/\/segmentfault.com\/a\/1190000043999227<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>@[toc]\u76f8\u4fe1\u6709\u5c0f\u4f19\u4f34\u4e5f\u542c\u8bf4\u8fc7\uff0c\u5728 SSM \u9879\u76ee\u4e2d\uff0cSpring \u5bb9\u5668\u662f\u7236\u5bb9\u5668\uff0cSpringMVC \u662f\u5b50\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-10008081","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\/10008081","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=10008081"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10008081\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10008081"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10008081"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10008081"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}