I have my main Spring context which is created when my application is started. Within this context is the database connection and the embedded web server.
The embedded webserver is then started with a DispatcherServlet
with its own Spring Context.
From one of the DispatcherServlets, I wish to access the database, but because the connection is not in its context, I can't.
What is the Java/Spring way to solve this problem?
This is my web.xml:
<servlet>
<servlet-name>App</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/jettycontext.xml</param-value>
</init-param>
</servlet>
This is the entrypoint main method:
try (ConfigurableApplicationContext context = new GenericXmlApplicationContext("maincontext.xml")) {
JServer server = context.getBean(JServer.class);
server.start();
}
This is the JServer.start() method:
server = new Server(8080);
server.setHandler(new WebAppContext("./webapp", "/"));
server.start();
server.join();