SpringBoot 配置檔案屬性獲取
阿新 • • 發佈:2019-01-01
1. 獲取application.yml或者application.properties中的單個屬性
1.1 在application.yml中自定義屬性
spring:
application:
name: spring-boot-study
server:
port: 10000
prop:
name: z3
age: 21
1.2 在Controller中獲取屬性
package com.zjw.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework. beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
private static final Logger logger = LoggerFactory.getLogger(HelloController.class);
@Value ("${spring.application.name}")
private String appName;
@Value("${prop.name}")
private String propName;
@Value("${prop.age}")
private int propAge;
@RequestMapping("/hello")
public String hello() {
logger.info("HelloController hello function invoked.");
return "Hello, My application name is " + appName + ".";
}
@RequestMapping("/getProp")
public String getProp() {
logger.info("HelloController getProp function invoked.");
return "prop.name: " + propName + ", prop.age: " + propAge;
}
}
2. 以POJO的方式批量讀取application.yml或者application.properties中的配置資訊
2.1 在application.yml中自定義屬性
spring:
application:
name: spring-boot-study
server:
port: 10000
prop:
name: z3
age: 21
2.2 建立POJO
package com.zjw.entity;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@ConfigurationProperties(prefix = "prop")
@Component
public class Prop {
private String name;
private String uuid;
private String value;
private String greeting;
public String getName() {
return name;
}
public String getGreeting() {
return greeting;
}
public void setGreeting(String greeting) {
this.greeting = greeting;
}
public void setName(String name) {
this.name = name;
}
public String getUuid() {
return uuid;
}
public void setUuid(String uuid) {
this.uuid = uuid;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
@Override
public String toString() {
return "[name: " + name + ", value: " + value
+ ", uuid: " + uuid + ", greeting: " + greeting + "]";
}
}
2.2 在Controller中使用POJO獲取配置資料
package com.zjw.controller;
import com.zjw.entity.Prop;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@EnableConfigurationProperties({Prop.class})
public class HelloController {
private static final Logger logger = LoggerFactory.getLogger(HelloController.class);
@Value("${spring.application.name}")
private String appName;
@Value("${prop.name}")
private String propName;
@Value("${prop.age}")
private int propAge;
@Autowired
private Prop prop;
@RequestMapping("/hello")
public String hello() {
logger.info("HelloController hello function invoked.");
return "Hello, My application name is " + appName + ".";
}
@RequestMapping("/getProp")
public String getProp() {
logger.info("HelloController getProp function invoked.");
return "prop.name: " + propName + ", prop.age: " + propAge;
}
@RequestMapping("/getPropByPojo")
public String getPropByPojo() {
logger.info("HelloController getPropByPojo function invoked.");
return prop.toString();
}
@RequestMapping("/getPropByPojoJson")
public Prop getPropByPojoJson() {
logger.info("HelloController getPropByPojo function invoked.");
return prop;
}
}
3. 自定義配置檔案
3.1 建立配置檔案admin.properties
admin.username=admin
admin.password=zhang111111
admin.id=${random.uuid}
3.2 建立POJO類
package com.zjw.entity;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@PropertySource(value = "classpath:admin.properties")
@ConfigurationProperties(prefix = "admin")
@Component
public class AdminProp {
private String id;
private String username;
private String password;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
3.3 在Controller中使用
package com.zjw.controller;
import com.zjw.entity.AdminProp;
import com.zjw.entity.Prop;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@EnableConfigurationProperties({Prop.class, AdminProp.class})
public class HelloController {
private static final Logger logger = LoggerFactory.getLogger(HelloController.class);
@Value("${spring.application.name}")
private String appName;
@Value("${prop.name}")
private String propName;
@Value("${prop.age}")
private int propAge;
@Autowired
private Prop prop;
@Autowired
private AdminProp adminProp;
@RequestMapping("/hello")
public String hello() {
logger.info("HelloController hello function invoked.");
return "Hello, My application name is " + appName + ".";
}
@RequestMapping("/getProp")
public String getProp() {
logger.info("HelloController getProp function invoked.");
return "prop.name: " + propName + ", prop.age: " + propAge;
}
@RequestMapping("/getPropByPojo")
public Prop getPropByPojo() {
logger.info("HelloController getPropByPojo function invoked.");
return prop;
}
@RequestMapping("/getAdminProp")
public AdminProp getAdminProp(){
logger.info("HelloController getAdminProp function invoked.");
return adminProp;
}
}
4. 支援多環境的配置檔案
在實際的開發過程中,存在多個環境,例如:開發、測試、生產等,SpringBoot支援在application.yml中指定環境的配置檔案,配置格式為application-{profile}.properties,其中profile對應環境標識,例如:
application-dev.yml
application-pro.yml
application-test.yml
- 可以在application.yml中配置spring.profiles.active:dev|pro|test 啟用不同的配置
- 可以指定JVM引數 -Dspring.profiles.active=dev|pro|test 啟用不同的配置