Search code examples
javaspringmavenspring-integration

java spring: how to access Application Context of another Spring Application


I have two separate spring boot(Maven) application ORAApp1 & CASApp1. I have both app running on separate cloud instance. In my CASApp1 i need to get beans from application context of ORAApp1.

Want to know if this is possible or not and if so, how it can be done.

Thanks, Atul.


Solution

  • Spring boot application doesn't expose beans to be consumed by other applications, so you can't really do that. Spring boot application by design is "self-contained" and beans are not "exposed services" but internal building blocks of the application that implement the business logic of your app.

    This is a design of all spring applications.

    In general, Spring is something that "lives" inside the JVM however you run two different JVM processes so you'll have to do some kind of inter-process communication between them.

    Also, it's a good idea to "control" what functions are exposed to be used by other applications and how (protocol, parameters, results, etc).

    Luckily spring boot application allows to easily establish such control by using controllers of spring-MVC. So basically you mark a bean with a couple of annotations (bean = a class managed by spring and allows injection of other spring beans) and it gets exposed (by some URL) automatically. This is the closest "spring-ish" way to achieve what you want.

    Of course, if you need a GRPC style of inter-process communication you definitely can create GRPC services.

    There are also other ways that can be used in different use cases (for example for management you might opt for JMX) or some kind of message bus for asynchronous communication, but this is a topic too broad to be discussed in one question. Bottom line, this is not "automatic" and you have to choose the style of IPC that works for your case, but definitely, you need to have something and you can't just "call" the beans as long as you have two different applications that run on two different JVMs (and it doesn't really matter whether they're running on the same host or not).