springboot指定上下文_Spring Boot sperate @Configurations用于多个应用程序上下文

该博客探讨如何在Spring Boot中运行一个应用程序并使其监听多个端口,以实现通过Apache将不同子域名转发到不同端口上的Spring Boot应用(Tomcat)。作者考虑使用Spring Profiles来区分不同应用程序上下文,并在启动时通过命令行参数指定端口和配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

解决方案

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值