所以我有我的春季启动应用程序:

@Configuration
@PropertySource("${configuration.file}")
public class Application extends SpringBootServletInitializer implements CommandLineRunner {
...
    public static void main(String[] args) throws IOException {
        SpringApplication app = new SpringApplication(Application.class);
        System.setProperty("configuration.file","file:"+"path to my file");
        app.run(args);
    }
...

当我在Windows configuration.file上运行我的应用程序时,设置正确,但是在tomcat服务器上运行时,我得到:
Could not resolve placeholder 'configuration.file' in string value "${configuration.file}"

是什么原因引起的?

最佳答案

很明显,这是由于定义的@PropertySource注释存在问题。您需要定义要在该批注中定义的属性文件的实际值,例如xyz.properties。您也可以在此处提供占位符。理想的做法是

@Configuration
 @PropertySource("classpath:/com/${my.placeholder:default/path}/app.properties")
public class AppConfig {
 @Autowired
 Environment env;

 @Bean
 public TestBean testBean() {
     TestBean testBean = new TestBean();
     testBean.setName(env.getProperty("testbean.name"));
     return testBean;
 }


}

看看注释here的不同示例

10-04 20:12