Marcin Majewski
Marcin Majewski

Reputation: 1047

Resolving system properties when app starts on tomcat

So I have my spring bott app:

@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);
    }
...

When i run my app on windows configuration.file is set properly but when I run this on tomcat server I get:

Could not resolve placeholder 'configuration.file' in string value "${configuration.file}"

What could be the cause of the problem?

Upvotes: 0

Views: 282

Answers (1)

Prakash Dayaramani
Prakash Dayaramani

Reputation: 116

Its clear that this is due to problem with the @PropertySource annotation defined. You need to define the actual of the property file say xyz.properties to be defined in that annotation. You can also give placeholder there. The ideal way of doing it will be,

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

}

Have a look at the different examples of the annotation here

Upvotes: 1

Related Questions