所以我有我的春季应用程序:
@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上运行我的应用程序时,配置正确。但是当我在tomcat服务器上运行时,我得到:
Could not resolve placeholder 'configuration.file' in string value "${configuration.file}"
问题可能是什么原因?
答案 0 :(得分:1)
很明显,这是由于定义了@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
的不同示例