Discussion:
SpringServlet support when Spring is not started from a web context?
Alex Sherwin
2010-05-14 12:44:20 UTC
Permalink
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
Chris Carrier
2010-05-14 15:42:10 UTC
Permalink
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
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());
     final ServletContextHandler contextHandler = new
ServletContextHandler(ServletContextHandler.SESSIONS);
     contextHandler.setContextPath(context);
     contextHandler.addServlet(new ServletHolder(springServlet), "/*");
     jettyServer.setHandler(contextHandler);
     jettyServer.start();
 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"
@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
---------------------------------------------------------------------
Alex Sherwin
2010-05-14 16:28:22 UTC
Permalink
I don't want the SpringServlet to start the ApplicationCotnext, I have
an ApplicationContext already which is starting the Jetty server, which
has the SpringServer in it, which I want to be part of the already
existing ApplicationContext...
Post by Chris Carrier
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
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());
final ServletContextHandler contextHandler = new
ServletContextHandler(ServletContextHandler.SESSIONS);
contextHandler.setContextPath(context);
contextHandler.addServlet(new ServletHolder(springServlet), "/*");
jettyServer.setHandler(contextHandler);
jettyServer.start();
@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"
@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
---------------------------------------------------------------------
---------------------------------------------------------------------
Paul Sandoz
2010-05-19 15:35:24 UTC
Permalink
Hi Alex,

It would be better to override SpringServlet.getContext

How about we add a constructor to SpringServlet that passes the
application context?

Can you log an issue?

Paul.
Post by Alex Sherwin
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());
final ServletContextHandler contextHandler = new
ServletContextHandler(ServletContextHandler.SESSIONS);
contextHandler.setContextPath(context);
contextHandler.addServlet(new ServletHolder(springServlet), "/
*");
jettyServer.setHandler(contextHandler);
jettyServer.start();
@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
@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
---------------------------------------------------------------------
chauzer
2010-12-02 00:24:47 UTC
Permalink
I'm running into the exact same issue here:

I have a spring applicationContext.xml which has configurations for an
embedded jetty instance, and im trying to pass along the spring-jersey
servlet into it. I did the same thing as you and overwrote the initiate
method from SpringServlet and passed in the applicationContext that I
already have. However, in my jersey resource classes, i want the scope to be
request (rather than singletons), but i'm receiving an error for that and i
did some research
(http://joannaandjohn.blogspot.com/2010/02/testing-request-and-session-scoped.html)
and it looks like im using the regular applicationContext instead of
webapplicationcontext?

Any ideas on this?

Thanks
--
View this message in context: http://jersey.576304.n2.nabble.com/SpringServlet-support-when-Spring-is-not-started-from-a-web-context-tp5050797p5794189.html
Sent from the Jersey mailing list archive at Nabble.com.
Paul Sandoz
2010-12-02 11:55:25 UTC
Permalink
Hi Edwin,

Did you manage to resolve your previous issue you emailed about with
the ServletContext being null ?

Could you please send a maven project that reproduces the issue then i
can investigate.

From the link you sent it looks you will also need to set explicitly
set the ApplicationContext for the SpringServlet as discussed on the
other related email thread. One way to override the setting of the
context is to do:


public MySpringServlet extends SpringServlet {
@Override
protected ConfigurableApplicationContext getContext() { ... }

}

But i agree we require a more Spring aware way of doing this without
having to modify the Jersey code e.g. by setting a property on the
SpringServlet.

Paul.
Post by chauzer
I have a spring applicationContext.xml which has configurations for an
embedded jetty instance, and im trying to pass along the spring-jersey
servlet into it. I did the same thing as you and overwrote the
initiate
method from SpringServlet and passed in the applicationContext that I
already have. However, in my jersey resource classes, i want the scope to be
request (rather than singletons), but i'm receiving an error for that and i
did some research
(http://joannaandjohn.blogspot.com/2010/02/testing-request-and-session-scoped.html
)
and it looks like im using the regular applicationContext instead of
webapplicationcontext?
Any ideas on this?
Thanks
--
View this message in context: http://jersey.576304.n2.nabble.com/SpringServlet-support-when-Spring-is-not-started-from-a-web-context-tp5050797p5794189.html
Sent from the Jersey mailing list archive at Nabble.com.
Loading...