I use Spring MVC with java config.
And I have two context configuration classes: RootContext and ServletContext.
RootContext class is loaded via <context-param> in web.xml
Here is the code of RootContext:
@Configuration
@EnableTransactionManagement
@Import(DaoConfig.class)
@PropertySource("/WEB-INF/config/application.properties")
public class RootContext {
@Autowired
private Environment env;
@Bean(destroyMethod = "close")
public DataSource dataSource() {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName(env.getProperty("jdbc.driverClassName"));
dataSource.setUrl(env.getProperty("jdbc.url"));
dataSource.setUsername(env.getProperty("jdbc.username"));
dataSource.setPassword(env.getProperty("jdbc.password"));
return dataSource;
}
@Bean
public PlatformTransactionManager transactionManager() {
return new DataSourceTransactionManager(dataSource());
}
}
If I run the application, I got this error:
java.io.FileNotFoundException: class path resource [WEB-INF/config/application.properties] cannot be opened because it does not exist
Everything works fine if I move application.properties file to classpath. But I want it to be in /WEB-INF/config directory.
Any suggestion how to solve this error?
Should I put @PropertySource("/WEB-INF/config/application.properties") line to ServletContext instead of RootContext??
Thank you.
I know this is a late reply but I hope this may be useful for anyone having the same issue.
You can do this by setting context-param in your web.xml and access it in configuration class with @PropertyResource.
web.xml
AppConfig.class