【问题标题】:Spring 4 PropertySourcesPlaceholderConfigurer doesn't resolve ${..} placeholders across modulesSpring 4 PropertySourcesPlaceholderConfigurer 不能跨模块解析 ${..} 占位符
【发布时间】:2015-08-01 14:20:46
【问题描述】:

我的应用程序中有两个模块:

  • 核心
  • 网络

core 模块在spring/applicationContext-core.xml 上下文中包含以下属性占位符配置:

<bean id="coreProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="locations">
        <list>
            <value>classpath:/properties/*.properties</value>
            <value>classpath:/profiles/${build.profile.id}/properties/*.properties</value>
            <value>file:${ui.home}/profiles/${build.profile.id}/properties/*.properties</value>
        </list>
    </property>
</bean>

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="ignoreUnresolvablePlaceholders" value="true"/>
    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
    <property name="ignoreResourceNotFound" value="false"/>

    <property name="properties" ref="coreProperties" />
</bean>

<bean id="propertySourcesPlaceholderConfigurer" class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
    <property name="ignoreUnresolvablePlaceholders" value="true"/>
</bean>

考虑到我有以下属性:

resource.suffix=.min

如果我将此值注入 core @Component:

@Value("${resource.suffix}")
private String resourceSuffix;

属性已正确解析。

但是,如果我在 web 模块内的 bean 中添加相同的配置,它也会简单地加载核心配置:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/servlet-context.xml /WEB-INF/application-security.xml
        classpath*:/spring/applicationContext-core.xml</param-value>
</context-param>

然后属性未解析,resourceSuffix 值设置为以下字符串文字值${resource.suffix

我错过了什么?

【问题讨论】:

  • @Value("${resource.suffix}") 再次在属性上,而不是在构造参数上,对吗?您还确定 web 模块 bean 是由 spring 实例化/扫描的吗?
  • 它位于 @Controller 内的字段上,并且该 bean 被扫描,因为它还注入了其他 @Autowired bean。
  • 哪个上下文正在实例化“web”模块?您可以尝试将 propertySourcesPlaceholderConfigurer 移到该上下文中吗?
  • 我将尝试复制propertySourcesPlaceholderConfigurer,因为我还需要解析core 模块上的一些属性。

标签: java spring properties configuration placeholder


【解决方案1】:

我认为这与 spring 如何与前/后处理器一起工作有关。

基本上,您可以有重复的定义或使用不同的机制来加载属性。

据我所知,在 spring 3.1 之前复制是唯一的方法。

更多关于http://www.baeldung.com/2012/02/06/properties-with-spring/#parent-child

【讨论】:

  • 谢谢米特。现在我必须阅读所有 Eugen 的帖子,以免自己从一些与 Spring 配置相关的挫折中解脱出来。
  • 我认为@PropertySource 是一个更好的选择,除非遗留代码然后复制是要走的路
猜你喜欢
  • 1970-01-01
  • 2015-03-11
  • 1970-01-01
  • 2015-05-22
  • 2022-11-01
  • 2016-07-24
  • 2018-02-20
  • 2017-12-15
  • 2017-05-01
相关资源
最近更新 更多