{"id":10008410,"date":"2024-02-27T22:11:47","date_gmt":"2024-02-27T14:11:47","guid":{"rendered":"https:\/\/blog.itmax.cn\/?p=10008410"},"modified":"2024-02-27T22:11:47","modified_gmt":"2024-02-27T14:11:47","slug":"%e5%a6%82%e4%bd%95%e5%88%9b%e5%bb%ba%e8%87%aa%e5%b7%b1%e7%9a%84spring-boot-starter%e5%b9%b6%e4%b8%ba%e5%85%b6%e7%bc%96%e5%86%99%e5%8d%95%e5%85%83%e6%b5%8b%e8%af%95","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10008410","title":{"rendered":"\u5982\u4f55\u521b\u5efa\u81ea\u5df1\u7684Spring Boot Starter\u5e76\u4e3a\u5176\u7f16\u5199\u5355\u5143\u6d4b\u8bd5"},"content":{"rendered":"<p>\u5f53\u6211\u4eec\u60f3\u8981\u5c01\u88c5\u4e00\u4e9b\u81ea\u5b9a\u4e49\u529f\u80fd\u7ed9\u522b\u4eba\u4f7f\u7528\u7684\u65f6\u5019\uff0c\u521b\u5efaSpring Boot Starter\u7684\u5f62\u5f0f\u662f\u6700\u597d\u7684\u5b9e\u73b0\u65b9\u5f0f\u3002\u5982\u679c\u60a8\u8fd8\u4e0d\u4f1a\u6784\u5efa\u81ea\u5df1\u7684Spring Boot Starter\u7684\u8bdd\uff0c\u672c\u6587\u5c06\u5e26\u4f60\u4e00\u8d77\u521b\u5efa\u4e00\u4e2a\u81ea\u5df1\u7684Spring Boot Starter\u3002<\/p>\n<h2>\u5feb\u901f\u5165\u95e8<\/h2>\n<ol>\n<li>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684 Maven \u9879\u76ee\u3002\u7b2c\u4e09\u65b9\u5c01\u88c5\u7684\u547d\u540d\u683c\u5f0f\u662f <code>xxx-spring-boot-starter<\/code> \uff0c\u4f8b\u5982\uff1a<code>didispace-spring-boot-starter<\/code>\u3002<\/li>\n<li>\u7f16\u8f91<code>pom.xml<\/code>\uff0c\u6dfb\u52a0<code>spring-boot-autoconfigure<\/code>\u548c<code>spring-boot-starter<\/code>\u4f9d\u8d56<\/li>\n<\/ol>\n<pre><code class=\"xml\">&lt;dependencies&gt;\n    &lt;dependency&gt;\n        &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\n        &lt;artifactId&gt;spring-boot-starter&lt;\/artifactId&gt;\n    &lt;\/dependency&gt;\n    &lt;dependency&gt;\n        &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\n        &lt;artifactId&gt;spring-boot-autoconfigure&lt;\/artifactId&gt;\n    &lt;\/dependency&gt;\n&lt;\/dependencies&gt;<\/code><\/pre>\n<ol start=\"3\">\n<li>\u521b\u5efa\u4e00\u4e2a\u7528 <code>@Configuration<\/code> \u6ce8\u91ca\u7684\u914d\u7f6e\u7c7b\uff0c\u5728\u8fd9\u91cc\u60a8\u53ef\u4ee5\u4f7f\u7528<code>@Bean<\/code>\u6765\u521b\u5efa\u4f7f\u7528<code>@ConditionalOnClass<\/code>\u3001<code>@ConditionalOnMissingBean<\/code>\u7b49\u6761\u4ef6\u6ce8\u91ca\u6765\u63a7\u5236\u4f55\u65f6\u5e94\u7528\u914d\u7f6e\u3002<\/li>\n<\/ol>\n<pre><code class=\"java\">@Configuration\n@ConditionalOnClass(MyFeature.class)\n@ConditionalOnProperty(prefix = &quot;myfeature&quot;, name = &quot;enabled&quot;, matchIfMissing = true)\npublic class MyFeatureAutoConfiguration {\n\n    @Bean\n    @ConditionalOnMissingBean\n    public MyFeature myFeature() {\n        return new MyFeature();\n    }\n}<\/code><\/pre>\n<ol start=\"4\">\n<li>\u5728<code>src\/main\/resources\/META-INF<\/code>\u76ee\u5f55\u4e0b\u521b\u5efa<code>spring.factories<\/code>\u6587\u4ef6\uff0c\u5e76\u5728<code>org.springframework.boot.autoconfigure.EnableAutoConfiguration<\/code>\u5173\u952e\u5b57\u4e0b\u5217\u51fa\u60a8\u7684\u81ea\u52a8\u914d\u7f6e\u7c7b\uff0c\u6bd4\u5982\uff1a<\/li>\n<\/ol>\n<pre><code>org.springframework.boot.autoconfigure.EnableAutoConfiguration=\/\ncom.didispace.myfeature.MyFeatureAutoConfiguration<\/code><\/pre>\n<p>\u8be5\u914d\u7f6e\u7684\u4f5c\u7528\u662f\u8ba9Spring Boot\u5e94\u7528\u5728\u5f15\u5165\u60a8\u81ea\u5b9a\u4e49Starter\u7684\u65f6\u5019\u53ef\u4ee5\u81ea\u52a8\u8fd9\u91cc\u7684\u914d\u7f6e\u7c7b\u3002<\/p>\n<blockquote><p>\u6ce8\u610f\uff1aSpring Boot 2.7\u5f00\u59cb\uff0c\u4e0d\u518d\u63a8\u8350\u4f7f\u7528<code>spring.factories<\/code>\uff0c\u800c\u662f\u6539\u7528<code>\/META-INF\/spring\/org.springframework.boot.autoconfigure.AutoConfiguration.imports<\/code>\uff0c\u6587\u4ef6\u5185\u5bb9\u76f4\u63a5\u653e\u9700\u8981\u81ea\u52a8\u52a0\u8f7d\u914d\u7f6e\u7c7b\u8def\u5f84\u5373\u53ef\u3002\u8fd9\u4e2a\u53d8\u66f4\u5177\u4f53\u53ef\u89c1\u4e4b\u524d\u7684\u8fd9\u7bc7\u6587\u7ae0\uff1a<a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=V9aTd5u3HUMmny%2B%2FglhmcA%3D%3D.HHZQRcwzu7Bx7NcONfX5HreYK2S7TkRDgvJrZ2EFDqeErTp6V0xFeuupbR1o19G6TSBUrrIhsNMjY%2FLmi6AGxZokfjima8bVbM4sLO9UiodNAhdeypFTlZ4kb1KG8zHd\" rel=\"noopener noreferrer\">\u300aSpring Boot 2.7\u5f00\u59cbspring.factories\u4e0d\u63a8\u8350\u4f7f\u7528\u4e86\u300b<\/a><\/p><\/blockquote>\n<h2>\u9a8c\u8bc1\u6d4b\u8bd5<\/h2>\n<p>\u5728\u5236\u4f5cSpring Boot Starter\u7684\u65f6\u5019\uff0c\u4e00\u5b9a\u8bb0\u5f97\u4f7f\u7528\u5355\u5143\u6d4b\u8bd5\u6765\u9a8c\u8bc1\u548c\u786e\u4fdd\u81ea\u52a8\u5316\u914d\u7f6e\u7c7b\u5728\u4efb\u4f55\u6761\u4ef6\u903b\u8f91\u5728\u542f\u52a8\u5668\u4e0b\u80fd\u591f\u6309\u7167\u6b63\u786e\u7684\u9884\u671f\u8fd0\u884c\u3002<\/p>\n<h3>\u521b\u5efa\u5355\u5143\u6d4b\u8bd5<\/h3>\n<p>\u4f7f\u7528<code>@SpringBootTest<\/code>\u52a0\u8f7d\u5b8c\u6574\u7684\u5e94\u7528\u7a0b\u5e8f\u4e0a\u4e0b\u6587\uff0c\u5e76\u9a8c\u8bc1\u542f\u52a8\u7a0b\u5e8f\u662f\u5426\u6b63\u786e\u914d\u7f6e\u4e86 Bean \u548c\u5c5e\u6027\u3002<\/p>\n<pre><code class=\"java\">@SpringBootTest(classes = TestApplication.class)\npublic class MyStarterAutoConfigurationTest {\n\n    @Autowired(required = false)\n    private MyService myService;\n\n    @Test\n    public void testMyServiceAutoConfigured() {\n        assertNotNull(myService, &quot;MyService should be auto-configured&quot;);\n    }\n}<\/code><\/pre>\n<h3>\u8986\u76d6\u4e0d\u540c\u7684\u914d\u7f6e<\/h3>\n<p>\u5982\u679c\u6709\u4e0d\u540c\u7684\u914d\u7f6e\u65b9\u6848\uff0c\u90a3\u4e48\u8fd8\u9700\u8981\u4f7f\u7528<code>@TestPropertySource<\/code>\u6216<code>@DynamicPropertySource<\/code>\u8986\u76d6\u5c5e\u6027\u4ee5\u6d4b\u8bd5\u4e0d\u540c\u914d\u7f6e\u4e0b\u7684\u60c5\u51b5\u3002<\/p>\n<p>\u6216\u8005\u4e5f\u53ef\u4ee5\u76f4\u63a5\u7b80\u5355\u7684\u901a\u8fc7<code>@SpringBootTest<\/code>\u4e2d\u7684\u5c5e\u6027\u6765\u914d\u7f6e\uff0c\u6bd4\u5982\u4e0b\u9762\u8fd9\u6837\uff1a<\/p>\n<pre><code class=\"java\">@SpringBootTest(properties = &quot;my.starter.custom-property=customValue&quot;)\npublic class MyStarterPropertiesTest {\n\n    @Value(&quot;${my.starter.custom-property}&quot;)\n    private String customProperty;\n\n    @Test\n    public void testPropertyOverride() {\n        assertEquals(&quot;customValue&quot;, customProperty, &quot;Custom property should be overridden by @SpringBootTest&quot;);\n    }\n}<\/code><\/pre>\n<h3>\u8986\u76d6<code>@Conditional<\/code>\u7684\u4e0d\u540c\u5206\u652f<\/h3>\n<p>\u5982\u679c\u60a8\u7684\u542f\u52a8\u5668\u5305\u542b\u6761\u4ef6\u914d\u7f6e\uff0c\u6bd4\u5982\uff1a<code>@ConditionalOnProperty<\/code>\u3001<code>@ConditionalOnClass<\/code>\u7b49\u6ce8\u89e3\uff0c\u90a3\u4e48\u5c31\u5fc5\u987b\u7f16\u5199\u6d4b\u8bd5\u6765\u8986\u76d6\u6240\u6709\u6761\u4ef6\u4ee5\u9a8c\u8bc1\u662f\u5426\u5df2\u6b63\u786e\u3002<\/p>\n<p>\u6bd4\u5982\u4e0b\u9762\u8fd9\u6837\uff1a<\/p>\n<pre><code class=\"java\">@SpringBootTest(classes = {TestApplication.class, MyConditionalConfiguration.class})\n@ConditionalOnProperty(name = &quot;my.starter.enable&quot;, havingValue = &quot;true&quot;)\npublic class MyStarterConditionalTest {\n\n    @Autowired\n    private ApplicationContext context;\n\n    @Test\n    public void conditionalBeanNotLoadedWhenPropertyIsFalse() {\n        assertFalse(\n            context.containsBean(&quot;conditionalBean&quot;),\n            &quot;Conditional bean should not be loaded when 'my.starter.enable' is false&quot;\n        );\n    }\n}<\/code><\/pre>\n<p>\u4e3a\u4e86\u8986\u76d6\u4e0d\u540c\u7684\u6761\u4ef6\u5206\u652f\uff0c\u6211\u4eec\u901a\u5e38\u8fd8\u9700\u8981\u4f7f\u7528<code>@TestConfiguration<\/code>\u6ce8\u89e3\u6765\u6709\u9009\u62e9\u5730\u542f\u7528\u6216\u7981\u7528\u67d0\u4e9b\u81ea\u52a8\u914d\u7f6e\u3002<\/p>\n<h2>\u5c0f\u7ed3<\/h2>\n<p>\u672c\u6587\u4ecb\u7ecd\u4e86\u4e24\u4e2aSpring Boot\u7684\u8fdb\u9636\u5185\u5bb9\uff1a<\/p>\n<ol>\n<li>\u5982\u4f55\u521b\u5efa Spring Boot Starter<\/li>\n<li>\u5982\u4f55\u4e3a Spring Boot Starter \u63d0\u4f9b\u5355\u5143\u6d4b\u8bd5<\/li>\n<\/ol>\n<p>\u638c\u63e1\u8fd9\u9879\u6280\u80fd\u53ef\u4ee5\u5e2e\u4f60\u66f4\u597d\u7684\u4e3aSpring Boot\u63d0\u4f9b\u6a21\u5757\u5212\u7684\u529f\u80fd\u5c01\u88c5\u3002\u5982\u679c\u60a8\u5b66\u4e60\u8fc7\u7a0b\u4e2d\u5982\u9047\u56f0\u96be\uff1f\u53ef\u4ee5\u52a0\u5165\u6211\u4eec\u8d85\u9ad8\u8d28\u91cf\u7684<a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=0yF3uqmosWtV4aHetCv3ww%3D%3D.ruD2pYzZmfD24Uq6wa6Juc3XOr%2B6NaYvNz%2FXjt9zCoR6Fv8dMy95CL0cuNNNzvSZ\" rel=\"noopener noreferrer\">Spring\u6280\u672f\u4ea4\u6d41\u7fa4<\/a>\uff0c\u53c2\u4e0e\u4ea4\u6d41\u4e0e\u8ba8\u8bba\uff0c\u66f4\u597d\u7684\u5b66\u4e60\u4e0e\u8fdb\u6b65\uff01\u66f4\u591a<a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=F1ieZ83Kwu1%2BS%2FM6TJLgGw%3D%3D.p46ohBI3%2B5FIeBL9hqZTGjcvOvhcqfa3HEQlItBEwDCGRYETuVcJsC25H80%2F2Bfg\" rel=\"noopener noreferrer\">Spring Boot\u6559\u7a0b\u53ef\u4ee5\u70b9\u51fb\u76f4\u8fbe\uff01<\/a>\uff0c\u6b22\u8fce\u6536\u85cf\u4e0e\u8f6c\u53d1\u652f\u6301\uff01<\/p>\n<p>\u6700\u540e\u518d\u7ed9\u5927\u5bb6\u63a8\u8350\u4e00\u4e9b\u6709\u5173Spring Boot Starter\u548c\u81ea\u52a8\u5316\u914d\u7f6e\u7684\u6269\u5c55\u9605\u8bfb\uff1a<\/p>\n<ul>\n<li><a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=UPNc284bhhrqdEM8j8omEg%3D%3D.iVrIBVl0pLMuZNRgfDV24XS%2BlRBayKebkitI6KlM6mlJs8YGHXR99zP%2BYPE89qrnv52B%2FBgOvIhKzBC3m4R53uQLItNRW4Mgkkk9yXucj60%3D\" rel=\"noopener noreferrer\">Spring Boot Starter\u914d\u7f6espring.factories\u7684\u81ea\u52a8\u751f\u6210\u795e\u5668<\/a><\/li>\n<li><a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=GvGdfH%2B6ELuO3dsjsSsUlA%3D%3D.Tgcu38jneYUH1Oym1jfLSREYFqPmkLLBIyHwoYa8DBG9BCIc1A2xag0adud2J%2Ff5rstVhfW3y%2B%2FUmjSkVHRnGw%3D%3D\" rel=\"noopener noreferrer\">Spring Boot\u81ea\u52a8\u5316\u914d\u7f6e\u7684\u5229\u5f0a\u53ca\u89e3\u51b3\u4e4b\u9053<\/a><\/li>\n<\/ul>\n<blockquote><p>\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u516c\u4f17\u53f7\uff1a\u7a0b\u5e8f\u733fDD\u3002\u7b2c\u4e00\u65f6\u95f4\u4e86\u89e3\u524d\u6cbf\u884c\u4e1a\u6d88\u606f\u3001\u5206\u4eab\u6df1\u5ea6\u6280\u672f\u5e72\u8d27\u3001\u83b7\u53d6\u4f18\u8d28\u5b66\u4e60\u8d44\u6e90<\/p><\/blockquote>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000044662701\">https:\/\/segmentfault.com\/a\/1190000044662701<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u6211\u4eec\u60f3\u8981\u5c01\u88c5\u4e00\u4e9b\u81ea\u5b9a\u4e49\u529f\u80fd\u7ed9\u522b\u4eba\u4f7f\u7528\u7684\u65f6\u5019\uff0c\u521b\u5efaSpring Boot Starter\u7684\u5f62\u5f0f\u662f\u6700\u597d\u7684\u5b9e\u73b0\u65b9\u5f0f [&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-10008410","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\/10008410","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=10008410"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10008410\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10008410"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10008410"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10008410"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}