springboot採納了建立生產就緒Spring應用程序的觀點。 Spring Boot優先於配置的慣例,旨在讓您儘快啓動和運行。在一般情況下,我們不需要做太多的配置就能夠讓spring boot正常運行。在一些特殊的情況下,我們需要做修改一些配置,或者需要有自己的配置屬性。
一、自定義屬性
當我們創建一個springboot項目的時候,系統默認會爲我們在src/main/java/resources目錄下創建一個application.properties。個人習慣,我會將application.properties改爲application.yml文件,兩種文件格式都支持。
在application.yml自定義一組屬性:
my:
name: forezp
age: 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: forezp
age: 12
number: ${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,並加上它的prrfix。另外@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 {
@Autowired
ConfigBean 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;
//省略getter,setter
}
在最新版本的springboot,需要加這三個註解。@Configuration
@PropertySource(value = “classpath:test.properties”)
@ConfigurationProperties(prefix = “com.forezp”);在1.4版本需要
PropertySource加上location。
@RestController
@EnableConfigurationProperties({ConfigBean.class,User.class})
public class LucyController {
@Autowired
ConfigBean configBean;
@RequestMapping(value = "/lucy")
public String miya(){
return configBean.getGreeting()+" >>>>"+configBean.getName()+" >>>>"+ configBean.getUuid()+" >>>>"+configBean.getMax();
}
@Autowired
User user;
@RequestMapping(value = "/user")
public String user(){
return user.getName()+user.getAge();
}
}
啓動工程,打開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。