Jump to: navigation, search

Difference between revisions of "Jetty/Howto/Spring"

< Jetty‎ | Howto
m
Line 5: Line 5:
 
== Downloading the Jetty-Spring module ==  
 
== Downloading the Jetty-Spring module ==  
  
The jetty-spring module is included in jetty-hightide,  available at Codehaus, and also as a Maven artefact. This example illustrates a Maven download:
+
The jetty-spring module is included in jetty-hightide,  available at Codehaus, and also as a Maven artifact. This example illustrates a Maven download:
  
 
<source lang="java">
 
<source lang="java">

Revision as of 06:50, 8 August 2012



Introduction

You can assemble and configure Jetty in code or with almost any IoC style framework including Spring.

Downloading the Jetty-Spring module

The jetty-spring module is included in jetty-hightide, available at Codehaus, and also as a Maven artifact. This example illustrates a Maven download:

wget --user-agent=other http://repo2.maven.org/maven2/org/mortbay/jetty/jetty-hightide/7.4.0.v20110414/jetty-hightide-7.4.0.v20110414.tar.gz
tar xfz jetty-hightide-7.4.0.v20110414.tar.gz
jetty-hightide-7.4.0.v20110414/


Dependencies

As distributed, the jetty-hightide bundle does not include the spring dependencies:

  • spring-1.2.8.jar
  • commons-logging-1.0.4.jar

You should download and install these dependencies in $JETTY_HOME/lib/spring

cd lib/spring
wget --user-agent=other http://repo2.maven.org/maven2/org/springframework/spring/2.5.6/spring-2.5.6.jar
wget --user-agent=other http://repo2.maven.org/maven2/commons-logging/commons-logging/1.1.1/commons-logging-1.1.1.jar
cd ../..

Using Spring to Configure Jetty

Configuring Jetty via Spring is simply a matter of calling the API as Spring beans. The following is the default jetty-spring.xml file:

<beans>
 
  <bean id="contexts" class="org.eclipse.jetty.server.handler.ContextHandlerCollection"/>
 
  <bean id="Server" class="org.mortbay.jetty.spring.Server" init-method="start" destroy-method="stop">
 
    <property name="threadPool">  
      <bean id="ThreadPool" class="org.eclipse.jetty.util.thread.QueuedThreadPool">
        <property name="minThreads" value="10"/>
        <property name="maxThreads" value="50"/>
      </bean>
    </property>
 
    <property name="connectors">
      <list>
        <bean id="Connector" class="org.eclipse.jetty.server.nio.SelectChannelConnector">
          <property name="port" value="8080"/>
        </bean>
      </list>
    </property>
 
    <property name="handler">
      <bean id="handlers" class="org.eclipse.jetty.server.handler.HandlerCollection">
        <property name="handlers">
          <list>
	     <ref bean="contexts"/>
             <bean id="defaultHandler" class="org.eclipse.jetty.server.handler.DefaultHandler"/>
          </list>
        </property>
      </bean>
    </property>
 
    <property name="beans">
      <list>
        <bean id="ContextDeployer" class="org.mortbay.jetty.spring.ContextDeployer">
          <property name="contexts" ref="contexts"/>
          <property name="contextsDir" value="contexts"/>
          <property name="scanInterval" value="5"/>
        </bean>
 
        <bean id="WebAppDeployer" class="org.eclipse.jetty.deploy.WebAppDeployer">
          <property name="contexts" ref="contexts"/>
          <property name="webAppDir" value="webapps"/>
          <property name="extract" value="true"/>
          <property name="defaultsDescriptor" value="etc/webdefault.xml"/>
        </bean>
 
        <bean class="org.eclipse.jetty.security.HashLoginService">
          <property name="name" value="Test Realm"/>
          <property name="config" value="etc/realm.properties"/>
          <property name="refreshInterval" value="0"/>
        </bean>
 
      </list>
    </property>
 
  </bean>
 
</beans>

Running Jetty with Spring

There are many ways to launch Spring, including the Jetty start.jar mechanism. The following command line starts the Jetty Spring config file including with the jetty-hightide distribution from codehaus:

  java -jar start.jar OPTIONS=Server,All,spring \
    start.class=org.mortbay.jetty.spring.Main \
    etc/jetty-spring.xml

This uses the jetty-spring Main class to load the Spring configuration file and join the resulting server.