Paul Labis: This is a personal blog where everyone can read my experiences, ideas and thoughts about programming, latest gadgets and other information technology related topics, lifestyle and many other stuff I would like to share to the public.

Embedded Jetty in Spring MVC, IoC/DI

I was trying to configure embedded jetty in a spring application that implements MVC(Model View Controller). I want to utilize single spring applicationContext xml file for IoC(Inversion of Control) and DI(Dependency Injection) on dispatcher servlets.

The goal is to be able to use or reference beans configured/located on already been loaded spring application context. Read and understand my resolution below.

This is what my main method look like:"Application starting");
_applicationContext = new ClassPathXmlApplicationContext(_resourceLocations);

_webServer = new Server();
SelectChannelConnector connector = new SelectChannelConnector();

WebAppContext webAppContext = new WebAppContext(_webServer, "path to web app folder", "/");

GenericWebApplicationContext webApplicationContext = new GenericWebApplicationContext();

webAppContext.getServletContext().setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, webApplicationContext);

ServletHandler servletHandler = new ServletHandler();

ServletHolder servletHolder = new ServletHolder(new DispatcherServlet());

ServletMapping servletMapping = new ServletMapping();


RequestLogHandler logHandler = new RequestLogHandler();
NCSARequestLog ncsaLog = new NCSARequestLog();
_webServer.addHandler(logHandler);"Starting main application context");
_applicationContext.start();"Starting Jetty Server");

try {
} catch (Exception e) {
// TODO Auto-generated catch block
}"Application started");
return null;

 The idea is that DispatcherServlet's context is child context of  GenericWebApplicationContext which in turn is a child of ClassPathXmlApplicationContext. Explicitly invoking setParent() gives you the access to all beans from the web application context.

Hope this helps. Feel free to leave your comment or suggestions.

  • Thank you very much for this example.

    I'm also trying to build a Spring MVC standalone application with embedded jetty. But I basically fail at the point, that the request somehow are not passed to the Spring Framework.

    Jetty runs fine, and I also see that the Controllers from Spring are loaded and mapped.

    I probabbly miss some configuration in the context file.

    Could you give me some more informations (maybe sources) how your project structure looks and whats in your context files?

    Have you also tried this with jetty 7 and Spring 3 ?

    Thank you in advance,