Embedded Jetty 8.x / Spring MVC / WebApplicationInitializer

Does anyone have a working example from the following:

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

I tried all possible combinations, but I can't get this to work. Most of the built-in berth examples that I have found are based on 7.x or still use XML configuration files. The best setup I got now is to create a WebAppContext and configure it on AnnotationConfiguration. This shows on the console that something is actually happening, but it cannot find my WebApplicationInitializer class, although it is definitely in the classpath. It was based on Jetty 8.1.4 and Spring 3.1.2.

For testing purposes, the WebApplicationInitializer class does not do much, it only prints something in the onStartup method to check if it loads.

Thanks!

+4
source share
2 answers

Have you seen this question: Spring 3.1 WebApplicationInitializer and Embedded Jetty 8 AnnotationConfiguration ?

I can’t share my code, but here is a code that can 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 app-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 version:

 <spring.version>3.1.2.RELEASE</spring.version> <jetty.version>8.1.5.v20120716</jetty.version> 
+1
source

Here is the thing that worked for me on pier 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 berth 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 one form of the org.eclipse.jetty.util package

0
source

All Articles