【问题标题】:Resolving system properties when app starts on tomcat应用程序在tomcat上启动时解析系统属性
【发布时间】:2015-10-13 07:04:37
【问题描述】:

所以我有我的 spring bott 应用程序:

@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}"

问题的原因可能是什么?

【问题讨论】:

    标签: java spring spring-boot spring-properties


    【解决方案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的不同例子

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-02-27
      • 2017-08-14
      • 1970-01-01
      • 2010-09-27
      • 1970-01-01
      • 2018-03-01
      • 1970-01-01
      相关资源
      最近更新 更多