Spring读取配置文件属性实现方法
一前言
本篇内容包括spring运行时读取配置文件的多种方式和SpEl表达式入门基础;
二运行时读取配置文件
spring运行时读取配置文件值提供了2种方式
属性占位符(Propertyplaceholder)。
Spring表达式语言(SpEL)
2.1读取外部配置文件
使用@PropertySource注解可以读取导classpath下配置文件属性;参数如下
- value是个字符串数组;
- ignoreResourceNotFound;如果设置为true,配置文件未找到时不会报错;
- encoding;指定字符集
首先resource目录下创建配置文件zszxz.properties;内容如下
zszxz.name=zszxz
zszxz.point=share
其次读取配置文件配置类如下
@Configuration
@PropertySource(value={"classpath:zszxz.properties"},encoding="UTF-8")
@Component
publicclassEnvironmentProperty{
//注入环境
@Autowired
privateEnvironmentenvironment;
publicvoidoutputProperty(){
System.out.println(environment.getProperty("zszxz.name"));
}
}
最后通过测试类调用outputProperty()输出配置文件中属性的值
@RunWith(SpringJUnit4ClassRunner.class)//创建spring应用上下文
@ContextConfiguration(classes=EnvironmentProperty.class)//加载配置类
publicclassPropertyTest{
@Autowired
EnvironmentPropertyenvironmentProperty;
@Test
publicvoidtest(){
//zszxz
environmentProperty.outputProperty();
}
}
Tip也可以使用@PropertySources注解,其value是@PropertySource类型的数组;
其中EnvironmentProperty获取主要属性方法如下
- StringgetProperty(Stringkey);通过key取值
- StringgetProperty(Stringkey,StringdefaultValue);获取值,没有则使用默认值;
- TgetProperty(Stringkey,Classvar2);获取值,指定返回类型;
- TgetProperty(Stringkey,Classvar2,TdefaultValue);获取值,指定返回类型,指定默认值;
- StringgetRequiredProperty(Stringkey);key必须为非空否则抛出IllegalStateException异常
2.2使用占位符获取配置文件
使用注解@Value获取配置文件属性值;其中值使用占位符("${........}")方式;
配置类示例
@Configuration
@PropertySource(value={"classpath:zszxz.properties"},encoding="UTF-8")
@Component
publicclassEnvironmentProperty{
@Value("${zszxz.point}")
privateStringpoint;
publicvoidoutputPoint(){
System.out.println(point);
}
}
测试示例
@RunWith(SpringJUnit4ClassRunner.class)//创建spring应用上下文
@ContextConfiguration(classes=EnvironmentProperty.class)//加载配置类
publicclassPropertyTest{
@Autowired
EnvironmentPropertyenvironmentProperty;
@Test
publicvoidtestPoint(){
//share
environmentProperty.outputPoint();
}
}
2.3SpEl表达式
Spring表达式语言(SpringExpressionLanguage,SpEL)是一种灵活的表达式语言,能够以简洁的方式将值装配到bean属性或者构造器参数中,此过程中能够计算表达式获取计算值;使用@Valjue注解时,SpEL表达式要放到“#{......}”之中;
获取bean示例
@Value("#{environmentProperty}")
privateEnvironmentPropertygetBean;
@Test
publicvoidtestBean(){
//com.zszxz.property.EnvironmentProperty$$EnhancerBySpringCGLIB$$8e54e11f@1d9b7cce
System.out.println(getBean);
}
获取方法示例
@Value("#{environmentProperty.getStr()}")
privateStringgetMethod;
@Test
publicvoidtestMethod(){
//知识追寻者
System.out.println(getMethod);
}
获取属性示例
注意点:username字段必须是public
@Value("#{environmentProperty.username}")
privateStringgetField;
@Test
publicvoidtestField(){
//知识追寻者
System.out.println(getField);
}
获取静态方法示例
其中T()表示运算会得到一个Class对象;
@Value("#{T(java.lang.Math).random()}")
privatedoublenumber;
@Test
publicvoidtestStatic(){
//0.9205474938572363
System.out.println(number);
}
非空判定示例
其中?表示非空判定
@Value("#{environmentProperty.username?.toString()}")
privateStringnotNull;
@Test
publicvoidtestNotNUll(){
//知识追寻者
System.out.println(notNull);
}
支持运算符如下
- 算术运算+、-、*、/、%、^
- 比较运算<、>、==、<=、>=、lt、gt、eq、le、ge
- 逻辑运算and、or、not、│
- 条件运算?:(ternary)、?:(Elvis)
- 正则表达式matches
更多内容读者自行参考官网学习
https://docs.spring.io/spring/docs/4.2.x/spring-framework-reference/html/expressions.html
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。