当前位置:   article > 正文

使用spring注解方法读取properties文件中值_spring注解读取properties的值

spring注解读取properties的值
引用properties还是要在xml里配置,有两种配置方法,对引用单个properties文件,和多个properties文件
一、读取单个 properties 文件
在 spring 的配置文件中,加入
引入until命名空间:
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/util
        http://www.springframework.org/schema/util/spring-util-3.0.xsd"
读取properties文件
<util:properties id="propertiesReader" location="classpath:test.properties" /> 
二、读取多个 properties 文件
<bean id="propertiesReader"
  class="org.springframework.beans.factory.config.PropertiesFactoryBean">
  <property name="locations">
   <list>
    <value>classpath:param.properties</value>
    <value>classpath:base.properties</value>
   </list>
  </property>
</bean>

两种方法其实都是一样的


在类中读取properties文件属性

在类中需要注入的属性实现 setter 和 getter 方法。

在 setter 方法前,添加 @Value 注解
@Value("#{propertiesReader[propertiesName]}")
propertiesName 为 properties 文件中的键。这样,在容器启动过程中, Spring 将自动注入值。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/代码探险家/article/detail/939216
推荐阅读
相关标签
  

闽ICP备14008679号