{"id":149802,"date":"2020-09-02T05:05:08","date_gmt":"2020-09-01T21:05:08","guid":{"rendered":"http:\/\/4563.org\/?p=149802"},"modified":"2020-09-02T05:05:08","modified_gmt":"2020-09-01T21:05:08","slug":"springboot-%e5%90%af%e5%8a%a8%e8%bf%87%e7%a8%8b%e4%b8%ad%e6%9c%89%e4%bb%80%e4%b9%88%e5%8a%9e%e6%b3%95%e5%8f%af%e4%bb%a5%e5%8f%96%e6%b6%88%e6%8e%89%e6%9f%90%e4%b8%aa%e7%b1%bb%e4%b8%8a%e6%b3%a8%e9%87%8a","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=149802","title":{"rendered":"SpringBoot \u542f\u52a8\u8fc7\u7a0b\u4e2d\u6709\u4ec0\u4e48\u529e\u6cd5\u53ef\u4ee5\u53d6\u6d88\u6389\u67d0\u4e2a\u7c7b\u4e0a\u6ce8\u91ca\u7684 @ImportResource \u6ce8\u89e3\u5417"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  SpringBoot \u542f\u52a8\u8fc7\u7a0b\u4e2d\u6709\u4ec0\u4e48\u529e\u6cd5\u53ef\u4ee5\u53d6\u6d88\u6389\u67d0\u4e2a\u7c7b\u4e0a\u6ce8\u91ca\u7684 @ImportResource \u6ce8\u89e3\u5417               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : b0644170fc <\/span>  <span><i><\/i> 11<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u4ee5\u4e0b\u662f\u6211\u7684\u542f\u52a8\u7c7b,\u6211\u5de5\u7a0b\u662f\u6ce8\u89e3\u548c xml \u6df7\u7528.\u5de5\u7a0b\u7684 SpringBoot \u7248\u672c\u662f 2.3.X<\/p>\n<pre><code>@SpringBootApplication @ImportResource({\"classpath:spring\/*.xml\"}) public class A {  } <\/code><\/pre>\n<p>\u6211\u6709\u4ee5\u4e0b\u7684\u5355\u5143\u6d4b\u8bd5\u7c7b,\u6211\u53ea\u60f3\u505a\u6570\u636e\u5c42\u7684\u5355\u5143\u6d4b\u8bd5<\/p>\n<pre><code>@DataJdbcTest @AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) @Import([SpringBootConfiguration.class, MybatisPlusAutoConfiguration.class]) public class B {  } <\/code><\/pre>\n<p>\u5355\u5143\u6d4b\u8bd5\u542f\u52a8\u540e,\u4f1a\u626b\u63cf\u5230\u6211\u7684\u542f\u52a8\u7c7b A ,\u56e0\u6b64\u4f1a\u8bfb\u5165 *.xml; *.xml \u4e2d\u7684 bean \u90fd\u662f\u6211\u4e0d\u9700\u8981\u7684\uff0c\u800c\u4e14\u5b9e\u4f8b\u5316\u8d39\u65f6.\u6240\u4ee5\u6211\u60f3\u53d6\u6d88\u6389\u8fd9\u4e9b.\u6211\u60f3\u5230\u5982\u4e0b\u65b9\u6cd5\uff1a<\/p>\n<ol>\n<li>\u505a\u5355\u5143\u6d4b\u8bd5\u65f6,\u624b\u52a8\u628a A \u4e0a\u7684 @ImportResource \u6ce8\u91ca\u6389<\/li>\n<li>\u6269\u5c55 BeanDefinitionRegistryPostProcessor<\/li>\n<\/ol>\n<p>\u5176\u4e2d\u7b2c\u4e8c\u4e2a\u65b9\u6cd5,\u7531\u4e8e\u81ea\u5df1\u6269\u5c55\u7684\u5b9e\u73b0\u7c7b\u7684\u4f18\u5148\u7ea7\u80af\u5b9a\u4f4e\u4e8e Spring \u81ea\u5df1\u7684 ConfigurationClassPostProcessor,\u6240\u4ee5\u5b9e\u9645\u4e0a\u8fd0\u884c\u5230\u6211\u81ea\u5df1\u5b9e\u73b0\u7684\u65b9\u6cd5\u4e0a\u65f6, *.xml \u4e0a\u7684 bean \u5df2\u7ecf\u6ce8\u518c\u5230 ApplicationContext \u4e2d\u4e86,\u6240\u4ee5\u6211\u5c31\u5728\u81ea\u5df1\u7684\u5b9e\u73b0\u7c7b\u4e2d remove \u6389\u8fd9\u4e9b\u6211\u4e0d\u9700\u8981\u7684 bean. \u95ee\u9898\u662f\u9700\u8981 remove \u6389\u7684 bean \u6709\u70b9\u591a,\u4e5f\u6ca1\u6709\u4ec0\u4e48\u89c4\u5f8b,\u4e00\u4e2a\u4e2a\u624b\u5199\u4e0d\u592a\u73b0\u5b9e.\u6240\u4ee5\u6211\u89c9\u5f97\u6700\u597d\u7684\u529e\u6cd5\u5c31\u662f\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u53bb\u6389 A \u7c7b\u4e0a\u7684\u6ce8\u89e3 @ImportResource. \u5404\u4f4d\u5927\u4f6c\uff0c\u89c9\u5f97\u6709\u4ec0\u4e48\u529e\u6cd5\u53ef\u4ee5\u505a\u5230\u8fd9\u70b9. \u6211\u7528\u7684\u662f Junit,\u6240\u4ee5\u6709\u6ca1\u6709\u53ef\u80fd\u5728 Junit \u9636\u6bb5\u505a\u4e9b\u4ec0\u4e48\uff1f<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>5<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"3067773\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : guodong110 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5728\u6d4b\u8bd5\u76ee\u5f55\u5199\u4e2a\uff0c\u5305\u8def\u5f84\u548c\u4ee3\u7801\u76ee\u5f55 A \u4e00\u81f4<br \/>&#8220;`java<br \/>@SpringBootApplication<br \/>\/\/@ImportResource({&#8220;classpath:spring\/*.xml&#8221;})<br \/>public class A {<\/p>\n<p>}<br \/>&#8220;`                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3067774\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : guodong110 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u8865\u4e0a\u9762<br \/>\u6d4b\u8bd5\u7c7b\u7528\u81ea\u5b9a\u4e49\u7684\u542f\u52a8\u5165\u53e3<br \/>&#8220;`java<br \/>@SpringBootTest(classes = A.class)<br \/>public class xxx{<\/p>\n<p>}<\/p>\n<p>&#8220;`                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3067775\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : b0644170fc <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @guodong110 \u8c22\u8c22\u8fd9\u4e2a\u65b9\u6cd5\u53ef\u4ee5.\u4e0d\u8fc7\u8fd9\u4f1a\u5f71\u54cd\u5230\u5176\u5b83\u4eba\u5199\u7684\u6d4b\u8bd5\u7c7b                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3067776\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : b0644170fc <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @guodong110 \u8fd9\u6837\u662f\u53ef\u4ee5.                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3067777\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : guodong110 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u8bd5\u8bd5\u8fd9\u4e2a\uff0c\u8fd9\u6837\u81ea\u5df1\u7684\u6d4b\u8bd5\u7528\u4f8b\u53ea\u7528\u81ea\u5df1\u7684\u542f\u52a8\u7c7b\uff0c\u4e0d\u5f71\u54cd\u522b\u4eba<br \/>&#8220;`java<br \/>@SpringBootApplication<br \/>@ComponentScan(basePackages = &#8220;xxx&#8221;,<br \/> excludeFilters = @ComponentScan.Filter(SpringBootApplication.class))<br \/>\/\/@ImportResource({&#8220;classpath:spring\/*.xml&#8221;})<br \/>public class CustomeApplication {<\/p>\n<p>}<\/p>\n<p>@SpringBootTest(classes = CustomeApplication.class)<br \/>public class xxx{<\/p>\n<p>}<\/p>\n<p>&#8220;`                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>SpringBoot \u542f\u52a8\u8fc7\u7a0b\u4e2d\u6709&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/149802"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=149802"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/149802\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=149802"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=149802"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=149802"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}