How to load Application.properties file dynamically based on Profile in Spring MVC?










2















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.



  1. application-dev

  2. application-test

  3. application-prod

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");










share|improve this question


























    2















    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.



    1. application-dev

    2. application-test

    3. application-prod

    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");










    share|improve this question
























      2












      2








      2








      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.



      1. application-dev

      2. application-test

      3. application-prod

      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");










      share|improve this question














      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.



      1. application-dev

      2. application-test

      3. application-prod

      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");







      java spring spring-mvc






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 14 '18 at 18:46









      Aks 1316Aks 1316

      477




      477






















          1 Answer
          1






          active

          oldest

          votes


















          0














          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






          share|improve this answer























          • I have tried to put spring.profiles.active in application.properties. But It is giving me an error and I am not sure it will work in Spring MVC. spring.profiles.active is for spring boot only.

            – Aks 1316
            Nov 16 '18 at 1:41











          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53306872%2fhow-to-load-application-properties-file-dynamically-based-on-profile-in-spring-m%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          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






          share|improve this answer























          • I have tried to put spring.profiles.active in application.properties. But It is giving me an error and I am not sure it will work in Spring MVC. spring.profiles.active is for spring boot only.

            – Aks 1316
            Nov 16 '18 at 1:41
















          0














          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






          share|improve this answer























          • I have tried to put spring.profiles.active in application.properties. But It is giving me an error and I am not sure it will work in Spring MVC. spring.profiles.active is for spring boot only.

            – Aks 1316
            Nov 16 '18 at 1:41














          0












          0








          0







          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






          share|improve this answer













          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







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 15 '18 at 15:01









          marekkmarekk

          737




          737












          • I have tried to put spring.profiles.active in application.properties. But It is giving me an error and I am not sure it will work in Spring MVC. spring.profiles.active is for spring boot only.

            – Aks 1316
            Nov 16 '18 at 1:41


















          • I have tried to put spring.profiles.active in application.properties. But It is giving me an error and I am not sure it will work in Spring MVC. spring.profiles.active is for spring boot only.

            – Aks 1316
            Nov 16 '18 at 1:41

















          I have tried to put spring.profiles.active in application.properties. But It is giving me an error and I am not sure it will work in Spring MVC. spring.profiles.active is for spring boot only.

          – Aks 1316
          Nov 16 '18 at 1:41






          I have tried to put spring.profiles.active in application.properties. But It is giving me an error and I am not sure it will work in Spring MVC. spring.profiles.active is for spring boot only.

          – Aks 1316
          Nov 16 '18 at 1:41




















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53306872%2fhow-to-load-application-properties-file-dynamically-based-on-profile-in-spring-m%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Top Tejano songwriter Luis Silva dead of heart attack at 64

          ReactJS Fetched API data displays live - need Data displayed static

          Evgeni Malkin