How to selectively make one class to be the code that should be run in Spring (Spring Boot 2.x)












0















We have several classes annotated with the @Component notation in our Spring Boot 2.x project - but we would like to selectively pick only one of these class at runtime.



To further elaborate we created a Uber Jar - which will run on several machines - but each jar should be running a different logic and this logic is dictated by one of this class.



What is the cleanest way to achieve this in Spring boot 2.x? I read something about profiles etc. any cleaner solutions are very much appreciated.










share|improve this question

























  • Possible duplicate of How do I tell Spring Boot which main class to use for the executable jar?

    – sofend
    Nov 25 '18 at 20:06











  • Maybe you're looking for: docs.spring.io/spring-boot/docs/current/maven-plugin/usage.html and then docs.spring.io/spring-boot/docs/current/reference/html/… in the latter take a look at the Launcher Manifest and how it is used in the PropertiesLauncher Once you select that launcher you can then directly specify the Main class to run via the loader.main property.

    – sofend
    Nov 25 '18 at 20:09
















0















We have several classes annotated with the @Component notation in our Spring Boot 2.x project - but we would like to selectively pick only one of these class at runtime.



To further elaborate we created a Uber Jar - which will run on several machines - but each jar should be running a different logic and this logic is dictated by one of this class.



What is the cleanest way to achieve this in Spring boot 2.x? I read something about profiles etc. any cleaner solutions are very much appreciated.










share|improve this question

























  • Possible duplicate of How do I tell Spring Boot which main class to use for the executable jar?

    – sofend
    Nov 25 '18 at 20:06











  • Maybe you're looking for: docs.spring.io/spring-boot/docs/current/maven-plugin/usage.html and then docs.spring.io/spring-boot/docs/current/reference/html/… in the latter take a look at the Launcher Manifest and how it is used in the PropertiesLauncher Once you select that launcher you can then directly specify the Main class to run via the loader.main property.

    – sofend
    Nov 25 '18 at 20:09














0












0








0








We have several classes annotated with the @Component notation in our Spring Boot 2.x project - but we would like to selectively pick only one of these class at runtime.



To further elaborate we created a Uber Jar - which will run on several machines - but each jar should be running a different logic and this logic is dictated by one of this class.



What is the cleanest way to achieve this in Spring boot 2.x? I read something about profiles etc. any cleaner solutions are very much appreciated.










share|improve this question
















We have several classes annotated with the @Component notation in our Spring Boot 2.x project - but we would like to selectively pick only one of these class at runtime.



To further elaborate we created a Uber Jar - which will run on several machines - but each jar should be running a different logic and this logic is dictated by one of this class.



What is the cleanest way to achieve this in Spring boot 2.x? I read something about profiles etc. any cleaner solutions are very much appreciated.







spring spring-boot






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 '18 at 9:09









BDL

14.9k63441




14.9k63441










asked Nov 25 '18 at 19:53









Durga DeepDurga Deep

1171211




1171211













  • Possible duplicate of How do I tell Spring Boot which main class to use for the executable jar?

    – sofend
    Nov 25 '18 at 20:06











  • Maybe you're looking for: docs.spring.io/spring-boot/docs/current/maven-plugin/usage.html and then docs.spring.io/spring-boot/docs/current/reference/html/… in the latter take a look at the Launcher Manifest and how it is used in the PropertiesLauncher Once you select that launcher you can then directly specify the Main class to run via the loader.main property.

    – sofend
    Nov 25 '18 at 20:09



















  • Possible duplicate of How do I tell Spring Boot which main class to use for the executable jar?

    – sofend
    Nov 25 '18 at 20:06











  • Maybe you're looking for: docs.spring.io/spring-boot/docs/current/maven-plugin/usage.html and then docs.spring.io/spring-boot/docs/current/reference/html/… in the latter take a look at the Launcher Manifest and how it is used in the PropertiesLauncher Once you select that launcher you can then directly specify the Main class to run via the loader.main property.

    – sofend
    Nov 25 '18 at 20:09

















Possible duplicate of How do I tell Spring Boot which main class to use for the executable jar?

– sofend
Nov 25 '18 at 20:06





Possible duplicate of How do I tell Spring Boot which main class to use for the executable jar?

– sofend
Nov 25 '18 at 20:06













Maybe you're looking for: docs.spring.io/spring-boot/docs/current/maven-plugin/usage.html and then docs.spring.io/spring-boot/docs/current/reference/html/… in the latter take a look at the Launcher Manifest and how it is used in the PropertiesLauncher Once you select that launcher you can then directly specify the Main class to run via the loader.main property.

– sofend
Nov 25 '18 at 20:09





Maybe you're looking for: docs.spring.io/spring-boot/docs/current/maven-plugin/usage.html and then docs.spring.io/spring-boot/docs/current/reference/html/… in the latter take a look at the Launcher Manifest and how it is used in the PropertiesLauncher Once you select that launcher you can then directly specify the Main class to run via the loader.main property.

– sofend
Nov 25 '18 at 20:09












2 Answers
2






active

oldest

votes


















0














Use the Spring Boot Maven plugin, and configure it to use the PropertiesLauncher, then set the loader.main via the command line. Or, you can specify the whole thing via the command line. Assuming you're using the Maven (or Gradle) Spring Boot plugin to build the jar/war file:



