I have a Java SE application running an embedded Jetty web server hosting a RESTful web service. I want to use Spring in both the Java SE part and in the web application running in Jetty. Some beans, e.g. a dao and the entity manager, needs to be shared between the two parts. When I start the application I get duplicate versions of both the beans and the entity manager.
WARN EntityManagerFactoryRegistry.addEntityManagerFactory:80 - HHH000436: Entity manager factory name (persistenceUnit) is already registered. If entity manager will be clustered or passivated, specify a unique value for property 'hibernate.ejb.entitymanager_factory_name'
In the Java SE part the first thing I do is to create the application context.
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:server-context.xml","classpath:application-context.xml");
In the web application Spring is configured in web.xml:
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
<init-param>
<param-name>com.sun.jersey.spi.container.ResourceFilters</param-name>
<param-value>example.restful.server.filter.SecurityFilterFactory</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.config.property.resourceConfigClass</param-name>
<param-value>com.sun.jersey.api.core.PackagesResourceConfig</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>example.restful.rest</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:application-context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
How can I make the web application use the application context already created instead of duplicating all the beans?