javascript
SpringBoot非官方教程 | 第二篇:Spring Boot配置文件详解
springboot采納了建立生產就緒Spring應用程序的觀點。 Spring Boot優先于配置的慣例,旨在讓您盡快啟動和運行。在一般情況下,我們不需要做太多的配置就能夠讓spring boot正常運行。在一些特殊的情況下,我們需要修改一些配置,或者需要有自己的配置屬性。
一、自定義屬性
當我們創建一個springboot項目的時候,系統默認會為我們在src/main/java/resources目錄下創建一個application.properties。個人習慣,我會將application.properties改為application.yml文件,兩種文件格式都支持。
在application.yml自定義一組屬性:
my:name: forezpage: 12?
如果你需要讀取配置文件的值只需要加@Value(“${屬性名}”):
@RestController public class MiyaController {@Value("${my.name}")private String name;@Value("${my.age}")private int age;@RequestMapping(value = "/miya")public String miya(){return name+":"+age;}}啟動工程,訪問:localhost:8080/miya,瀏覽器顯示:
forezp:12
二、將配置文件的屬性賦給實體類
當我們有很多配置屬性的時候,這時我們會把這些屬性作為字段來創建一個javabean,并將屬性值賦予給他們,比如:
my:name: forezpage: 12number: ${random.int}uuid : ${random.uuid}max: ${random.int(10)}value: ${random.value}greeting: hi,i'm ${my.name}其中配置文件中用到了${random} ,它可以用來生成各種不同類型的隨機值。
怎么講這些屬性賦于給一個javabean 呢,首先創建一個javabean :
@ConfigurationProperties(prefix = "my") @Component public class ConfigBean {private String name;private int age;private int number;private String uuid;private int max;private String value;private String greeting;省略了getter setter....需要加個注解@ConfigurationProperties,并加上它的prefix。另外@Component可加可不加。另外spring-boot-configuration-processor依賴可加可不加,具體原因不詳。
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId><optional>true</optional> </dependency>另外需要在應用類或者application類,加EnableConfigurationProperties注解。
@RestController @EnableConfigurationProperties({ConfigBean.class}) public class LucyController {@AutowiredConfigBean configBean;@RequestMapping(value = "/lucy")public String miya(){return configBean.getGreeting()+" >>>>"+configBean.getName()+" >>>>"+ configBean.getUuid()+" >>>>"+configBean.getMax();}啟動工程,訪問localhost:8080/lucy,我們會發現配置文件信息讀到了。
三、自定義配置文件
上面介紹的是我們都把配置文件寫到application.yml中。有時我們不愿意把配置都寫到application配置文件中,這時需要我們自定義配置文件,比如test.properties:
com.forezp.name=forezp com.forezp.age=12怎么將這個配置文件信息賦予給一個javabean呢?
@Configuration @PropertySource(value = "classpath:test.properties") @ConfigurationProperties(prefix = "com.forezp") public class User {private String name;private int age;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;} }在最新版本的springboot,需要加這三個注解。
@Configuration? @PropertySource(value = “classpath:test.properties”)? @ConfigurationProperties(prefix = “com.forezp”)在1.4版本需要?
PropertySource加上location。
啟動工程,打開localhost:8080/user;瀏覽器會顯示:
forezp12
四、多個環境配置文件
在現實的開發環境中,我們需要不同的配置環境;格式為application-{profile}.properties,其中{profile}對應你的環境標識,比如:
- application-test.properties:測試環境
- application-dev.properties:開發環境
- application-prod.properties:生產環境
怎么使用?只需要我們在application.yml中加:
spring:profiles:active: dev?
其中application-dev.yml:
server:port: 8082啟動工程,發現程序的端口不再是8080,而是8082。
源碼下載:https://github.com/forezp/SpringBootLearning
五、參考文獻
spring-boot-reference-guide-zh
spring Boot干貨系列:(二)配置文件解析
Spring Boot屬性配置文件詳解
總結
以上是生活随笔為你收集整理的SpringBoot非官方教程 | 第二篇:Spring Boot配置文件详解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SpringBoot非官方教程 | 第一
- 下一篇: Spring Boot 启动加载数据 C