3

Does anyone have a working sample of the following:

  • Embedded Jetty 8.x app
  • Using Spring MVC
  • Zero XML configuration (i.e. using a Spring WebApplicationInitializer on the Servlet side and annotations/java configuration on the Spring side)

I have tried every possible combination, but I can't get this to work. Most embedded jetty examples I found are based on 7.x or still use XML configuration files. The best setup I got now is to create a WebAppContext and set the configuration to AnnotationConfiguration. This shows on the console that something is actually happening, but it cannot find my WebApplicationInitializer class while it definitely is on the classpath. This was based on Jetty 8.1.4 and Spring 3.1.2.

For testing purposes, the WebApplicationInitializer class doesn't do much, it only prints something in the onStartup method to check if this is being loaded.

Thanks!

Paul
  • 31
  • 3
  • I just tried with Eclipse Jetty WTP and it found my WebApplicationInitializer without issues. Are you using Eclipse too? – Luciano Oct 20 '12 at 01:02
  • Maybe this is of help http://stackoverflow.com/questions/13222071/spring-3-1-webapplicationinitializer-embedded-jetty-8-annotationconfiguration/13308996#13308996 – Stevie Nov 09 '12 at 13:33
  • Here's a helpful blog post on the subject: http://kielczewski.eu/2013/11/using-embedded-jetty-spring-mvc/ – enocom Aug 01 '15 at 21:08

2 Answers2

1

Did you see this question : Spring 3.1 WebApplicationInitializer & Embedded Jetty 8 AnnotationConfiguration ?

I can't share my code but here is some code that may help you :

Web.xml

<!-- Java-based Spring container definition -->
<context-param>
    <param-name>contextClass</param-name>
    <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</context-param>

<!-- Location of Java @Configuration classes that configure the components that makeup this application -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>com.workable.config</param-value>
</context-param>

Empty application-config.xml :

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://www.springframework.org/schema/beans    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
</beans>

Spring WebMVCConfig :

/**
 * Spring MVC Configuration.
 *
 */
@Configuration
@EnableWebMvc
@EnableAsync
@EnableScheduling
public class WebMvcConfig extends WebMvcConfigurerAdapter {
}

/**
 * Main configuration class for the application.
 * Turns on @Component scanning, loads externalized application.properties.
 */
@Configuration
@ComponentScan(basePackages = {
    "com.workable"
}, excludeFilters = {@Filter(Configuration.class)})
public class MainConfig {
    ...
}

Lib versions :

<spring.version>3.1.2.RELEASE</spring.version>
<jetty.version>8.1.5.v20120716</jetty.version>
Community
  • 1
  • 1
  • I didn't actually see the linked post, I kind of gave up some time ago, but I'll give it a try with your feedback and the additional info from the other post (which was posted a few months after my question). – Paul Jan 24 '13 at 11:57
0

Here is a thing which worked for me on jetty 9.

    Server server = new Server(8080);

    WebAppContext webAppContext = new WebAppContext();
    webAppContext.setResourceBase("src/main/webapp");
    webAppContext.setContextPath("/");
    webAppContext.setConfigurations(new Configuration[] {
            new WebXmlConfiguration(),
            new AnnotationConfiguration() {
                @Override
                public void preConfigure(WebAppContext context) {
                    ClassInheritanceMap map = new ClassInheritanceMap();
                    map.put(WebApplicationInitializer.class.getName(), 
                            new ConcurrentHashSet<String>() {{add(WebApplication.class.getName());}});
                    context.setAttribute(CLASS_INHERITANCE_MAP, map);
                    _classInheritanceHandler = new ClassInheritanceHandler(map);
                }
            }
        });
    server.setHandler(webAppContext);
    server.start();

For jetty 8 this may work:

webAppContext.setConfigurations(new Configuration[] {
    new WebXmlConfiguration(),
    new AnnotationConfiguration() {
        @Override
        public void preConfigure(WebAppContext context) throws Exception {
            MultiMap<String> map = new MultiMap<String>();
            map.add(WebApplicationInitializer.class.getName(), MyWebApplicationInitializerImpl.class.getName());
            context.setAttribute(CLASS_INHERITANCE_MAP, map);
            _classInheritanceHandler = new ClassInheritanceHandler(map);
        }
    }
});

MultiMap is the one form org.eclipse.jetty.util package

Vadim Kirilchuk
  • 3,532
  • 4
  • 32
  • 49