//
//  ========================================================================
//  Copyright (c) 1995-2020 Mort Bay Consulting Pty Ltd and others.
//  ========================================================================
//  All rights reserved. This program and the accompanying materials
//  are made available under the terms of the Eclipse Public License v1.0
//  and Apache License v2.0 which accompanies this distribution.
//
//      The Eclipse Public License is available at
//      http://www.eclipse.org/legal/epl-v10.html
//
//      The Apache License v2.0 is available at
//      http://www.opensource.org/licenses/apache2.0.php
//
//  You may elect to redistribute this code under either of these licenses.
//  ========================================================================
//

[[framework-jetty-spring]]
=== Spring Setup

You can assemble and configure Jetty in code or with almost any IoC style framework including Spring.
If all you want to do is setup a Jetty server in your stock Spring usage, simply look at the xml snippet below as an example.
If you want to replace the jetty-xml being used to start the normal Jetty distribution with spring, you may do so however currently it will not leverage the rest of the module system.

==== Jetty-Spring Module

The skeleton of a jetty spring module can be enabled from the jetty-distribution via the link:#startup-modules[module mechanism].
For example:

[source, screen, subs="{sub-order}"]
....
$ java -jar start.jar --add-to-start=spring
....

This (or the alternative link:#start-jar[--add-to-start]=spring command) creates a `${jetty.home}/lib/spring` directory and populates it with the jetty-spring integration jar.
It does NOT supply the spring jars and their dependencies.
You will need to download these and place them into jetty's classpath - you can use the `${jetty.home}/lib/spring` directory created by spring.mod for this purpose.

==== Using Spring to Configure Jetty

Configuring Jetty via Spring is simply a matter of calling the API as Spring beans.
The following is an example mimicking the default jetty startup configuration.

[source, xml, subs="{sub-order}"]
----

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<!-- =============================================================== -->
<!-- Configure the Jetty Server with Spring                          -->
<!-- This file is the similar to jetty.xml, but written in spring    -->
<!-- XmlBeanFactory format.                                          -->
<!-- =============================================================== -->

<beans>
    <bean id="contexts" class="org.eclipse.jetty.server.handler.ContextHandlerCollection"/>
    <bean id="server" name="Main" class="org.eclipse.jetty.server.Server" init-method="start" destroy-method="stop">
        <constructor-arg>
            <bean id="threadPool" class="org.eclipse.jetty.util.thread.QueuedThreadPool">
                <property name="minThreads" value="10"/>
                <property name="maxThreads" value="50"/>
            </bean>
        </constructor-arg>
        <property name="connectors">
            <list>
                <bean id="connector" class="org.eclipse.jetty.server.ServerConnector">
                    <constructor-arg ref="server"/>
                    <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="deploymentManager" class="org.eclipse.jetty.deploy.DeploymentManager">
                    <property name="contexts" ref="contexts"/>
                    <property name="appProviders">
                        <list>
                            <bean id="webAppProvider" class="org.eclipse.jetty.deploy.providers.WebAppProvider">
                                <property name="monitoredDirName" value="webapps"/>
                                <property name="scanInterval" value="1"/>
                                <property name="extractWars" value="true"/>
                            </bean>
                        </list>
                    </property>
                </bean>
            </list>
        </property>
    </bean>
</beans>


----
