spring-boot读取props和yml配置文件的方法
最近微框架spring-boot很火,笔者也跟风学习了一下,废话不多说,现给出一个读取配置文件的例子。
首先,需要在pom文件中依赖以下jar包
org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test
其次,我们需要一个spring-boot启动类
@SpringBootApplication @EnableConfigurationProperties({PropsConfig.class,YmlConfig.class}) publicclassReadApplication{ publicstaticvoidmain(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") publicclassPropsConfig{ privateStringdriverClassName; privateStringurl; privateStringusername; privateStringpassword; privateStringfilters; privateStringmaxActive; privateStringinitialSize; privateStringmaxWait; publicStringgetDriverClassName(){ returndriverClassName; } publicvoidsetDriverClassName(StringdriverClassName){ this.driverClassName=driverClassName; } publicStringgetUrl(){ returnurl; } publicvoidsetUrl(Stringurl){ this.url=url; } publicStringgetUsername(){ returnusername; } publicvoidsetUsername(Stringusername){ this.username=username; } publicStringgetPassword(){ returnpassword; } publicvoidsetPassword(Stringpassword){ this.password=password; } publicStringgetFilters(){ returnfilters; } publicvoidsetFilters(Stringfilters){ this.filters=filters; } publicStringgetMaxActive(){ returnmaxActive; } publicvoidsetMaxActive(StringmaxActive){ this.maxActive=maxActive; } publicStringgetInitialSize(){ returninitialSize; } publicvoidsetInitialSize(StringinitialSize){ this.initialSize=initialSize; } publicStringgetMaxWait(){ returnmaxWait; } publicvoidsetMaxWait(StringmaxWait){ this.maxWait=maxWait; } }
单元测试类
@RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest(classes=ReadApplication.class) publicclassReadApplicationPropsTests{ @Autowired privatePropsConfigpropsConfig; @Test publicvoidtestDisplayPropsValue(){ StringdriverClassName=propsConfig.getDriverClassName(); Stringurl=propsConfig.getUrl(); Stringusername=propsConfig.getUsername(); Stringpassword=propsConfig.getPassword(); Stringfilters=propsConfig.getFilters(); StringmaxActive=propsConfig.getMaxActive(); StringinitialSize=propsConfig.getInitialSize(); StringmaxWait=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下的属性 publicclassYmlConfig{ privateStringsimpleProp; privateString[]arrayProps; privateList
单元测试类
@RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest(classes=ReadApplication.class) publicclassReadApplicationYmlTests{ @Autowired privateYmlConfigymlConfig; @Test publicvoidtestDisplayYmlValue()throwsJsonProcessingException{ System.out.println("simpleProp:"+ymlConfig.getSimpleProp()); ObjectMapperobjectMapper=newObjectMapper(); 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文件也可以顺利运行之。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。