Hi I am learning Spring MVC and I want to know How to load application.properties file dynamically.
I am adding HibernateConfig.java file and AppConfig.java file. I want to load application properties file dynamically using profiles. For Example: dev, test, prod. I have tried to use dynamic name application-{profile}.properties and also tried profile annotation. but not able to understand how they are actually working. I have created a different application.properties files.
This property file contains my DB related data. but I don't know how to set profile and how to load PropertySource based on a profile.
I have set the active profile in my appConfig file. Please help me in understanding how to configure profile and application.properties using spring MVC Java-based configuration. I have searched and found many solutions for XML based configuration but I haven't found any proper answer for java based configuration.
HibernateConfig.java
@Configuration
@EnableTransactionManagement
@ComponentScan({"com.project.configuration"})
@PropertySource(value = {"classpath:application.properties"})
public class HibernateConfiguration {
@Autowired
private Environment env;
@Bean
public LocalSessionFactoryBean sessionFactory(){
return sessionFactory;
}
@Bean
public DataSource dataSource(){
/* loading DB */
return dataSource;
}
@Bean
public Properties hibernateProperties(){
}
}
AppConfig.java
@Override
public void onStartup(ServletContext servletContext) throws ServletException
{
super.onStartup(servletContext);
servletContext.setInitParameter("spring.profiles.active", "dev");
}
I think you cant set this parameter at that time, its already too late. You have to start the app with specified profile (or set it in bootstrap file) . You can pass it as an argument or place it in: application.properities Under key: spring.profiles.active
When you set this to 'dev' it will read main application.properities and then the profile one. More about how to set it: https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-profiles.html
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.