2
votes

I am new to Spring-Boot application and trying to read external property file in my Spring Boot application which is in my resource folder besides application.properties, but getting following exception,

enter image description here

Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [com.test.config.MyInetgrationApplicationConfig]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/myIntegration.properties]
2017-09-06 17:27:04.866 ERROR 10512 --- [ost-startStop-1] o.s.b.f.s.DefaultListableBeanFactory     : Destroy method on bean with name 'org.springframework.boot.autoconfigure.internalCachingMetadataReaderFactory' threw an exception

java.lang.IllegalStateException: ApplicationEventMulticaster not initialized - call 'refresh' before multicasting events via the context: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@225fa519: startup date [Wed Sep 06 17:27:04 IST 2017]; root of context hierarchy
    at org.springframework.context.support.AbstractApplicationContext.getApplicationEventMulticaster(AbstractApplicationContext.java:414) [spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.context.support.ApplicationListenerDetector.postProcessBeforeDestruction(ApplicationListenerDetector.java:97) ~[spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.factory.support.DisposableBeanAdapter.destroy(DisposableBeanAdapter.java:253) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]

My Config class is as follows,

@Configuration
@PropertySource(name="myIntegrationProperties", value ="myIntegration.properties")
public class MyInetgrationApplicationConfig {

    /**
     * Rest template.
     *
     * @return the rest template
     */
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    @Value("${datasource.name}")
    private String dataSourceName;

    @Value("${datasource.driver}")
    private String dataSourceDriver;

    @Value("${datasource.url}")
    private String dataSourceUrl;

    @Value("${datasource.username}")
    private String dataSourceUserName;

    @Value("${datasource.password}")
    private String dataSourcePassword;

    @Bean
    @Primary
    public DataSource dataSource() {
        DataSource dataSource = new DataSource();
        dataSource.setName(dataSourceName);
        dataSource.setUrl(dataSourceUrl);
        dataSource.setDriverClassName(dataSourceDriver);
        dataSource.setUsername(dataSourceUserName);
        dataSource.setPassword(dataSourcePassword);

        return dataSource;
    }
}

My Application class,

@SpringBootApplication(scanBasePackages = { "com.test" })
public class MyInetgrationApplication extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(MyInetgrationApplication.class);
    }

}
3

3 Answers

3
votes

You need to add claapath:

@PropertySource("classpath:myIntegration.properties")

The myIntegration.properties file should be placed under /src/main/resources so that it will be available on the classpath at runtime.

1
votes

Try giving classpath reference to you resource file path, as below:

@PropertySource(value = { "classpath:myIntegration.properties" }, name="myIntegrationProperties")
0
votes

I was not able to get values using @Value("${datasource.name}") so I have tried after autowiring the Environment and then using environment.getProperty("datasource.name") it worked for me. Following is the sample code,

    @Configuration
    @PropertySource("classpath:myIntegration.properties")
    public class MyInetgrationApplicationConfig {

        @Autowired
        private Environment environment;

        @Bean
        @Primary
        public DataSource dataSource() {
            DataSource dataSource = new DataSource();
            dataSource.setName(environment.getProperty("datasource.name"));
            dataSource.setUrl(environment.getProperty("datasource.url"));
            .
            .
            .
            .
            return dataSource;
        }
   }