为了让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