@PropertySource

时间:2019-01-09 11:05:52

标签: java linux spring-boot

我正在做一个Springboot应用程序,并且已经外部化了配置文件。我使用Eclipse在Windows 10上开发了该应用程序,并在带有Tomcat 8的Debian 9服务器上运行。

配置文件的路径在名为DDK_OPT的环境变量中定义。此变量在“ /etc/environment”中定义为:

DDK_OPT=/opt/DDK

在Springboot应用程序中,我尝试在@PropertySource装饰器中使用它:

@Configuration
@PropertySource({"file:${DDK_OPT}/application.properties"})
@EnableJpaRepositories(
        basePackages = "eus.ddk.repository.APL", 
        entityManagerFactoryRef = "aplEntityManager", 
        transactionManagerRef = "aplTransactionManager"
)
public class APLPersistenceConfig {
...

Tomcat启动时,应用程序失败,并显示以下错误消息:

2019-01-09 11:37:23.610 [ERROR] SpringApplication.Application startup failed
org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [eus.ddk.APLApplication]; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'DDK_OPT' in value "file:${DDK_OPT}/application.properties"
        at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:183) ~[ConfigurationClassParser.class:4.3.21.RELEASE]
        at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:308) ~[ConfigurationClassPostProcessor.class:4.3.21.RELEASE]
        at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:228) ~[ConfigurationClassPostProcessor.class:4.3.21.RELEASE]
        at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:272) ~[PostProcessorRegistrationDelegate.class:4.3.21.RELEASE]
        at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:92) ~[PostProcessorRegistrationDelegate.class:4.3.21.RELEASE]
...

在Windows上,该应用程序可以完美运行,但是我无法使其在服务器上运行。

任何帮助将不胜感激。

谢谢!

1 个答案:

答案 0 :(得分:0)

我没有在/etc/environment中设置环境变量,而是在/etc/default/tomcat8文件中进行了设置,并且它按我的意愿开始工作。

仍然感谢斯蒂芬的帮助!