{"id":2006,"date":"2017-06-16T11:34:35","date_gmt":"2017-06-16T03:34:35","guid":{"rendered":"https:\/\/ai.zhousir.top\/?p=2006"},"modified":"2017-06-17T10:35:44","modified_gmt":"2017-06-17T02:35:44","slug":"springboot%e8%8e%b7%e5%8f%96%e9%85%8d%e7%bd%ae%e6%96%87%e4%bb%b6%e6%95%b0%e6%8d%ae","status":"publish","type":"post","link":"https:\/\/ai.zhousir.top\/?p=2006","title":{"rendered":"SpringBoot\u914d\u7f6e\u6587\u4ef6\u4e0e\u751f\u4ea7\u73af\u5883\u548c\u5f00\u53d1\u73af\u5883\u914d\u7f6e"},"content":{"rendered":"<h2>\u4e00\u3001SpringBoot\u751f\u4ea7\u73af\u5883\u548c\u5f00\u53d1\u73af\u5883\u914d\u7f6e<\/h2>\n<h3>1.\u5b9a\u4e49\u751f\u4ea7\u73af\u5883\u914d\u7f6e\u6587\u4ef6\u4e3a application-product.yml<\/h3>\n<p>[java]server:<br \/>\n  port: 8081<br \/>\nspring:<br \/>\n  datasource:<br \/>\n    driver-class-name: com.mysql.jdbc.Driver<br \/>\n    url: jdbc:mysql:\/\/127.0.0.1:3306\/productDb<br \/>\n    username: root<br \/>\n    password: 123456  [\/java]<\/p>\n<h3>2.\u5b9a\u4e49\u5f00\u53d1\u73af\u5883\u914d\u7f6e\u6587\u4ef6\u4e3a application-dev.yml<\/h3>\n<p>[java]server:<br \/>\n  port: 8080<br \/>\nspring:<br \/>\n  datasource:<br \/>\n    driver-class-name: com.mysql.jdbc.Driver<br \/>\n    url: jdbc:mysql:\/\/127.0.0.1:3306\/devlepmentDb<br \/>\n    username: root<br \/>\n    password: kf123  [\/java]<\/p>\n<h3>3.\u914d\u7f6e\u6587\u4ef6application.yml\u4e2d\u8bbe\u7f6e\u5f53\u524d\u73af\u5883<\/h3>\n<p>[java]spring:<br \/>\n  profiles:<br \/>\n    active: dev[\/java]<\/p>\n<h2>\u4e8c\u3001SpringBoot\u83b7\u53d6\u914d\u7f6e\u6587\u4ef6\u6570\u636e\u6709\u4e24\u79cd\u65b9\u5f0f<\/h2>\n<p>\u9996\u5148\u5728\u914d\u7f6e\u6587\u4ef6application.yml\u4e2d\u51c6\u5907\u597d\u6570\u636e<\/p>\n<p>[java]test:<br \/>\n  username: viktor<br \/>\n  password: 123<br \/>\n[\/java]<\/p>\n<h2>\u83b7\u53d6\u65b9\u5f0f\u4e00:\u901a\u8fc7@Value\u6ce8\u89e3\u83b7\u53d6<\/h2>\n<p>\u7f3a\u70b9\uff1a\u9700\u8981\u5728\u6bcf\u4e2a\u5143\u7d20\u4e0a\u6dfb\u52a0\u6ce8\u89e3<\/p>\n<p>[java]@Controller<br \/>\n@RequestMapping(&quot;\/config&quot;)<br \/>\npublic class ConfigeController {<br \/>\n\t@Value(value=&quot;${test.username}&quot;)<br \/>\n\tprivate String userName;<\/p>\n<p>\t@Value(value=&quot;${test.password}&quot;)<br \/>\n\tprivate String passWord;<\/p>\n<p>\t@RequestMapping(&quot;&quot;)<br \/>\n\t@ResponseBody<br \/>\n\tpublic String config() {<br \/>\n\t\treturn &quot;config == &quot;+userName+&quot; &quot;+passWord;<br \/>\n\t}<\/p>\n<p>}[\/java]<\/p>\n<h2>\u83b7\u53d6\u65b9\u5f0f\u4e8c: \u901a\u8fc7@ConfigurationProperties\u6ce8\u89e3\u83b7\u53d6<\/h2>\n<h5>1.\u5728\u7c7b\u4e0a\u6dfb\u52a0\u6ce8\u89e3@ConfigurationProperties(prefix=&#8221;test&#8221;); prefix=&#8221;test&#8221;\u4e3a\u83b7\u53d6\u5143\u7d20\u524d\u7f00,\u5982:test.username<\/h5>\n<h5>2.\u7c7b\u4e2d\u5c5e\u6027\u540d\u4e0e\u914d\u7f6e\u4e2d\u5143\u7d20\u540d\u76f8\u540c<\/h5>\n<h5>3.\u6dfb\u52a0geter,seter\u65b9\u6cd5<\/h5>\n<h5>4.\u4e5f\u662f\u91cd\u70b9,\u6dfb\u52a0@ConfigurationProperties\u6ce8\u89e3\u4f9d\u8d56<\/h5>\n<p>[java]&lt;!&#8211; \u652f\u6301 @ConfigurationProperties \u6ce8\u89e3 yml\u914d\u7f6e\u6587\u4ef6&#8211;&gt;<br \/>\n&lt;dependency&gt;<br \/>\n\t&lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;<br \/>\n\t&lt;artifactId&gt;spring-boot-configuration-processor&lt;\/artifactId&gt;<br \/>\n\t&lt;optional&gt;true&lt;\/optional&gt;<br \/>\n&lt;\/dependency&gt;[\/java]<\/p>\n<h3>5.ConfigeControllerNew.java<\/h3>\n<p>[java]@Controller<br \/>\n@RequestMapping(&quot;\/confignew&quot;)<br \/>\n@ConfigurationProperties(prefix=&quot;test&quot;)<br \/>\npublic class ConfigeControllerNew {<br \/>\n\tprivate String username;<br \/>\n\tprivate String password;<\/p>\n<p>\t@RequestMapping(&quot;&quot;)<br \/>\n\t@ResponseBody<br \/>\n\tpublic String config() {<br \/>\n\t\treturn &quot;config == &quot;+username+&quot; &quot;+password;<br \/>\n\t}<\/p>\n<p>\tpublic String getUsername() {<br \/>\n\t\treturn username;<br \/>\n\t}<\/p>\n<p>\tpublic void setUsername(String username) {<br \/>\n\t\tthis.username = username;<br \/>\n\t}<\/p>\n<p>\tpublic String getPassword() {<br \/>\n\t\treturn password;<br \/>\n\t}<\/p>\n<p>\tpublic void setPassword(String password) {<br \/>\n\t\tthis.password = password;<br \/>\n\t}<\/p>\n<p>}[\/java]<\/p>\n<h2>\u5b9e\u9645\u5e94\u7528<\/h2>\n<h3>1. \u914d\u7f6e\u6587\u4ef6application.yml\u5305\u542b\u5982\u4e0b\u4fe1\u606f<\/h3>\n<p>[java]user<br \/>\n  username: \u5f20\u4e09<br \/>\n  age: 23<br \/>\n[\/java]<\/p>\n<h3>2. \u5b9a\u4e49\u83b7\u53d6\u914d\u7f6e\u6587\u4ef6\u5c5e\u6027\u7684\u5de5\u5177\u7c7b<\/h3>\n<p>[java]<br \/>\n@Component<br \/>\n@ConfigurationProperties(prefix = &quot;user&quot;)<br \/>\npublic class UserProperties {<\/p>\n<p>    private String username;<\/p>\n<p>    private Integer age;<\/p>\n<p>    public String getUsername() {<br \/>\n        return username;<br \/>\n    }<\/p>\n<p>    public void setUsername(String username) {<br \/>\n        this.username= username;<br \/>\n    }<\/p>\n<p>    public Integer getAge() {<br \/>\n        return age;<br \/>\n    }<\/p>\n<p>    public void setAge(Integer age) {<br \/>\n        this.age = age;<br \/>\n    }<br \/>\n}[\/java]<\/p>\n<h3>3. \u5728Controller\u7c7b\u6216\u5176\u5b83\u4e2d\u4f7f\u7528<\/h3>\n<p>[java]@RestController<br \/>\n@RequestMapping(&quot;\/hello&quot;)<br \/>\npublic class HelloController {<\/p>\n<p>    @Autowired<br \/>\n    private UserProperties userProperties;<\/p>\n<p>    @GetMapping(value = &quot;\/proper&quot;)<br \/>\n    public String say() {<br \/>\n        return userProperties.getUsername();<br \/>\n    }<br \/>\n}[\/java]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001SpringBoot\u751f\u4ea7\u73af\u5883\u548c\u5f00\u53d1\u73af\u5883\u914d\u7f6e 1.\u5b9a\u4e49\u751f\u4ea7\u73af\u5883\u914d\u7f6e\u6587\u4ef6\u4e3a application-produ [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[93],"tags":[],"_links":{"self":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/2006"}],"collection":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2006"}],"version-history":[{"count":10,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/2006\/revisions"}],"predecessor-version":[{"id":2021,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/2006\/revisions\/2021"}],"wp:attachment":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2006"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2006"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2006"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}