Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

563
Views
How to register beans that can be shared by multiple WARs (Spring boot + Tomcat)

I am developing an application with Spring boot and deploying multiple WARs to an external tomcat. I want to register a shareable bean with these WARs when tomcat starts.

I use: Java8, Spring boot(2.5.5) and Tomcat9

Bean I want to be shared by Apps:

public class TestData {
    private String username;
    
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
}

SampleApp(There are multiple apps that Autowire an instance of TestData in the same way):

@SpringBootApplication
@RestController
public class App extends SpringBootServletInitializer {
    @Autowired
    TestData testData;
    
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(App.class);
    }

    @RequestMapping(value = "SampleApp1", method = RequestMethod.GET)
    public String getUsername() {
        return testData.getUsername();
    }
}
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

I think your question is about sharing the data of one singleton Bean between different application containers deployed in the same webserver.

This is not possible for security reasons. Each deployed application has its own classloader and hence cannot easily access beans residing in the memory of the other containers.

I suggest you add a microservice that is accessed by the different deployed applications and use that instead. It can easily be accessed via e.g OpenFeign as a client and almost looks like a bean with getters and setters if done right.

over 4 years ago · Santiago Trujillo Report

0

There are several approaches to your problem, solving it on different levels.

  1. Create a shared library with required stuff and install it to Tomcat server. "Install" normally means to put assemled jars to $CATALINA_HOME/lib directory or similar. Class Loader HOW-TO can give you more details. Not sure how it works with shared instances of classes, but it's definitely possible to share classes (factories, for example) and configurations through shared library. With shared configuration and factories you can at least produce identical beans (not necessary the same, but it seems enough for you case).

  2. Define a JNDI resource shared between all applications. Of course, this requires your class to be accessible by Tomcat, so you first have to go through option 1. This way you can have "published" instance of a class shared between all applications. Details can be found at JNDI Resources HOW-TO. With this approach you can inject published resource with @Resource annotation.

  3. Create a microservice (as one of other answers suggests) that can provide you data and features and access it from your application. This gives you maximum flexibility but also brings complexity requiring actual communication to the microservice.

Obviously, options 1 and 2 require direct access to the server configuration.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!