sldk
sldk

Reputation: 374

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.

Upvotes: 8

Views: 4689

Answers (1)

iamiddy
iamiddy

Reputation: 3073

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

Upvotes: 6

Related Questions