欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

spring-boot读取props和yml配置文件的方法

程序员文章站 2024-02-21 18:25:28
最近微框架spring-boot很火,笔者也跟风学习了一下,废话不多说,现给出一个读取配置文件的例子。 首先,需要在pom文件中依赖以下jar包

最近微框架spring-boot很火,笔者也跟风学习了一下,废话不多说,现给出一个读取配置文件的例子。

首先,需要在pom文件中依赖以下jar包

<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启动类 

@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文件,大致内容如下:

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注解即可。

@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; 
 } 
} 

单元测试类

@runwith(springjunit4classrunner.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); 
   
 } 
} 

可以看到在控制台输出的测试内容:

driverclassname -> com.mysql.jdbc.driver 
url -> jdbc:mysql://localhost:3306/test 
username -> root 
password -> root 
initialsize -> 1 
maxwait -> 60000 

2.读取yml配置文件

在类路径下放置一个application.yml文件,大致内容如下:

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配置文件的类。

@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; 
 } 
} 

单元测试类

@runwith(springjunit4classrunner.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()));  
 } 
} 

可以看到在控制台输出的测试内容:

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"} 

是不是很神奇,不需要spring的applicationcontext.xml文件也可以顺利运行之。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。