title: How to load Application.properties file dynamically based on Profile in Spring MVC?
body: <p>Hi I am learning Spring MVC and I want to know How to load application.properties file dynamically.</p>
  
  <p>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 <strong>application-{profile}.properties</strong> and also tried profile annotation. but not able to understand how they are actually working. I have created a different application.properties files.</p>
  
  <ol>
  <li>application-dev</li>
  <li>application-test</li>
  <li>application-prod</li>
  </ol>
  
  <p>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.<br>
  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.</p>
  
  <blockquote>
    <p>HibernateConfig.java</p>
  </blockquote>
  
  <pre><code>@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(){
  }
  }
  </code></pre>
  
  <blockquote>
    <p>AppConfig.java</p>
  </blockquote>
  
  <pre><code>@Override
  public void onStartup(ServletContext servletContext) throws ServletException 
  {
      super.onStartup(servletContext);
      servletContext.setInitParameter("spring.profiles.active", "dev");
  }
  </code></pre>