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.
CodePudding user response:
It is not possible in your case. Here you have 2 different cloud instances, which means 2 different JVM and 2 different spring containers.
If you want to instantiate multiple ApplicationContexts
in your application. It will be in a parent-child hierarchy. There will be one root ApplicationContext
& then there will be a few child ApplicationContext
respective to every DispatcherServlet
. Beans global to the application will be defined in the root ApplicationContext
. All the ApplicationContexts
will be managed by only one spring container running on a single JVM.
CodePudding user response:
You cannot directly get the beans from one spring boot app to another.
The ApplicationContext Interface
The Spring IoC container is responsible for managing the objects of an application. It uses dependency injection to achieve inversion of control. The interfaces BeanFactory and ApplicationContext represent the Spring IoC container
The web applications are containerized in spring and you cannot regularly share variables with different applications/containers. You can have two contexts within an application If you have two contexts each will have its own singleton.
Workaround:
You can try to use the same mechanism as you use to communicate between two services. eg. REST API
To get beans from the application context.
@Bean
public CommandLineRunner run(ApplicationContext appContext) {
return args -> {
String[] beans = appContext.getBeanDefinitionNames();
Arrays.stream(beans).sorted().forEach(System.out::println);
};
}