无法解决占位符

时间:2015-07-25 10:01:14

标签: java spring maven

我是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 个答案:

答案 0 :(得分: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;
}
}

一切都按预期完美运作。

相关问题