{"id":1759,"date":"2023-03-26T21:36:13","date_gmt":"2023-03-26T13:36:13","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1759"},"modified":"2023-04-23T21:25:25","modified_gmt":"2023-04-23T13:25:25","slug":"write-spring-boot-starter","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/26\/write-spring-boot-starter\/","title":{"rendered":"\u7f16\u5199\u81ea\u5df1\u7684Spring Boot Starter"},"content":{"rendered":"<h2>\u57fa\u672c\u6b65\u9aa4<\/h2>\n<ul>\n<li>\u5f15\u5165\u5bf9\u5e94\u7684\u4f9d\u8d56<\/li>\n<li>\u7f16\u5199\u5b9e\u73b0\u7c7b<\/li>\n<li>\u7f16\u5199\u914d\u7f6e\u6587\u4ef6\u8bfb\u53d6\u7c7b\uff0c\u4e3b\u8981\u6ce8\u89e3\u662f<code>@ConfigurationProperties<\/code>(\u914d\u7f6e\u7684\u503c\u4f8b\u5982<code>a.b<\/code>)<\/li>\n<li>\u7f16\u5199\u81ea\u52a8\u88c5\u914d\u7c7b<\/li>\n<li>\u7f16\u5199\u9ed8\u8ba4\u7684\u914d\u7f6e\u6587\u4ef6<code>resources\/META-INF\/spring.factories<\/code><\/li>\n<\/ul>\n<p><!-- more --><\/p>\n<p>\u5728<code>resources\/META-INF\/spring.factories<\/code>\u4e2d\u914d\u7f6e\u6211\u4eec\u7684\u81ea\u52a8\u88c5\u914d\u7c7b<\/p>\n<h2>\u5177\u4f53\u7f16\u7801<\/h2>\n<h3>\u5f15\u5165\u7684\u4f9d\u8d56<\/h3>\n<pre><code class=\"language-xml\">&lt;dependencies&gt;\n    &lt;dependency&gt;\n        &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\n        &lt;artifactId&gt;spring-boot-configuration-processor&lt;\/artifactId&gt;\n        &lt;optional&gt;true&lt;\/optional&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<p>\u8bf4\u660e\uff1a<\/p>\n<ul>\n<li>\u7b2c\u4e00\u4e2a\u4f9d\u8d56 \u4e3b\u8981\u662f\u4e3a\u7f16\u8bd1\u5668\u914d\u7f6e\u7684\uff0c\u53ef\u4ee5\u6839\u636e<code>yml<\/code>\u6216<code>properties<\/code>\u914d\u7f6e\u9879\u9f20\u6807\u53f3\u952e\uff0c\u70b9\u5230\u4f7f\u7528\u8fd9\u4e2a\u5c5e\u6027\u7684\u7c7b\u4e0a<\/li>\n<li>\u7b2c\u4e8c\u4e2a\u4f9d\u8d56 \u4e3b\u8981\u662f\u4e3a\u4e86\u81ea\u52a8\u88c5\u914d<\/li>\n<\/ul>\n<h3>\u7f16\u5199\u81ea\u5df1\u7684\u529f\u80fd\u5b9e\u73b0\u7c7b<\/h3>\n<p>\u4e3a\u4e86\u8bf4\u660e\u95ee\u9898 \u5b9e\u73b0\u7c7b\u7684\u4f5c\u7528\u5c31\u662f\u8fd4\u56de\u914d\u7f6e\u5b57\u7b26\u4e32\u7684hashcode<\/p>\n<pre><code class=\"language-java\">package cn.appblog;\n\n\/**\n * \u76ee\u6807\u529f\u80fd\u5b9e\u73b0\u7c7b\n *\/\npublic class HashCodeClient {\n    private String target;\n\n    public HashCodeClient(String target){\n        this.target = target;\n    }\n\n    public String getHashCode() {\n        return String.valueOf(this.target.hashCode());\n    }\n}<\/code><\/pre>\n<h3>\u7f16\u5199\u914d\u7f6e\u6587\u4ef6\u8bfb\u53d6\u7c7b<\/h3>\n<pre><code class=\"language-java\">package cn.appblog;\n\nimport org.springframework.boot.context.properties.ConfigurationProperties;\n\n@ConfigurationProperties(&quot;cn.appblog&quot;)\npublic class HashCodeProperties {\n\n    private String target;\n\n    public String getTarget() {\n        return target;\n    }\n\n    public void setTarget(String target) {\n        this.target = target;\n    }\n}<\/code><\/pre>\n<p>\u8fd9\u91cc\u6211\u4eec\u8981\u8bfb\u53d6\u7684\u914d\u7f6e\u5c31\u662f<code>cn.appblog.target<\/code>\u7684\u503c\uff0c<code>@ConfigurationProperties<\/code>\u6ce8\u89e3\u7684\u4f5c\u7528\u5c31\u662f\u8bfb\u53d6\u914d\u7f6e\u6587\u4ef6\u6307\u5b9a\u5c5e\u6027\u7684\u503c<\/p>\n<h3>\u7f16\u5199\u81ea\u52a8\u88c5\u914d\u7c7b<\/h3>\n<pre><code class=\"language-java\">package cn.appblog;\n\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.boot.autoconfigure.EnableAutoConfiguration;\nimport org.springframework.boot.autoconfigure.condition.ConditionalOnBean;\nimport org.springframework.boot.autoconfigure.condition.ConditionalOnClass;\nimport org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;\nimport org.springframework.boot.context.properties.EnableConfigurationProperties;\nimport org.springframework.context.annotation.Bean;\nimport org.springframework.context.annotation.Configuration;\n\n@Configuration\n@EnableConfigurationProperties(HashCodeProperties.class)\n@ConditionalOnClass(HashCodeClient.class)\npublic class HashCodeAutoConfigrution {\n\n    @Autowired\n    private HashCodeProperties hashCodeProperties;\n\n    @ConditionalOnMissingBean\n    @Bean\n    public HashCodeClient getHashCodeClient(){\n        return new HashCodeClient(hashCodeProperties.getTarget());\n    }\n}<\/code><\/pre>\n<p><strong>@Configuration<\/strong><\/p>\n<p>\u6807\u8bc6\u672c\u7c7b\u662f\u914d\u7f6e\u7c7b\uff08\u76f8\u5f53\u4e8espring\u4e2dapplication.xml\uff09<\/p>\n<p><strong>@EnableConfigurationProperties(HashCodeProperties.class)<\/strong><\/p>\n<p>\u5982\u679c<code>HashCodeProperties<\/code>\u4e2d\u6709\u6ce8\u89e3<code>@ConfigurationProperties<\/code>\u90a3\u4e48\u8fd9\u4e2a\u7c7b\u5c31\u4f1a\u88ab\u52a0\u5230spring\u4e0a\u4e0b\u6587\u7684\u5bb9\u5668\u4e2d\uff0c\u4e5f\u5c31\u662f\u53ef\u4ee5\u901a\u8fc7<code>@Autowire<\/code>\u6765\u6ce8\u5165<\/p>\n<p><strong>@ConditionalOnClass<\/strong><\/p>\n<p>\u5f53\u7c7b\u8def\u5f84\u4e0b\u6709\u6307\u5b9a\u7c7b\u7684\u60c5\u51b5\u4e0b\u624d\u8fdb\u884c\u4e0b\u4e00\u6b65<\/p>\n<p><strong>@ConditionalOnMissingBean<\/strong><\/p>\n<p>\u5f53spring\u5bb9\u5668\u4e2d\u6ca1\u6709\u8fd9\u4e2aBean\u65f6\u624d\u8fdb\u884c\u4e0b\u4e00\u6b65<\/p>\n<h3>\u8bbe\u7f6e\u81ea\u52a8\u52a0\u8f7d\u7684\u914d\u7f6e\u6587\u4ef6spring.factories<\/h3>\n<p>\u5728<code>resources\/META-INF<\/code>\u4e0b\u6dfb\u52a0<code>spring.factories<\/code>\u6307\u5b9a\u81ea\u52a8\u88c5\u914d\u7684\u7c7b\u4e5f\u53eb\u5165\u53e3\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-java\">org.springframework.boot.autoconfigure.EnableAutoConfiguration=\n  cn.appblog.HashCodeAutoConfigrution<\/code><\/pre>\n<h3>\u6dfb\u52a0\u6211\u4eec\u7684\u9ed8\u8ba4\u914d\u7f6e<\/h3>\n<p>\u5728<code>application.yml<\/code>\u4e2d\u6dfb\u52a0\u4e0b\u9762<\/p>\n<pre><code class=\"language-yml\">cn:\n  appblog:\n    target: www.appblog.cn<\/code><\/pre>\n<p>\u8fd9\u5c31\u6709\u4e86\u9ed8\u8ba4\u503c<\/p>\n<h3>\u53d1\u5e03\u672c\u5730\u4ed3\u5e93\u6216\u90e8\u7f72\u79c1\u6709\u4ed3\u5e93<\/h3>\n<p>\u901a\u8fc7<code>maven install<\/code>\u547d\u4ee4\u53d1\u5e03\u5728\u672c\u5730\uff0c\u7136\u540e\u5728\u5176\u4ed6\u9879\u76ee\u5f15\u5165\u8fd9\u4e2ajar\u4f9d\u8d56\uff0c\u6d4b\u8bd5\u65f6\u76f4\u63a5\u81ea\u52a8\u6ce8\u5165\u6211\u4eec\u7684bean\u5373\u53ef<\/p>\n<h2>\u6d4b\u8bd5\u662f\u5426\u6210\u529f<\/h2>\n<p>\u65b0\u5efaSpring Boot\u5de5\u7a0b\uff0c\u5f15\u5165\u4f9d\u8d56<\/p>\n<pre><code class=\"language-xml\">&lt;!--\u5f15\u5165\u81ea\u5b9a\u4e49\u7684starter--&gt;\n&lt;dependency&gt;\n    &lt;groupId&gt;com.test.springboot&lt;\/groupId&gt;\n    &lt;artifactId&gt;myself-spring-boot-starter&lt;\/artifactId&gt;\n    &lt;version&gt;1.0-SNAPSHOT&lt;\/version&gt;\n&lt;\/dependency&gt;<\/code><\/pre>\n<p>\u76f4\u63a5\u8fdb\u884c\u5355\u5143\u6d4b\u8bd5<\/p>\n<pre><code class=\"language-java\">package cn.appblog.test;\n\nimport cn.appblog.HashCodeClient;\nimport org.junit.Assert;\nimport org.junit.Before;\nimport org.junit.Test;\nimport org.junit.runner.RunWith;\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.boot.SpringBootConfiguration;\nimport org.springframework.boot.autoconfigure.SpringBootApplication;\nimport org.springframework.boot.test.context.SpringBootTest;\nimport org.springframework.test.context.ContextConfiguration;\nimport org.springframework.test.context.junit4.SpringJUnit4ClassRunner;\n\nimport javax.annotation.Resource;\n\nimport java.util.HashMap;\nimport java.util.Map;\n\nimport static org.junit.Assert.*;\n\n@RunWith(SpringJUnit4ClassRunner.class)\n@SpringBootTest\npublic class ControllerTest {\n\n    @Autowired\n    private HashCodeClient hashCodeClient;\n\n    @Test\n    public void testStarter(){\n        System.out.println(hashCodeClient.getHashCode());\n    }\n}<\/code><\/pre>\n<p>\u8fd0\u884c\u6210\u529f\u83b7\u53d6hashCode<\/p>\n<p>\u5728<code>application.yml<\/code>\u4e2d\u8986\u76d6\u539f\u6765\u7684\u5c5e\u6027<\/p>\n<pre><code class=\"language-yml\">cn:\n  appblog:\n    target: test<\/code><\/pre>\n<p>\u6d4b\u8bd5\u5f97hashCode\u4e0d\u4e00\u81f4\uff0c\u8bc1\u660e\u6210\u529f<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u57fa\u672c\u6b65\u9aa4 \u5f15\u5165\u5bf9\u5e94\u7684\u4f9d\u8d56 \u7f16\u5199\u5b9e\u73b0\u7c7b \u7f16\u5199\u914d\u7f6e\u6587\u4ef6\u8bfb\u53d6\u7c7b\uff0c\u4e3b\u8981\u6ce8\u89e3\u662f@ConfigurationPropert [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[41],"tags":[444],"class_list":["post-1759","post","type-post","status-publish","format-standard","hentry","category-spring-boot","tag-spring-boot-starter"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1759","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1759"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1759\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1759"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1759"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1759"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}