gpt4 book ai didi

java - 使用自定义 ClassPathResource 配置 Spring 4 PropertySourcesPlaceholderConfigurer

转载 作者:行者123 更新时间:2023-11-30 11:05:17 24 4
gpt4 key购买 nike

如何使用自定义 .properties 文件为不同的环境(生产、开发、暂存)配置 PropertySourcesPlaceholderConfigurer?在部署 spring 时抛出“无法解析字符串值“classpath:${property.placeholder}”中的占位符 'property.placeholder'”“

这是我的 pom.xml

<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<property.placeholder>_developer.properties</property.placeholder>
</properties>
</profile>
<profile>
<id>staging</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<properties>
<property.placeholder>_staging.properties</property.placeholder>
</properties>
</profile>
<profile>
<id>production</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<properties>
<property.placeholder>_production.properties</property.placeholder>
</properties>
</profile>
</profiles>

这里是 PropertySourcesPlaceholderConfigurer 配置

static @Bean
public PropertySourcesPlaceholderConfigurer myPropertySourcesPlaceholderConfigurer() {
PropertySourcesPlaceholderConfigurer p = new PropertySourcesPlaceholderConfigurer();
org.springframework.core.io.Resource[] resourceLocations = new org.springframework.core.io.Resource[] {
new ClassPathResource("${property.placeholder}")
};
p.setLocations(resourceLocations);
return p;
}

它在 xml spring 配置中工作,但如果我使用 java 配置则不起作用。知道它是如何工作的吗?

最佳答案

我找到了另一种方法来做我想做的事:

首先我将这个插件添加到我的 pom.xml 中的构建部分:

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0-alpha-2</version>
<executions>
<execution>
<id>1</id>
<phase>initialize</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
<configuration>
<files>
<file>src/main/resources/${property.placeholder}</file>
</files>
</configuration>
</execution>
<execution>
<id>2</id>
<phase>generate-resources</phase>
<goals>
<goal>write-project-properties</goal>
</goals>
<configuration>
<outputFile>
${project.build.outputDirectory}/application.properties
</outputFile>
</configuration>
</execution>
</executions>
</plugin>

第二次我在我的 spring 配置文件中修改 PropertySourcesPlaceholder 配置:

static @Bean
public PropertySourcesPlaceholderConfigurer myPropertySourcesPlaceholderConfigurer() {
PropertySourcesPlaceholderConfigurer p
= new PropertySourcesPlaceholderConfigurer();
org.springframework.core.io.Resource[] resourceLocations
= new org.springframework.core.io.Resource[] {
new ClassPathResource("application.properties")
};
p.setLocations(resourceLocations);
return p;
}

我还将此注释添加到我的 spring 配置类中:

@PropertySource("classpath:application.properties")

所以现在我将依赖环境的属性写入不同的属性文件(如“_developer.properties”或“_staging.properties”),当我使用 Maven 构建项目时,它已被复制到我使用的“application.properties”在 PropertySourcesPlaceholder 配置中

关于java - 使用自定义 ClassPathResource 配置 Spring 4 PropertySourcesPlaceholderConfigurer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29704901/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com