package com.sample.config;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.servlet.ServletHandler;
import org.mortbay.jetty.webapp.WebAppContext;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.GenericWebApplicationContext;
public class ServerConfigurer
extends Server
implements ApplicationContextAware
{
private String _webAppDir = null;
private String _contextPath = null;
private ServletHandler _servletHandler = null;
private static ApplicationContext _applicationContext = null;
public String getContextPath() {
return _contextPath;
}
public ServletHandler getServletHandler() {
return _servletHandler;
}
public String getWebAppDir() {
return _webAppDir;
}
/**
* @see org.springframework.context.ApplicationContextAware#setApplicationContext(org.springframework.context.ApplicationContext)
*/
@Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException
{
_applicationContext = applicationContext;
}
public void setContextPath(String contextPath) {
_contextPath = contextPath;
}
public void setServletHandler(ServletHandler servletHandler) {
_servletHandler = servletHandler;
}
public void setWebAppDir(String webAppDir) {
_webAppDir = webAppDir;
}
@Override
protected void doStart()
throws Exception
{
final WebAppContext webAppContext = new WebAppContext(getServer(), _webAppDir, _contextPath);
final GenericWebApplicationContext webApplicationContext = new GenericWebApplicationContext();
webApplicationContext.setServletContext(webAppContext.getServletContext());
webApplicationContext.setParent(_applicationContext);
webAppContext.getServletContext().setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, webApplicationContext);
webApplicationContext.refresh();
webAppContext.setServletHandler(_servletHandler);
addHandler(webAppContext);
super.doStart();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<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-2.0.xsd" >
<!-- more beans configured -->
<bean name="webServer" class="point to your server configurer java file" init-method="start">
<property name="connectors">
<list>
<bean class="org.mortbay.jetty.nio.SelectChannelConnector">
<property name="host" value="${jetty.host}"/>
<property name="port" value="${jetty.port}"/>
</bean>
</list>
</property>
<property name="webAppDir" value="${jetty.webApp.dir}"/>
<property name="contextPath" value="${jetty.context.Path}"/>
<property name="servletHandler">
<bean class="org.mortbay.jetty.servlet.ServletHandler">
<property name="servlets">
<list>
<bean class="org.mortbay.jetty.servlet.ServletHolder">
<property name="name" value="dispatcher" />
<property name="servlet">
<bean class="org.springframework.web.servlet.DispatcherServlet" />
</property>
</bean>
</list>
</property>
<property name="servletMappings">
<list>
<bean class="org.mortbay.jetty.servlet.ServletMapping">
<property name="servletName" value="dispatcher" />
<property name="pathSpec" value="*.htm" />
</bean>
</list>
</property>
</bean>
</property>
<property name="handlers">
<list>
<!-- log handler -->
<bean class="org.mortbay.jetty.handler.RequestLogHandler">
<property name="requestLog">
<bean class="org.mortbay.jetty.NCSARequestLog">
<property name="append" value="true"/>
<property name="filename" value="${http.log.dir}/access.log.yyyy_mm_dd"/>
<property name="extended" value="true"/>
<property name="retainDays" value="999"/>
<property name="filenameDateFormat" value="yyyy-MM-dd"/>
</bean>
</property>
</bean>
</list>
</property>
</bean>
</beans>
example:
_applicationContext = new ClassPathXmlApplicationContext(_resourceLocations);
_applicationContext.start();
I also did another article related to Spring MVC/IOC-DI and Embedded Jetty utilizing applicationContext .xml of Spring. You might be interested running and configuring jetty through java code and not from applicationContext.xml.