How to selectively make one class to be the code that should be run in Spring (Spring Boot 2.x)
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
add a comment |
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
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
add a comment |
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
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
spring spring-boot
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
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
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
add a comment |
You can group and move those @Component
classes in multiple @Configuration
classes. In this case you have to manually declare them as @Bean
s (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.
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
add a comment |
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
add a comment |
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
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
answered Nov 25 '18 at 20:13
sofendsofend
343415
343415
add a comment |
add a comment |
You can group and move those @Component
classes in multiple @Configuration
classes. In this case you have to manually declare them as @Bean
s (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.
add a comment |
You can group and move those @Component
classes in multiple @Configuration
classes. In this case you have to manually declare them as @Bean
s (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.
add a comment |
You can group and move those @Component
classes in multiple @Configuration
classes. In this case you have to manually declare them as @Bean
s (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.
You can group and move those @Component
classes in multiple @Configuration
classes. In this case you have to manually declare them as @Bean
s (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.
answered Nov 25 '18 at 21:02
Petru FluerasPetru Flueras
163
163
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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