爲了讓Spring Boot更好的生成配置元數據文件,咱們須要添加以下依賴(否則沒有提示就苦逼了),該依賴只會在編譯時調用,因此不用擔憂會對生產形成影響…java
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
Spring Boot提供的SpringApplication類會搜索並加載application.properties文件來獲取配置屬性值。
web
winner.name = winner_0715
winner.address = beijing
不須要其餘配置,咱們只須要經過 @Value("${屬性名}") 註解來加載對應的配置屬性,spring
@Value("${winner.name}")
private String name;
@Value("${winner.address}")
private String address;
定義一個名爲demo.properties的資源文件,自定義配置文件的命名不強制 application 開頭數據庫
winner.name=winner_0715 winner.address=beijing winner.email=winner_0715@163.com
其次定義PropertiesDemo.java文件,用來映射咱們在demo.properties中的內容。app
package com.winner.service; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component; /** * @author winner_0715 * @description: * @date 2018/12/3 */ @Component @PropertySource("classpath:demo.properties") @ConfigurationProperties(prefix = "winner") public class PropertiesDemo { private String name; private String address; private String email; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } @Override public String toString() { final StringBuilder sb = new StringBuilder("PropertiesDemo2{"); sb.append("name='").append(name).append('\''); sb.append(", address='").append(address).append('\''); sb.append(", email='").append(email).append('\''); sb.append('}'); return sb.toString(); } }
接下來在 PropertiesController用來注入 PropertiesDemo測試咱們編寫的代碼框架
package com.winner.web; import com.winner.service.PropertiesDemo1;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; /** * @author winner_0715 * @description: * @date 2018/12/3 */ @RestController public class PropertiesController { @Autowired private PropertiesDemo propertiesDemo; @RequestMapping(value = "/properties/get", method = RequestMethod.GET) public PropertiesDemo getPropertiesDemo() { return propertiesDemo; } }
在真實的應用中,經常會有多個環境(如:開發,測試,生產等),不一樣的環境相關的配置不一樣,如數據庫鏈接,第三方接口url,這個時候就須要用到spring.profile.active的強大功能了,它的格式爲 application-{profile}.properties,這裏的 application 爲前綴不能改,{profile}是咱們本身定義的。對於多環境的配置,各類項目構建工具或是框架的基本思路是一致的,經過配置多份不一樣環境的配置文件,再經過打包命令指定須要打包的內容以後進行區分打包,Spring Boot也不例外,或者說更加簡單。maven
在pom.xml中增長不一樣環境打包的配置:ide
<!-- 不一樣環境查找不一樣配置文件 -->
<profiles>
<profile>
<id>dev</id>
<properties>
<profiles.active>dev</profiles.active>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>beta</id>
<properties>
<profiles.active>beta</profiles.active>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<profiles.active>prod</profiles.active>
<maven.test.skip>true</maven.test.skip>
<scope.jar>provided</scope.jar>
</properties>
</profile>
</profiles>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<excludes>
<exclude>application-dev.properties</exclude>
<exclude>application-beta.properties</exclude>
<exclude>application-prod.properties</exclude>
</excludes>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>application-${profiles.active}.properties</include>
<include>application.properties</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
使用佔位符,在打包時替換,首先在配置文件中增長:spring-boot
spring.profiles.active=@profiles.active@
執行打包命令:工具
mvn package -Ptest
或者不使用profile的形式,經過 active 加載測試環境的配置。
java -jar ***.jar --spring.profiles.active=test
相對於屬性文件,YAML 文件是一個更好的配置文件格式。Spring Boot 提供的 SpringApplication 類也提供了對 YAML 配置文件的支持。
建立application.yml 文件
等價於
winner.name=zhangsan
winner.address=beijing