【问题标题】:Reading 2 property files having same variable names in Spring在 Spring 中读取 2 个具有相同变量名的属性文件
【发布时间】:2015-06-22 11:40:53
【问题描述】:

我正在使用 Spring xml 中的以下条目读取属性文件。

<context:property-placeholder 
    location="classpath:resources/database1.properties,
              classpath:resources/licence.properties"/>

我正在使用 xml 条目或使用 @Value 注释在变量中注入这些值。

<bean id="myClass" class="MyClass">
    <property name="driverClassName" value="${database.driver}" />
    <property name="url" value="${database.url}" />
    <property name="name" value="${database.name}" />
</bean>

我想添加一个新的属性文件 (database2.properties),它与 ​​database1.properties 的变量名很少。

database1.properties:

database.driver=com.mysql.jdbc.Driver
database.url=jdbc:mysql://192.168.1.10/
database.name=dbname

database2.properties:

database.url=jdbc:mysql://192.168.1.50/
database.name=anotherdbname
database.user=sampleuser

您可以在两个属性文件中看到少数属性变量具有相同名称,例如 database.urldatabase.name

是否可以注入database.url的database2.properties?

或者我必须更改变量名?

谢谢。

【问题讨论】:

    标签: java spring spring-mvc properties-file


    【解决方案1】:

    你迟早会切换到 Spring Boot。所以使用 Spring Boot 你可以拥有这样的 POJO:

    public class Database {
    
      @NotBlank
      private String driver;
      @NotBlank
      private String url;
      @NotBlank
      private String dbname;
    
      public String getDriver() {
        return driver;
      }
      public void setDriver(String driver) {
        this.driver = driver;
      }
      public String getUrl() {
        return url;
      }
      public void setUrl(String url) {
        this.url = url;
      }
      public String getDbname() {
        return dbname;
      }
      public void setDbname(String dbname) {
        this.dbname = dbname;
      }
    }
    

    并使用@ConfigurationProperties 填充它:

    @Bean
    @ConfigurationProperties(locations="classpath:database1.properties", prefix="driver")
    public Database database1(){
      return new Database();
    }
    
    @Bean
    @ConfigurationProperties(locations="classpath:database2.properties", prefix="driver")
    public Database database2(){
      return new Database();
    }
    

    这样做的缺点是它是可变的。使用 Lombok 库,您可以消除讨厌的 getter 和 setter。

    【讨论】:

    • +1 感谢您的回答。目前我没有使用Spring Boot,因此无法使用您的解决方案..
    【解决方案2】:

    你可以通过配置两个PropertyPlaceholderConfigurer来实现。通常只有一个实例提供所有属性,但是,如果您更改placeholderPrefix,您可以使用两个实例,例如

    <bean id="firstPropertyGroup" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
      <property name="locations" value="classpath:resources/database1.properties,
                  classpath:resources/licence.properties" />
       <property name="placeholderPrefix" value="${db1."/>
    </bean>
    
    <bean id="secondPropertyGroup" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
      <property name="locations" value="classpath:resources/database2.properties" />
      <property name="placeholderPrefix" value="${db2."/>"
    </bean>
    

    然后您将访问您的属性,例如 ${db1.database.url}${db2.database.url}

    【讨论】:

      【解决方案3】:

      可能有一个解决方案,类似于您想要实现的目标。检查此问题的第二个答案:Multiple properties access。它基本上解释了如何使用您定义的另一个表达式来访问第二个文件的属性。 否则,最简单的解决方案就是更改键值(变量名)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-17
        相关资源
        最近更新 更多