当将值加载到XML配置时,忽略了Spring Boot文件属性,而将classPath属性取代



我有一个属性配置bean看起来像:

<bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
    <property name="ignoreUnresolvablePlaceholders" value="false"/>
    <property name="ignoreResourceNotFound" value="true"/>
    <property name="localOverride" value="false"/>
    <property name="locations">
        <list>
            <value>file://${emulator.config}</value>
            <value>classpath:emulator.properties</value>
            <value>file://${db.config}</value>
            <value>classpath:db.properties</value>
        </list>
    </property>
</bean>

第一个模拟器配置中的值以@Value为:

在代码中获取。
@Value("${emulator.database.template.create}")
private String createDBPath;

按期望的顺序正确采用的所有内容:

  1. 如果我向像这样的JVM选项提供外部文件路径

      java 
     -Dlog4j.configurationFile=/correctfullpath/log4j2.xml 
     -Dspring.config.location=/correctfullpath/application.properties 
     -Demulator.config=/correctfullpath/emulator.properties 
     -Ddb.config=/correctfullpath/db.properties -jar target/registrator-emulator.war
    

我得到了从提供的/crorcectfullpath/emulator.properties获取的 @value填充变量的值;

  1. 如果选择了JVM选项,则该值是从JAR
  2. 中的类Path属性文件中获取的

,如果我要获得xml-configurated bean的属性值:

<bean id="manageDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${database.driver}"/>
    <property name="url" value="${database.url}"/>
    <property name="username" value="${database.user}"/>
    <property name="password" value="${database.password}"/>
</bean>

shere $ {database.url}使用db.properties忽略了用-ddb.config =/requortfullpath/db.properties选项提供的值;它们总是从JAR的ClassPath属性文件中获取。唯一的解决方法(不太适合我)是从"占位符"属性bean中评论classpath属性:

<bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
    <property name="ignoreUnresolvablePlaceholders" value="false"/>
    <property name="ignoreResourceNotFound" value="true"/>
    <property name="localOverride" value="false"/>
    <property name="locations">
        <list>
            <value>file://${emulator.config}</value>
            <value>classpath:emulator.properties</value>
            <value>file://${db.config}</value>
            <!--<value>classpath:db.properties</value>-->
        </list>
    </property>
</bean>

因此,当提供文件属性时,是否可以在XML配置中忽略Class Path属性?

upd。根据Deinum M. Deinum的建议

但我仍然必须评论classpath db.properties:

@Configuration
@PropertySources({@PropertySource("file://${emulator.config}"),
        @PropertySource("classpath:emulator.properties"),
        @PropertySource(value = "file://${statsd.config}",ignoreResourceNotFound = true),
        @PropertySource("classpath:statsd.properties"),
        @PropertySource(value = "file://${pools.config}",ignoreResourceNotFound = true),
        @PropertySource("classpath:pools.properties"),
        @PropertySource(value= "file://${db.config}"),
        //@PropertySource("classpath:db.properties")
        })

在这里如何创建bean:

@value(" $ {database.driver}") 私有字符串数据级;

@Value("${database.url}")
private String dataBaseUrl;
@Value("${database.user}")
private String dataBaseUser;
@Value("${database.password}")
private String dataBasePassword;


@Bean
@Primary
public DataSource manageDataSource() {
    return DataSourceBuilder
            .create()
            .username(dataBaseUser)
            .password(dataBasePassword)
            .url(dataBaseUrl)
            .driverClassName(dataBaseDriver)
            .build();
}

问题的根源是属性顺序!

由于文件属性覆盖了classPath属性,因此应将它们放在class路径之后:

@PropertySources({
        @PropertySource("classpath:emulator.properties"),
        @PropertySource(value = "file://${emulator.config}", ignoreResourceNotFound = true),
        @PropertySource("classpath:statsd.properties"),
        @PropertySource(value = "file://${statsd.config}",ignoreResourceNotFound = true),
        @PropertySource("classpath:pools.properties"),
        @PropertySource(value = "file://${pools.config}",ignoreResourceNotFound = true),
        @PropertySource("classpath:db.properties"),
        @PropertySource(value= "file://${db.config}",ignoreResourceNotFound = true)
        })

相关内容

最新更新