开发者

Can configure and start embedded Tomcat via Spring? OK with Jetty?

Is there a way to configure and setup Embedded Tomcat in Spring? I can do so easily with Jetty 7 that I 开发者_高级运维created a standalone Java application that will start Jetty as webcontainer and finally JUnit test can call the BO via HTTPInvoker.

To me, it seems I have to write code to do so by using Tomcat?

Spring xml file

<!-- Manually start server after setting parent context. (init-method="start") -->
<bean id="jettyServer"
      class="org.eclipse.jetty.server.Server"
      init-method="start"
      destroy-method="stop">
    <property name="threadPool">
        <bean id="ThreadPool"
              class="org.eclipse.jetty.util.thread.ExecutorThreadPool">
            <constructor-arg value="0" />
            <!--property name="corePoolSize" value="${jetty.server.thread.pool.core.pool.size}"/>
            <property name="maximumPoolSize" value="${jetty.server.thread.pool.max.pool.size}"/-->
        </bean>
    </property>
    <property name="connectors">
        <list>
            <bean id="Connector"
                  class="org.eclipse.jetty.server.nio.SelectChannelConnector"
                  p:port="${jetty.server.port}"
                  p:maxIdleTime="${jetty.server.max.idle.time}"
                  p:acceptors="${jetty.server.acceptor.num}"
                  p:confidentialPort="${jetty.server.ssl.port}" />
        </list>
    </property>

    <property name="handler">
        <bean class="org.eclipse.jetty.server.handler.HandlerCollection">
            <property name="handlers">
                <list>
                   <bean class="org.eclipse.jetty.servlet.ServletContextHandler">
                      <property name="contextPath" value="/"/>
                      <property name="sessionHandler">
                        <bean class="org.eclipse.jetty.server.session.SessionHandler"/>
                      </property>
                      <property name="resourceBase" value="."/>
                      <property name="servletHandler">
                        <bean class="org.eclipse.jetty.servlet.ServletHandler">
                          <property name="servlets"> <!-- servlet definition -->
                            <list>
                            <!-- default servlet -->
                            <bean class="org.eclipse.jetty.servlet.ServletHolder">
                              <property name="name" value="DefaultServlet"/>
                              <property name="servlet">                                    
                                  <bean class="org.springframework.web.servlet.DispatcherServlet"/>
                              </property>
                              <property name="initParameters">
                                <map>                                        
                                    <entry key="contextConfigLocation" value="classpath:config/DefaultServlet-servlet.xml" />
                                </map>
                              </property>
                            </bean>
                            </list>
                          </property>
                          <property name="servletMappings">
                            <list><!-- servlet mapping -->
                            <bean class="org.eclipse.jetty.servlet.ServletMapping">
                              <property name="pathSpecs">
                                <list><value>/</value></list>
                              </property>
                              <property name="servletName" value="DefaultServlet"/>
                            </bean>
                            </list>
                          </property>
                        </bean>
                      </property>
                    </bean>

                    <bean class="org.eclipse.jetty.server.handler.RequestLogHandler">
                        <property name="requestLog">
                            <bean class="org.eclipse.jetty.server.NCSARequestLog">
                                <constructor-arg value="${jetty.server.log.dir}/jetty-yyyy_mm_dd.log"/>
                                <property name="extended" value="false"/>
                            </bean>
                        </property>
                    </bean>
                </list>
            </property>
        </bean>
    </property>
</bean>

DefaultServlet-servlet.xml

<!-- This default handler takes care of each of the services enumerated below -->
    <bean id="defaultHandlerMapping"
            class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />

    <bean id="helloService" class="com.company.ws.bo.HelloServiceImpl"/>

    <!-- SpringHTTP Service Exposure -->

    <bean name="/HelloService"
            class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter"
            lazy-init="true">
            <property name="service" ref="helloService" />
            <property name="serviceInterface"
                    value="com.company.ws.bo.IHelloService" />
    </bean>


Tomcat 7 can be used as an embedded Server. As far as I know there is no special spring support, but you don't need special spring support to start an tomcat out of an spring application.

@See: this blog and this (german)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