400 028 6601

建站动态

根据您的个性需求进行定制 先人一步 抢占小程序红利时代

Spring怎么读取配置文件属性

这篇文章主要讲解了Spring怎么读取配置文件属性,内容清晰明了,对此有兴趣的小伙伴可以学习一下,相信大家阅读完之后会有帮助。

按需设计网站可以根据自己的需求进行定制,网站设计、网站制作构思过程中功能建设理应排到主要部位公司网站设计、网站制作的运用实际效果公司网站制作网站建立与制做的实际意义

一 前言

本篇内容包括spring 运行时读取配置文件的多种方式和SpEl表达式入门基础;

二运行时读取配置文件

spring 运行时读取配置文件值提供了2种方式

属性占位符(Property placeholder)。

Spring表达式语言(SpEL)

2.1 读取外部配置文件

使用 @PropertySource 注解可以读取导classpath下配置文件属性;参数如下

首先resource 目录下创建配置文件zszxz.properties ; 内容如下

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()输出配置文件中属性的值

@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类型的数组;

其中 EnvironmentProperty 获取主要属性方法如下

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

支持运算符如下

看完上述内容,是不是对Spring怎么读取配置文件属性有进一步的了解,如果还想学习更多内容,欢迎关注创新互联行业资讯频道。


网站标题:Spring怎么读取配置文件属性
文章路径:http://mzwzsj.com/article/pshgeg.html

其他资讯

让你的专属顾问为你服务