Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在spring配置中使用属性_Java_Spring - Fatal编程技术网

Java 在spring配置中使用属性

Java 在spring配置中使用属性,java,spring,Java,Spring,根据我的环境,我有一个属性配置,如下所示: <bean id="someConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:properties

根据我的环境,我有一个属性配置,如下所示:

 <bean id="someConfigurer" 
       class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">  
        <list> 
            <value>classpath:properties/database.${my.env}.properties</value>
            <value>classpath:properties/morestuff.${my.env}.properties</value>
            [..]
        </list>  
    </property>
</bean>
${my.env:qual}
如果我注释掉PropertyPlaceHolderConfigure以消除错误,我可以在其他bean中使用
%{my.env}


有人能解释这种行为吗?

您可以这样设置默认值(在Spring 3中):

 <bean id="someConfigurer" 
       class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">  
        <list> 
            <value>classpath:properties/database.${my.env}.properties</value>
            <value>classpath:properties/morestuff.${my.env}.properties</value>
            [..]
        </list>  
    </property>
</bean>
${my.env:qual}

试图添加的长镜头

property name=“IgnoreUnsolvablePlaceholders”value=“true”/>


对于您的第一个bean

我建议您使用本地化构建,而不是在Spring上下文中处理文件名

正如farmor所说,属性'IgnoreUnsolvablePlaceholders'也应该为第一个bean设置为true,如API中所述

默认值为“false”:如果占位符失败,将引发异常 解决。将此标志切换为“true”,以保留 占位符字符串,在这种情况下,由其他人决定 占位符配置程序来解析它

您还可以使用:

<property name="ignoreResourceNotFound" value="true"/>
<property name="ignoreUnresolvablePlaceholders" value="true"/>