一 前言
本篇内容包括spring 运行时读取配置文件的多种方式和SpEl表达式入门基础;以为文章不错点赞在看谢谢。html
知识追寻者(Inheriting the spirit of open source, Spreading technology knowledge;)java
二运行时读取配置文件
spring 运行时读取配置文件值提供了2种方式正则表达式
- 属性占位符(Property placeholder)。
- Spring表达式语言(SpEL)
2.1 读取外部配置文件
使用 @PropertySource 注解能够读取导classpath下配置文件属性;参数以下spring
- value是个字符串数组;
- ignoreResourceNotFound;若是设置为true, 配置文件未找到时不会报错;
- encoding;指定字符集
首先resource 目录下建立配置文件zszxz.properties ; 内容以下express
zszxz.name = zszxz
zszxz.point = share
复制代码
其次读取配置文件配置类以下数组
@Configuration
@PropertySource(value = {"classpath:zszxz.properties"},encoding = "UTF-8")
@Component
public class EnvironmentProperty {
// 注入环境
@Autowired
private Environment environment;
public void outputProperty(){
System.out.println(environment.getProperty("zszxz.name"));
}
}
复制代码
最后经过测试类调用outputProperty()输出配置文件中属性的值markdown
@RunWith(SpringJUnit4ClassRunner.class)//建立spring应用上下文
@ContextConfiguration(classes= EnvironmentProperty.class)//加载配置类
public class PropertyTest {
@Autowired
EnvironmentProperty environmentProperty;
@Test
public void test(){
// zszxz
environmentProperty.outputProperty();
}
}
复制代码
Tip 也能够使用@PropertySources 注解,其value是 @PropertySource类型的数组;dom
其中 EnvironmentProperty 获取主要属性方法以下学习
- String getProperty(String key); 经过key 取值
- String getProperty(String key, String defaultValue); 获取值,没有则使用默认值;
- T getProperty(String key, Class var2); 获取值,指定返回类型;
- T getProperty(String key, Class var2, T defaultValue);获取值,指定返回类型,指定默认值;
- String getRequiredProperty(String key) ; key必须为非空不然抛出
IllegalStateException
异常
2.2 使用占位符获取配置文件
使用注解@Value获取配置文件属性值; 其中值使用占位符("${........}")方式;测试
配置类示例
@Configuration
@PropertySource(value = {"classpath:zszxz.properties"},encoding = "UTF-8")
@Component
public class EnvironmentProperty {
@Value("${zszxz.point}")
private String point;
public void outputPoint(){
System.out.println(point);
}
}
复制代码
测试示例
@RunWith(SpringJUnit4ClassRunner.class)//建立spring应用上下文
@ContextConfiguration(classes= EnvironmentProperty.class)//加载配置类
public class PropertyTest {
@Autowired
EnvironmentProperty environmentProperty;
@Test
public void testPoint(){
// share
environmentProperty.outputPoint();
}
}
复制代码
2.3 SpEl表达式
Spring表达式语言(Spring Expression Language,SpEL)是一种灵活的表达式语言,可以以简洁的方式将值装配到bean属性或者构造器参数中,此过程当中可以计算表达式获取计算值;使用@Valjue注解时,SpEL表达式要放到“#{......}”之中;
获取bean示例
@Value("#{environmentProperty}")
private EnvironmentProperty getBean;
@Test
public void testBean(){
// com.zszxz.property.EnvironmentProperty?EnhancerBySpringCGLIB?8e54e11f@1d9b7cce
System.out.println(getBean);
}
复制代码
获取方法示例
@Value("#{environmentProperty.getStr()}")
private String getMethod;
@Test
public void testMethod(){
// 知识追寻者
System.out.println(getMethod);
}
复制代码
获取属性示例
注意点:username字段必须是public
@Value("#{environmentProperty.username}")
private String getField;
@Test
public void testField(){
// 知识追寻者
System.out.println(getField);
}
复制代码
获取静态方法示例
其中T()表示运算会获得一个Class对象;
@Value("#{T(java.lang.Math).random()}")
private double number;
@Test
public void testStatic() {
// 0.9205474938572363
System.out.println(number);
}
复制代码
非空断定示例
其中? 表示非空断定
@Value("#{environmentProperty.username?.toString()}")
private String notNull;
@Test
public void testNotNUll() {
// 知识追寻者
System.out.println(notNull);
}
复制代码
支持运算符以下
- 算术运算 + 、 - 、 * 、 / 、 % 、 ^
- 比较运算 < 、 > 、 == 、 <= 、 >= 、 lt 、 gt 、 eq 、 le 、 ge
- 逻辑运算 and 、 or 、 not 、 │
- 条件运算 ?: (ternary) 、 ?: (Elvis)
- 正则表达式 matches
更多内容读者自行参考官网学习
源码: 公众号 知识追寻者 文章汇总处
本文同步分享在 博客“知识追寻者”(JueJin)。
若有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一块儿分享。