Old fashioned way:
Since you have already initialized ContextLoaderListener simple trick is to use WebApplicationContext to get your bean components anywhere in the application:
WebApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext); SomeBean someBean = (SomeBean) ctx.getBean("someBean");
Jersey Support:
Or you can use annotation-based discovery, as Jersey already supports Spring DI . You must register your beans under your main application entry point. This entry point in the example below will be some.package.MyApplication , to be provided as the <init-param> servlet container:
<servlet> <servlet-name>SpringApplication</servlet-name> <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> <init-param> <param-name>javax.ws.rs.Application</param-name> <param-value>some.package.MyApplication</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>
Register your beans in your application:
package some.package; import org.glassfish.jersey.server.ResourceConfig; import org.glassfish.jersey.server.spring.scope.RequestContextFilter; public class MyApplication extends ResourceConfig { public MyApplication () { register(RequestContextFilter.class); register(SomeBean.class);
Here you can see the finished example from the Jersey Git repository.
tmarwen
source share