轉載請注明出處:http://blog.csdn.net/QQ_26525215本文源自【大學之旅_諳憶的博客】
SPRing Boot允許使用properties文件、yaml文件或者命令行參數作為外部配置。
命令行參數配置
Spring Boot可以是基于jar包運行的,打成jar包的程序可以直接通過下面命令運行:
java -jar *.jar如果你的Tomcat的端口和你其他的端口起沖突了, 還可以通過以下命令修改Tomcat端口號:
java -jar *.jar --server.port=10090*為你的jar包名。
常規屬性配置
在常規Spring環境下,注入properties文件里面的值的方式可以通過@PropertySource指明properties文件的位置,然后通過@Value注入值。 詳情見此篇博客:
【【Spring】Spring常用配置-Spring EL和資源調用 】在Spring Boot中,我們只需要在application.properties定義屬性,直接使用@Value注入即可。
實戰
利用IDEA–>Spring-Initializr 創建好SpringBoot骨架! 步驟如下(以后的博客中可能就不再累贅寫創建SpringBoot骨架啦):
創建好骨架后,進行如下的修改。
1、application.properties增加屬性
springBoot2_2.author=chenhaoxiangspringBoot2_2.name=spring Boot#修改Tomcat啟動端口server.port=10090#修改訪問路徑-也就是把默認的"/"修改為了"/helloboot"server.context-path=/helloboot2、修改入口類
package cn.hncu;import org.springframework.beans.factory.annotation.Value;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@SpringBootApplication@RestControllerpublic class SpringBoot22Application { @Value("${springBoot2_2.author}") private String author; @Value("${springBoot2_2.name}") private String name; @RequestMapping("/") String index(){ return "name:"+name+" and author:"+author; } public static void main(String[] args) { SpringApplication.run(SpringBoot22Application.class, args); }}3、運行程序,訪問http://localhost:10090/helloboot/
效果如下:
類型安全的配置
上面我們使用@Value注入每個配置,但是在實際項目中會顯得格外麻煩,因為我們的配置通常會是許多個,若使用上例的方式則要使用@Value注入很多次。
Spring Boot 還提供了基于類型安全的配置方式,通過@ConfigurationProperties將properties屬性和一個Bean及其屬性關聯,從而實現類型安全配置。
實戰
我們在上面的例子上修改。
1、添加配置,即在application.properties上添加:
author.name=chxauthor.age=20當然,如果你不想在這個properties中配置屬性,也可以自己新建一個properties文件,這就需要我們在@ConfigurationProperties的屬性locations里指定properties的位置,且需要在入口類上配置。
2、類型安全的Bean
package cn.hncu.model;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.stereotype.Component;/** * Created with IntelliJ IDEA. * User: 陳浩翔. * Date: 2017/2/18. * Time: 下午 4:52. * Explain:類型安全的Bean */@Component@ConfigurationProperties(prefix = "author")//通過locations指定properties文件的位置,這里是在application.properties文件中,可以不指定public class AuthorSettings { private String name; private Long age; public String getName() { return name; } public void setName(String name) { this.name = name; } public Long getAge() { return age; } public void setAge(Long age) { this.age = age; }}通過@ConfigurationProperties加載properties文件內的配置,通過prefix屬性指定properties的配置的前綴,通過locations指定properties文件的位置。
如果不是在application.properties文件中,則需要配置locations。例如:
@ConfigurationProperties(prefix = "author",locations = {"classpath:author.properties"})本例不需要配置locations。
3、檢驗代碼
package cn.hncu;import cn.hncu.model.AuthorSettings;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;/** * Created with IntelliJ IDEA. * User: 陳浩翔. * Date: 2017/2/18. * Time: 下午 4:59. * Explain:檢驗代碼類 - 類型安全的配置 */@RestControllerpublic class CheckoutAuthor { @Autowired //直接注入該配置 private AuthorSettings authorSettings; @RequestMapping("/author") public String index2(){ return "author name is "+ authorSettings.getName() +" and author age is "+authorSettings.getAge(); }}運行。
訪問:http://localhost:10090/helloboot/author
效果如下:
本篇博客涉及到的源碼鏈接:
【->點擊訪問源碼-?CHX】本文章由[諳憶]編寫, 所有權利保留。 歡迎轉載,分享是進步的源泉。
轉載請注明出處:http://blog.csdn.net/qq_26525215本文源自【大學之旅_諳憶的博客】
新聞熱點
疑難解答