十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
最近微框架spring-boot很火,笔者也跟风学习了一下,废话不多说,现给出一个读取配置文件的例子。
首先,需要在pom文件中依赖以下jar包
Java代码
其次,我们需要一个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
private List
private Map
public String getSimpleProp() {
return simpleProp;
}
public void setSimpleProp(String simpleProp) {
this.simpleProp = simpleProp;
}
public List
return listProp1;
}
public List
return listProp2;
}
public String[] getArrayProps() {
return arrayProps;
}
public void setArrayProps(String[] arrayProps) {
this.arrayProps = arrayProps;
}
public Map
return mapProps;
}
public void setMapProps(Map
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"}
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。