users@jersey.java.net

Re: [Jersey] SpringServlet support when Spring is not started from a web context?

From: Chris Carrier <ctcarrier_at_gmail.com>
Date: Fri, 14 May 2010 08:42:10 -0700

Why not just set the contextConfigLocation in your bootstrap?

Context root = new Context(server, "/", Context.NO_SESSIONS);

//Spring stuff
root.getInitParams().put("contextConfigLocation",
"classpath:/META-INF/spring/applicationContext.xml");
root.addEventListener(new ContextLoaderListener());
root.addEventListener(new RequestContextListener());

We use embedded Jetty like this and it works fine.

Chris

On Fri, May 14, 2010 at 5:44 AM, Alex Sherwin
<alex.sherwin_at_acadiasoft.com> wrote:
> I just noticed when setting up SpringServlet with an embedded Jetty server
> started by Spring (which was not started by a web context), that the
> SpringServlet will fail to startup, as it makes the assumption that it can
> get the information either from the init-param "contextConfigLocation" or
> the "default" via
> WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
>
> What I'm doing is using Jetty, like so:
>
>      final ServletContextHandler contextHandler = new
> ServletContextHandler(ServletContextHandler.SESSIONS);
>      contextHandler.setContextPath(context);
>      contextHandler.addServlet(new ServletHolder(springServlet), "/*");
>      jettyServer.setHandler(contextHandler);
>      jettyServer.start();
>
> My solution was to extend SpringServlet and override initiate like so:
>
>  @Override
>  protected void initiate(ResourceConfig rc, WebApplication wa) {
>    try {
>      wa.initiate(rc, new SpringComponentProviderFactory(rc,
> applicationContext));
>    } catch (Exception e) {
>      throw new RuntimeException(e);
>    }
>  }
>
> Where my new servlet bean is a Spring Component, which I obtain from the
> Spring ApplicationContext, and as such I inject the "applicationContext"
> variable in the above method override like so:
>
> @Autowired
>  protected ConfigurableApplicationContext applicationContext;
>
> Easy enough solution, however, I feel like Jersey should natively support
> Spring in such a way that doesn't REQUIRE it to be started from a web
> application context
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>
>