Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot sperate @Configurations for multiple application contexts

I'd like to run one spring boot application but have it listen on multiple ports.

The aim is to be able to let an Apache forward multiple (sub-) domains to the spring boot application (Tomcat) on different ports. Example:

         example.com/** -> PORT 8080
  client.example.com/** -> PORT 8090
employee.example.com/** -> PORT 8100

As far as I understood from several threads on SO, I'm best off launching multiple @SpringBootApplication Annotated classes from one main class, right? (https://stackoverflow.com/a/25870132/1510659)

What I didn't grasp yet, is how to configure each one of those applications separately.

Let's say I have launched these three Applications as shown in the linked post above:

MainExampleApplication
ClientExampleApplication
EmployeeExampleApplication

Now, for example, I want to have separate Spring Security @Configuration classes for each of these Applications, as well as @RequestMappings which might have the same value (e.g. "/").

How do I tell the @Configuration or @Controller classes which Application they are assigned to?

Or are there properties that can to be passed to the applications on startup to specify which resources are responsible for the context?

I hope I'm not going in a totally wrong direction here. I do have experience with Spring MVC and have configured some rather simplistic Spring applications - but not with multiple contexts. I'd be really glad if someone could lead me in the right direction. Thank you in advance.

UPDATE

As mentioned in iamiddy's answer and xeon's comment, I used Spring Profiles for that. I provided the SpringApplicationBuilder with a profile for each of my application contexts on startup and used the @Profile("some_profile") on the @Components that should only be available to some of the contexts.

like image 790
sldk Avatar asked Jun 27 '15 11:06

sldk


People also ask

Can we have multiple application context in Spring boot?

We can have multiple application contexts that share a parent-child relationship. A context hierarchy allows multiple child contexts to share beans which reside in the parent context. Each child context can override configuration inherited from the parent context.

How Spring boots handle multiple environments?

Spring Boot allows you to externalize your configuration so you can work with the same application code in different environments. You can use properties files, YAML files, environment variables and command-line arguments to externalize configuration.

Can we have multiple application properties in Spring boot?

Spring Boot supports different properties based on the Spring active profile. For example, we can keep two separate files for development and production to run the Spring Boot application.

Can we have more than one @springbootapplication?

In this example we are having two SpringBoot applications that we want to merge into one and for that we will create a new SpringBoot application for package them both and add the two old applications as maven dependencies. Then we will ask Spring to boot-up the application.


1 Answers

Use Profiles it's a great spring feature, loads only beans associted with the profile. Once that's done start your applications N times with different port and profile arguments

Ex: Here is how you would start the first one, do it for the rest to your N

java -jar -Dspring.profiles.active=production1 -Dserver.port=9000 app.jar

like image 83
iamiddy Avatar answered Oct 13 '22 22:10

iamiddy