Reputation: 31
Does anyone have a working sample of the following:
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!
Upvotes: 3
Views: 1959
Reputation: 3542
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
Upvotes: 0
Reputation: 859
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>
Upvotes: 1