{"id":1758,"date":"2023-03-26T21:35:30","date_gmt":"2023-03-26T13:35:30","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1758"},"modified":"2023-04-23T21:25:48","modified_gmt":"2023-04-23T13:25:48","slug":"getting-started-with-implement-spring-boot-starter","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/26\/getting-started-with-implement-spring-boot-starter\/","title":{"rendered":"\u5b9e\u73b0\u4e00\u4e2aSpring Boot Starter\u5165\u95e8"},"content":{"rendered":"<h2>\u529f\u80fd\u5b9e\u73b0<\/h2>\n<p>Spring Boot Starter\u53ef\u4ee5\u5c06\u9700\u8981\u7684\u529f\u80fd\u6574\u5408\u8d77\u6765\uff0c\u65b9\u4fbf\u4f7f\u7528\u3002\u8fd9\u4e2a\u4f8b\u5b50\u662f\u4e00\u4e2a\u975e\u5e38\u7b80\u5355\u7684Starter\u5b9e\u73b0\uff0c\u5173\u952e\u5728\u4e8e\u8d70\u901a\u6d41\u7a0b\u3002\u9996\u5148\u5355\u72ec\u521b\u5efa\u4e00\u4e2amaven\u9879\u76ee\uff0c\u547d\u540d\u4e3a<code>greeter-spring-boot-starter<\/code>\u3002\u5728\u9879\u76ee\u4e2d\u521b\u5efa\u4e00\u4e2a\u914d\u7f6e\u5b9a\u4e49\u7c7b<code>GreeterProperties<\/code>\uff0c\u4e3b\u8981\u5b9a\u4e49\u4e00\u4e2a\u914d\u7f6e\u524d\u7f00\u548c\u5bf9\u5e94\u7684\u5c5e\u6027\uff0c\u4e00\u4e2astarter\u6838\u5fc3\u7c7b<code>GreeterAutoConfiguration<\/code>\uff0c\u901a\u8fc7\u8fd9\u4e2a\u5b9e\u73b0\u57fa\u4e8estarter\u65b9\u5f0f\u7684\u52a0\u8f7d\uff0c\u5982\u4e0b\uff1a<\/p>\n<p><!-- more --><\/p>\n<pre><code class=\"language-java\">@Configuration\n@ConditionalOnClass(Greeter.class)\n@ConditionalOnProperty(prefix = &quot;appblog.greeter&quot;,value = &quot;enable&quot;,matchIfMissing = true)\n@EnableConfigurationProperties(GreeterProperties.class)\npublic class GreeterAutoConfiguration {\n\n    @Bean\n    @ConditionalOnMissingBean\n    public Greeter greeter() {\n        return new Greeter();\n    }\n}<\/code><\/pre>\n<p>\u6700\u540e\u4e00\u4e2a\u4e1a\u52a1\u7c7b<code>Greeter<\/code>\uff0c\u4e00\u4e2a\u6839\u636e\u5f53\u524d\u65f6\u95f4\u6253\u62db\u547c\u7684\u903b\u8f91\uff0c\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-java\">package cn.appblog.autoconfigure;\n\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.stereotype.Service;\n\nimport java.util.Calendar;\n\n@Service\npublic class Greeter {\n\n    @Autowired\n    private GreeterProperties greeterProperties;\n\n    private String sayMorning() {\n        return greeterProperties.getMorningMsg();\n    }\n\n    private String sayAfternoon() {\n        return greeterProperties.getAfternoonMsg();\n    }\n\n    private String sayEvening() {\n        return greeterProperties.getEveningMsg();\n    }\n\n    \/**\n     * \u5916\u90e8\u8c03\u7528\u65b9\u6cd5\n     * @return\n     *\/\n    public String sayHello() {\n        String msg;\n        int h = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);\n        if (h &lt; 8) {\n            msg = sayMorning();\n        } else if (h &gt;= 12 &amp;&amp; h &lt;= 16) {\n            msg = sayAfternoon();\n        } else {\n            msg = sayEvening();\n        }\n\n        return &quot;[&quot; + greeterProperties.getUserName() + &quot;] &quot; + msg;\n    }\n}<\/code><\/pre>\n<p>\u914d\u7f6e\u5b9a\u4e49\u7684\u7c7b<code>GreeterProperties<\/code><\/p>\n<pre><code class=\"language-java\">package cn.appblog.autoconfigure;\n\nimport org.springframework.boot.context.properties.ConfigurationProperties;\n\n@ConfigurationProperties(prefix = &quot;appblog.greeter&quot;)\npublic class GreeterProperties {\n    private String userName;\n    private String morningMsg;\n    private String afternoonMsg;\n    private String eveningMsg;\n    private String nightMsg;\n\n    public String getUserName() {\n        return userName;\n    }\n\n    public void setUserName(String userName) {\n        this.userName = userName;\n    }\n\n    public String getMorningMsg() {\n        return morningMsg;\n    }\n\n    public void setMorningMsg(String morningMsg) {\n        this.morningMsg = morningMsg;\n    }\n\n    public String getEveningMsg() {\n        return eveningMsg;\n    }\n\n    public void setEveningMsg(String eveningMsg) {\n        this.eveningMsg = eveningMsg;\n    }\n\n    public String getNightMsg() {\n        return nightMsg;\n    }\n\n    public void setNightMsg(String nightMsg) {\n        this.nightMsg = nightMsg;\n    }\n\n    public String getAfternoonMsg() {\n        return afternoonMsg;\n    }\n\n    public void setAfternoonMsg(String afternoonMsg) {\n        this.afternoonMsg = afternoonMsg;\n    }\n}<\/code><\/pre>\n<p>\u53e6\u5916\u5728<code>resource<\/code>\u91cc\u9700\u8981\u5b9a\u4e49<code>META-INFO<\/code>\u4fe1\u606f\u3002\u5efa\u7acb<code>META-INFO<\/code>\u6587\u4ef6\u5939\uff0c\u5c06\u4ee5\u4e0b\u4fe1\u606f\u5199\u5165<code>spring.factories<\/code>\u6587\u4ef6\uff0cspring Boot\u5c31\u662f\u901a\u8fc7\u8fd9\u4e2a\u6587\u4ef6\u52a0\u8f7dStarter\u7684\u3002<\/p>\n<pre><code class=\"language-java\">org.springframework.boot.autoconfigure.EnableAutoConfiguration=\\\n  cn.appblog.autoconfigure.GreeterAutoConfiguration<\/code><\/pre>\n<p>\u4ee5\u4e0a\u9879\u76ee<code>pom.xml<\/code>\u6587\u4ef6\u4f9d\u8d56\u914d\u7f6e\uff0c\u53ea\u9700\u8981<code>spring-boot-autoconfigure<\/code>\u5373\u53ef\uff1a<\/p>\n<pre><code class=\"language-xml\">&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;version&gt;2.1.2.RELEASE&lt;\/version&gt;\n&lt;\/dependency&gt;<\/code><\/pre>\n<p>\u5b8c\u6210\u540e<code>mvn install<\/code>\uff0c\u90e8\u7f72\u5230\u672c\u5730\u4ed3\u5e93\uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528\u4e86\u3002\u5982\u679c\u662f\u751f\u4ea7\u6216\u8005\u4f01\u4e1a\u5f00\u53d1\u73af\u5883\uff0c\u4f7f\u7528<code>mvn deploy<\/code>\u90e8\u7f72\u5230\u8fdc\u7a0b\u4ed3\u5e93\u3002<\/p>\n<h2>\u5177\u4f53\u4f7f\u7528\u65b9\u5f0f<\/h2>\n<p>\u5728\u4e0a\u9762\u7684\u6b65\u9aa4\u5b8c\u6210\u6dfb\u52a0\u5230\u672c\u5730\u4ed3\u5e93\u540e\uff0c\u5c31\u53ef\u4ee5\u5728\u53e6\u5916\u7684\u9879\u76ee\u4e2d\u6dfb\u52a0starter\u4f9d\u8d56\uff0c\u6ce8\u610f\u9ed8\u8ba4\u7684\u7248\u672c\u53f7\u3002<code>pom.xml<\/code>\u914d\u7f6e\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-xml\">&lt;dependency&gt;\n    &lt;groupId&gt;cn.appblog&lt;\/groupId&gt;\n    &lt;artifactId&gt;greeter-spring-boot-starter&lt;\/artifactId&gt;\n    &lt;version&gt;1.0-SNAPSHOT&lt;\/version&gt;\n&lt;\/dependency&gt;<\/code><\/pre>\n<p>\u914d\u7f6e\u4fe1\u606f\u5199\u5165<code>application.properties<\/code>\u91cc\uff0c\u5c31\u53ef\u4ee5\u8c03\u7528\u5177\u4f53\u7684<code>Greeter<\/code>\u903b\u8f91\u65b9\u6cd5\u3002<\/p>\n<pre><code>appblog.greeter.userName=netscape\nappblog.greeter.morningMsg=\u4e2d\u5348\u597d\nappblog.greeter.afternoonMsg=\u4e0b\u5348\u597d\nappblog.greeter.eveningMsg=\u665a\u4e0a\u597d<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u529f\u80fd\u5b9e\u73b0 Spring Boot Starter\u53ef\u4ee5\u5c06\u9700\u8981\u7684\u529f\u80fd\u6574\u5408\u8d77\u6765\uff0c\u65b9\u4fbf\u4f7f\u7528\u3002\u8fd9\u4e2a\u4f8b\u5b50\u662f\u4e00\u4e2a\u975e\u5e38\u7b80\u5355\u7684S [&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-1758","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\/1758","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=1758"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1758\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1758"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1758"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1758"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}