java -cp bootApp.jar -Dloader.main=org.your.package.DemoApplication org.springframework.boot.loader.PropertiesLauncher





share|improve this answer































    0














    You can group and move those @Component classes in multiple @Configuration classes. In this case you have to manually declare them as @Beans (methods). You will define as many @Configuration classes as your machines count.



    Further on, you can enable or disable configuration classes by means of Spring Profiles. Profiles are enabled using spring.profiles.active system property (-Dspring.profiles.active=profile1,profile2).



    If you want to have it clean then you have to do it in 'Dependency Inversion Principle' way: You should define interfaces for your @Components and each @Configuration will declare @Beans of a concrete type. In this way you can use @Autowired dependency injection w/o knowing the concrete implementation.






    share|improve this answer























      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%2f53471302%2fhow-to-selectively-make-one-class-to-be-the-code-that-should-be-run-in-spring-s%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      Use the Spring Boot Maven plugin, and configure it to use the PropertiesLauncher, then set the loader.main via the command line. Or, you can specify the whole thing via the command line. Assuming you're using the Maven (or Gradle) Spring Boot plugin to build the jar/war file:



      java -cp bootApp.jar -Dloader.main=org.your.package.DemoApplication org.springframework.boot.loader.PropertiesLauncher





      share|improve this answer




























        0














        Use the Spring Boot Maven plugin, and configure it to use the PropertiesLauncher, then set the loader.main via the command line. Or, you can specify the whole thing via the command line. Assuming you're using the Maven (or Gradle) Spring Boot plugin to build the jar/war file:



        java -cp bootApp.jar -Dloader.main=org.your.package.DemoApplication org.springframework.boot.loader.PropertiesLauncher





        share|improve this answer


























          0












          0








          0







          Use the Spring Boot Maven plugin, and configure it to use the PropertiesLauncher, then set the loader.main via the command line. Or, you can specify the whole thing via the command line. Assuming you're using the Maven (or Gradle) Spring Boot plugin to build the jar/war file:



          java -cp bootApp.jar -Dloader.main=org.your.package.DemoApplication org.springframework.boot.loader.PropertiesLauncher





          share|improve this answer













          Use the Spring Boot Maven plugin, and configure it to use the PropertiesLauncher, then set the loader.main via the command line. Or, you can specify the whole thing via the command line. Assuming you're using the Maven (or Gradle) Spring Boot plugin to build the jar/war file:



          java -cp bootApp.jar -Dloader.main=org.your.package.DemoApplication org.springframework.boot.loader.PropertiesLauncher






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 25 '18 at 20:13









          sofendsofend

          343415




          343415

























              0














              You can group and move those @Component classes in multiple @Configuration classes. In this case you have to manually declare them as @Beans (methods). You will define as many @Configuration classes as your machines count.



              Further on, you can enable or disable configuration classes by means of Spring Profiles. Profiles are enabled using spring.profiles.active system property (-Dspring.profiles.active=profile1,profile2).



              If you want to have it clean then you have to do it in 'Dependency Inversion Principle' way: You should define interfaces for your @Components and each @Configuration will declare @Beans of a concrete type. In this way you can use @Autowired dependency injection w/o knowing the concrete implementation.






              share|improve this answer




























                0














                You can group and move those @Component classes in multiple @Configuration classes. In this case you have to manually declare them as @Beans (methods). You will define as many @Configuration classes as your machines count.



                Further on, you can enable or disable configuration classes by means of Spring Profiles. Profiles are enabled using spring.profiles.active system property (-Dspring.profiles.active=profile1,profile2).



                If you want to have it clean then you have to do it in 'Dependency Inversion Principle' way: You should define interfaces for your @Components and each @Configuration will declare @Beans of a concrete type. In this way you can use @Autowired dependency injection w/o knowing the concrete implementation.






                share|improve this answer


























                  0












                  0








                  0







                  You can group and move those @Component classes in multiple @Configuration classes. In this case you have to manually declare them as @Beans (methods). You will define as many @Configuration classes as your machines count.



                  Further on, you can enable or disable configuration classes by means of Spring Profiles. Profiles are enabled using spring.profiles.active system property (-Dspring.profiles.active=profile1,profile2).



                  If you want to have it clean then you have to do it in 'Dependency Inversion Principle' way: You should define interfaces for your @Components and each @Configuration will declare @Beans of a concrete type. In this way you can use @Autowired dependency injection w/o knowing the concrete implementation.






                  share|improve this answer













                  You can group and move those @Component classes in multiple @Configuration classes. In this case you have to manually declare them as @Beans (methods). You will define as many @Configuration classes as your machines count.



                  Further on, you can enable or disable configuration classes by means of Spring Profiles. Profiles are enabled using spring.profiles.active system property (-Dspring.profiles.active=profile1,profile2).



                  If you want to have it clean then you have to do it in 'Dependency Inversion Principle' way: You should define interfaces for your @Components and each @Configuration will declare @Beans of a concrete type. In this way you can use @Autowired dependency injection w/o knowing the concrete implementation.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 25 '18 at 21:02









                  Petru FluerasPetru Flueras

                  163




                  163






























                      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%2f53471302%2fhow-to-selectively-make-one-class-to-be-the-code-that-should-be-run-in-spring-s%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

                      404 Error Contact Form 7 ajax form submitting

                      How to know if a Active Directory user can login interactively

                      TypeError: fit_transform() missing 1 required positional argument: 'X'