问题描述
我真的是Spring和Maven的新手,我想创建一个特定于环境的构建。 主要思想是在Maven中创建概要文件,并且概要文件设置了一些变量以帮助加载正确的属性文件。
这是我在maven中的个人资料之一:
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<env>dev</env>
</properties>
</profile>
这是我的FTPProperties类:
@Configuration
@PropertySource("classpath:/properties/ftp-${env}.properties")
public class FTPProperties {
@Autowired
private Environment environment;
private String server;
public FTPProperties() {
}
@PostConstruct
private void init(){
this.server = environment.getProperty("ftp.server");
}
public String getServer() {
return server;
}
}
当我尝试构建它时,出现以下异常:
java.lang.IllegalArgumentException: Could not resolve placeholder 'env' in string value "classpath:/properties/ftp-${env}.properties"
任何帮助是极大的赞赏。
1楼
我找到了解决方案:
删除FTPProperties类,并将配置移至applicationContext.xml,如下所示:
<bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
<property name="ignoreUnresolvablePlaceholders" value="true"/>
<property name="locations">
<list>
<value>classpath:/properties/ftp-${env}.properties</value>
</list>
</property>
</bean>
我刚刚更新了我的Maven个人资料:
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<env>dev</env>
</properties>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
</profile>
之后,我创建了一个FTPService类:
@Service
public class FTPService {
@Value("${ftp.server}")
private String server;
public String getServer() {
return server;
}
}
一切都按预期完成。