java – How do I dynamically load the Application.properties file based on the profile in Spring MVC?

Hello, I'm learning Spring MVC and want to know how to dynamically load the application.properties file.

I add the file HibernateConfig.java and the file AppConfig.java. I want to dynamically load the application properties file by using profiles. For example: dev, test, prod. I tried to use dynamic name application {profile} .properties and also tried profile comments. but unable to understand how they actually work. I have created another application.properties file.

  1. application-dev
  2. application test
  3. Application Prod

This property file contains my DB-related data. However, I do not know how to set a profile and how to load PropertySource based on a profile.
I have set the active profile in my appConfig file. Please help me understand how to configure profiles and application.properties using Spring MVC Java-based configuration. I have searched and found many solutions for the XML-based configuration, but found no suitable answer for the Java-based configuration.

HibernateConfig.java

@Construction
@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 () {
/ * Load DB * /
Return dataSource;
}

@Bean
public properties hibernateProperties () {
}
}

AppConfig.java

@Run over
public void onStartup (ServletContext servletContext) raises ServletException
{
super.onStartup (servletContext);
servletContext.setInitParameter ("spring.profiles.active", "dev");
}