您好,登錄后才能下訂單哦!
最近微框架spring-boot很火,筆者也跟風學習了一下,廢話不多說,現給出一個讀取配置文件的例子。
首先,需要在pom文件中依賴以下jar包
Java代碼
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
其次,我們需要一個spring-boot啟動類 下載
Java代碼
@SpringBootApplication
@EnableConfigurationProperties({PropsConfig.class,YmlConfig.class})
public class ReadApplication {
public static void main(String[] args) {
SpringApplication.run(ReadApplication.class, args);
}
}
沒錯,@EnableConfigurationProperties注解里指出的PropsConfig.class,YmlConfig.class分別就是讀取props和yml配置文件的類。接下來,我們分別進行讀取properties和yml配置文件的具體實現。
1.讀取properties配置文件
在類路徑下放置一個application.properties文件,大致內容如下: 下載
Java代碼
master.ds.driverClassName=com.mysql.jdbc.Driver
master.ds.url=jdbc:mysql://localhost:3306/test
master.ds.username=root
master.ds.password=root
master.ds.filters=stat
master.ds.maxActive=20
master.ds.initialSize=1
master.ds.maxWait=60000
master.ds.minIdle=10
master.ds.timeBetweenEvictionRunsMillis=60000
master.ds.minEvictableIdleTimeMillis=300000
master.ds.validationQuery=SELECT 'x'
master.ds.testWhileIdle=true
master.ds.testOnBorrow=false
master.ds.testOnReturn=false
master.ds.poolPreparedStatements=true
master.ds.maxOpenPreparedStatements=100
master.ds.removeAbandoned=true
master.ds.removeAbandonedTimeout=1800
master.ds.logAbandoned=true
讀取props配置的類,很簡單,基本就是一個pojo/vo類,在類上加載@ConfigurationProperties注解即可。
Java代碼 下載
@ConfigurationProperties(prefix = "master.ds",locations = "classpath:application.properties")
public class PropsConfig {
private String driverClassName;
private String url;
private String username;
private String password;
private String filters;
private String maxActive;
private String initialSize;
private String maxWait;
public String getDriverClassName() {
return driverClassName;
}
public void setDriverClassName(String driverClassName) {
this.driverClassName = driverClassName;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
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;
}
public String getFilters() {
return filters;
}
public void setFilters(String filters) {
this.filters = filters;
}
public String getMaxActive() {
return maxActive;
}
public void setMaxActive(String maxActive) {
this.maxActive = maxActive;
}
public String getInitialSize() {
return initialSize;
}
public void setInitialSize(String initialSize) {
this.initialSize = initialSize;
}
public String getMaxWait() {
return maxWait;
}
public void setMaxWait(String maxWait) {
this.maxWait = maxWait;
}
}
單元測試類
Java代碼 下載
@RunWith(SpringJUnit4Cla***unner.class)
@SpringBootTest(classes = ReadApplication.class)
public class ReadApplicationPropsTests {
@Autowired
private PropsConfig propsConfig;
@Test
public void testDisplayPropsValue() {
String driverClassName = propsConfig.getDriverClassName();
String url = propsConfig.getUrl();
String username = propsConfig.getUsername();
String password = propsConfig.getPassword();
String filters = propsConfig.getFilters();
String maxActive = propsConfig.getMaxActive();
String initialSize = propsConfig.getInitialSize();
String maxWait = propsConfig.getMaxWait();
System.out.println("driverClassName -> " + driverClassName);
System.out.println("url -> " + url);
System.out.println("username -> " + username);
System.out.println("password -> " + password);
System.out.println("initialSize -> " + initialSize);
System.out.println("maxWait -> " + maxWait);
}
}
可以看到在控制臺輸出的測試內容:
Java代碼
driverClassName -> com.mysql.jdbc.Driver
url -> jdbc:mysql://localhost:3306/test
username -> root
password -> root
initialSize -> 1
maxWait -> 60000
2.讀取yml配置文件
在類路徑下放置一個application.yml文件下載,大致內容如下:
Java代碼
myProps: #自定義的屬性和值
simpleProp: simplePropValue
arrayProps: 1,2,3,4,5
listProp1:
- name: abc
value: abcValue
- name: efg
value: efgValue
listProp2:
- config2Value1
- config2Vavlue2
mapProps:
key1: value1
key2: value2
讀取yml配置文件的類。
Java代碼
@ConfigurationProperties(prefix="myProps") //application.yml中的myProps下的屬性
public class YmlConfig {
private String simpleProp;
private String[] arrayProps;
private List<Map<String, String>> listProp1 = new ArrayList<>(); //接收prop1里面的屬性值
private List<String> listProp2 = new ArrayList<>(); //接收prop2里面的屬性值
private Map<String, String> mapProps = new HashMap<>(); //接收prop1里面的屬性值
public String getSimpleProp() {
return simpleProp;
}
public void setSimpleProp(String simpleProp) {
this.simpleProp = simpleProp;
}
public List<Map<String, String>> getListProp1() {
return listProp1;
}
public List<String> getListProp2() {
return listProp2;
}
public String[] getArrayProps() {
return arrayProps;
}
public void setArrayProps(String[] arrayProps) {
this.arrayProps = arrayProps;
}
public Map<String, String> getMapProps() {
return mapProps;
}
public void setMapProps(Map<String, String> mapProps) {
this.mapProps = mapProps;
}
}
單元測試類
Java代碼 下載
@RunWith(SpringJUnit4Cla***unner.class)
@SpringBootTest(classes = ReadApplication.class)
public class ReadApplicationYmlTests {
@Autowired
private YmlConfig ymlConfig;
@Test
public void testDisplayYmlValue() throws JsonProcessingException {
System.out.println("simpleProp: " + ymlConfig.getSimpleProp());
ObjectMapper objectMapper = new ObjectMapper();
System.out.println("arrayProps: " + objectMapper.writeValueAsString(ymlConfig.getArrayProps()));
System.out.println("listProp1: " + objectMapper.writeValueAsString(ymlConfig.getListProp1()));
System.out.println("listProp2: " + objectMapper.writeValueAsString(ymlConfig.getListProp2()));
System.out.println("mapProps: " + objectMapper.writeValueAsString(ymlConfig.getMapProps()));
}
}
可以看到在控制臺輸出的測試內容:
Java代碼
simpleProp: simplePropValue
arrayProps: ["1","2","3","4","5"]
listProp1: [{"name":"abc","value":"abcValue"},{"name":"efg","value":"efgValue"}]
listProp2: ["config2Value1","config2Vavlue2"]
mapProps: {"key1":"value1","key2":"value2"}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。