【问题标题】:What is the Best way to get the values from the Properties File in Spring?在 Spring 中从属性文件中获取值的最佳方法是什么?
【发布时间】:2018-03-24 21:24:27
【问题描述】:

我使用以下方法从属性中获取值。但我想知道其中哪一个最适合遵循编码标准?另外,还有其他方法可以从 Spring 的属性文件中获取值吗?

PropertySourcesPlaceholderConfigurer 
getEnvironment() from the Spring's Application Context
Spring EL @Value

【问题讨论】:

    标签: java spring


    【解决方案1】:

    与其他配置类(ApplicationConfiguration 等)一起,我创建了一个带有注释 @Service 的类,在这里我有以下字段可以访问我文件中的属性:

    @Service
    public class Properties (){
    
        @Value("${com.something.user.property}")
        private String property;
    
        public String getProperty (){ return this.property; }
    
    }
    

    然后我可以自动装配类并从我的属性文件中获取属性

    【讨论】:

      【解决方案2】:

      答案是, 视情况而定。

      如果属性是配置值, 然后配置一个propertyConfigurer (以下是 Spring xml 配置文件的示例)。

      <bean id="propertyConfigurer"
            class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
          <property name="ignoreResourceNotFound" value="true" />
          <property name="locations">
              <list>
                  <value>classpath:configuration.properties</value>
                  <value>classpath:configuration.overrides.properties</value>
              </list>
          </property>
      </bean>
      

      以这种方式配置时, 找到的最后一个文件的属性会覆盖之前找到的属性 (在位置列表中)。 这使您可以发送捆绑在 war 文件中的标准 configuration.properties 文件,并在每个安装位置存储一个 configuration.overrides.properties,以解决安装系统的差异。

      一旦你有了一个propertyConfigurer, 使用 @Value 注释来注释你的类。 这是一个例子:

      @Value("${some.configuration.value}")
      private String someConfigurationValue;
      

      不需要将配置值聚集到一个类中, 但这样做更容易找到值的使用位置。

      【讨论】:

        【解决方案3】:

        @Value 将是一种简单易用的使用方式,因为它会将值从属性文件注入到您的字段中。

        Spring 3.1 中添加的旧 PropertyPlaceholderConfigurer 和新 PropertySourcesPlaceholderConfigurer 都解析了 bean 定义属性值和 @Value 注释中的 ${…} 占位符。

        不同于 getEnvironment

        使用 property-placeholder 不会将属性暴露给 Spring Environment - 这意味着检索这样的值 不起作用 - 它会返回 null

        当您使用&lt;context:property-placeholder location="classpath:foo.properties" /&gt; 并使用env.getProperty(key); 时,它将始终返回null。

        有关使用 getEnvironment 的问题,请参阅此帖子:Expose <property-placeholder> properties to the Spring Environment

        此外,在 Spring Boot 中,您可以使用 @ConfigurationProperties 在 application.properties 中定义具有分层和类型安全的属性。而且你不需要为每个字段都加上@Value。

        @ConfigurationProperties(prefix = "database")
        public class Database {
            String url;
            String username;
            String password;
        
            // standard getters and setters
        }
        

        在 application.properties 中:

        database.url=jdbc:postgresql:/localhost:5432/instance
        database.username=foo
        database.password=bar
        

        引用自:properties with spring

        【讨论】:

          猜你喜欢
          • 2016-02-11
          • 1970-01-01
          • 2021-01-14
          • 1970-01-01
          • 1970-01-01
          • 2016-02-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多