【问题标题】:Configuring spring PropertySourcePlaceholderConfigurer and environment via XML通过 XML 配置 spring PropertySourcePlaceholderConfigurer 和环境
【发布时间】:2015-04-27 14:50:47
【问题描述】:

我正在尝试升级我们一些旧应用程序的库,其中一部分是从 Spring 3 迁移到 Spring 4.1.6。我有一个我认为是非常典型的配置,它使用 Maven 来引入依赖项,然后使用 Maven 配置文件和过滤来选择正确的属性文件来运行应用程序。看起来像这样。

pom.xml

<profiles>
    <profile>
        <id>dev</id>
        <properties>
            <app.env>Development</app.env>
        </properties>
        <build>
            <resources>
                <resource>
                    <directory>${basedir}/src/main/resources</directory>
                    <filtering>true</filtering>
                </resource>
            </resources>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>2.4</version>
                    <configuration>
                        <webResources>
                            <webResource>
                                <directory>${basedir}/src/main/webapp/WEB-INF</directory>
                                <includes>
                                    <include>web.xml</include>
                                </includes>
                                <filtering>true</filtering>
                                <targetPath>WEB-INF</targetPath>
                            </webResource>
                        </webResources>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>

我的 spring 应用程序上下文位于带有 PropertySourcesPlaceholderConfigurer bean 定义的过滤资源目录中。 ${app.env} 被过滤掉以由 Development.properties 文件加载

ApplicaitonContext.xml
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:com/tms/edu/settings/${app.env}.properties</value>
            </list>
        </property>
    </bean>

此时我的理解是,当使用 ProperySourcesPlaceholderConfigurer 而不是旧的 PropertyPlaceHolderConfigurer 时,它应该自动将资源注册为 PropertySource,以便自动连接的 Environment 对象可以简单地调用 getProperty("db.location") 并拥有它在运行时可用。所以我应该能够在我扫描的包中有一个简单的类,它基本上可以做到这一点:

@Service
public class PropertyReader {

    @Autowired
    private Environment env;

    public String getPropertyValue(String key) {
        return env.getProperty(urlkey);
    }
}

但这不起作用。 getProperty() 为应该工作的键返回 null 并在调试器中检查环境对象我没有看到我的资源加载到 propertySources 列表中。

我只是在寻找如何使用注释配置 PropertySource 的示例,但这不适用于我们在 Maven 构建中管理配置文件和属性的方式。我知道我可以使用 @Value ${} 样式的注释来获取这些值,并且效果很好。但是我仍然很好奇为什么 Environment 没有像我预期的那样工作。

【问题讨论】:

  • 同样的问题,我什么都试过了。你解决问题了吗?
  • 已经有一段时间了,所以这可能并不令人满意,但是通过代码看起来我可以在一个项目中使用它,但配置似乎与我在我的问题。我使用@Configuration @PropertySource("classpath:application.properties") 的其他项目只是硬编码在一个属性文件中。

标签: java spring maven spring-mvc


【解决方案1】:

如果您使用 XML 配置,您是否尝试过添加 &lt;context:property-placeholder location="classpath:foo.properties" /&gt;&lt;context:property-placeholder location="classpath:${app.env}.properties"/&gt;?

相当于@PropertySource("classpath:whatever.properties")

此命名空间启用 PropertySources,请参阅 here on "Using the @Value annotation"

注意:classpath 可以直接引用resources 文件夹,这是将属性作为 Maven 项目放置的正常位置

【讨论】:

  • 谢谢,我没试过。但我似乎以这种方式得到了同样的结果。我在 Spring PropertiesLoader 类中有一个断点,我可以看到它使用 PropertiesLoaderUtils 查找和加载我的资源。我得到记录器输出:2015-Apr-27 11:38:44,112 INFO PropertySourcesPlaceholderConfigurer:172 - 从类路径资源加载属性文件 [com/tms/edu/settings/SystemTest.properties] 但是一旦它到达 env.getProperties(键)它返回null。
  • 奇怪,Environment 不是 nul 吗? Spring识别您的文件,您的属性是正确的并且其中的值是正确的?我已经遇到过这种问题,但这是原因之一,或者只是需要一个干净的构建。
  • 环境“正确”自动连接(减去预期属性)。有趣的是,我试图通过实现 ApplicationContextAware 来获取 applicationContext,我在我的 xml 中包含了 bean 定义,但除非我明确地自动装配 bean,否则不会调用 setApplicationContext()。这就像我有一个延迟加载的上下文的行为,但我从来没有设置它。我认为存在一些奇怪的更深层次的配置问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-21
  • 2013-08-16
  • 2016-09-12
  • 1970-01-01
相关资源
最近更新 更多