java - 通过XML配置spring PropertySourcePlaceholderConfigurer和环境

标签 java spring maven spring-mvc

我正在尝试升级一些旧应用程序的库,其中一部分是从 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,以便 Autowiring 的环境对象可以简单地调用 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 ${} 样式注释来获取这些值,并且效果很好。但我仍然很好奇为什么环境没有按我的预期工作。

最佳答案

如果您使用 XML 配置,您是否尝试过添加 <context:property-placeholder location="classpath:foo.properties" />或者 <context:property-placeholder location="classpath:${app.env}.properties"/>

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

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

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

关于java - 通过XML配置spring PropertySourcePlaceholderConfigurer和环境,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29899030/

相关文章:

java - Kafka消费者-消费者进程和线程与主题分区的关系是什么

java - Gradle Artifactory 插件 - 如何自动部署 Spring Boot Fatjar

java - 线程池为 7 的 Spring 调度程序无法正常工作

java - Maven:提供依赖项的多个版本

java - Zookeeper cfg 文件 - 为什么有多个端口?

java - Android studio 邮件问题

java - Java 中读取的 POM XML 属性变为 null

java - 如何在公共(public)pom中保留版本号并在其他pom文件中使用它(spring maven项目)

java - 缓慢移动的物体(视觉)

java - Spring 验证 : Multiple DTOs vs single DTO with multiple validators