springboot 02-PropertiesFile 自定义配置属性,多环境配置
application.properties:
# 自定义配置 test.hello.world = HelloWorld test.person.name = 哈哈 test.person.sex = 男 # 多环境配置文件激活属性 spring.profiles.active=dev
application-dev.properties:
# 服务端口
server.port=1111
application-test.properties:
# 服务端口
server.port=2222
application-prod.properties:
# 服务端口
server.port=3333
TestProperties:
import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; /** * properties类描述: * * @author yangzhenlong * @since 2017/2/9 */ @Component public class TestProperties { @Value("${test.hello.world}") private String helloWorld; @Value("${test.person.name}") private String personName; @Value("${test.person.sex}") private String personSex; public String getHelloWorld() { return helloWorld; } public void setHelloWorld(String helloWorld) { this.helloWorld = helloWorld; } public String getPersonName() { return personName; } public void setPersonName(String personName) { this.personName = personName; } public String getPersonSex() { return personSex; } public void setPersonSex(String personSex) { this.personSex = personSex; } }
PropertiesController:
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * PropertiesController类描述: * * @author yangzhenlong * @since 2017/2/9 */ @RestController public class PropertiesController { @Autowired TestProperties testProperties; @RequestMapping("/properties") public String[] getProperties(){ String[] result = {"hello:" + testProperties.getHelloWorld(), "name:" + testProperties.getPersonName(), "sex:" + testProperties.getPersonSex()}; return result; } }
启动app类后,浏览器访问:http://localhost:1111/properties
逃避不一定躲得过,面对不一定最难过