如何配置嵌入式Jetty来获取Web片段?

时间:2014-09-12 08:42:01

标签: java xml spring configuration jetty

我的Java应用程序使用嵌入式Jetty 9.2.2。我在pom.xml中添加了一个包含web_fragment.xml文件的库。但这个片段并没有被Jetty拾取。当我启动应用程序时,我可以在日志中看到库已加载。但是,当从库中向servlet发出请求时,应用程序返回404。 如何让它发挥作用?

在app中有一个Spring配置文件dispatcher-servlet.xml,其中包含了库:

<import resource="classpath:/web.fragment.lib.spring.xml" />

没有web.xml文件,但该应用程序包含带有映射的spring.xml文件。它使用dispatcher-servlet.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"
       default-lazy-init="false">

  <context:annotation-config/>
  <context:property-placeholder system-properties-mode="FALLBACK" location="classpath:config.properties"/>

  <bean name="WebServer" class="org.eclipse.jetty.server.Server" init-method="start">
    <property name="connectors">
      <list>
        <bean name="LocalSocket" class="org.eclipse.jetty.server.ServerConnector">
          <constructor-arg ref="WebServer"/>
          <property name="host" value="0.0.0.0"/>
          <property name="port" value="${jetty.port}"/>
        </bean>
      </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="sessionHandler">
                <bean class="org.eclipse.jetty.server.session.SessionHandler"/>
              </property>
              <property name="contextPath" value="${context.path}"/>
              <property name="servletHandler">
                <bean class="org.eclipse.jetty.servlet.ServletHandler">
                  <property name="servlets">
                    <list>
                      <bean class="org.eclipse.jetty.servlet.ServletHolder">
                        <property name="name" value="dispatcherServlet"/>
                        <property name="servlet">
                          <bean class="org.springframework.web.servlet.DispatcherServlet"/>
                        </property>
                        <property name="initParameters">
                          <map>
                            <entry key="contextConfigLocation" value="**classpath:dispatcher-servlet.xml**"/>
                          </map>
                        </property>
                      </bean>
                    </list>
                  </property>
                  <property name="servletMappings">
                    <list>
                      <bean class="org.eclipse.jetty.servlet.ServletMapping">
                        <property name="pathSpecs">
                          <list>
                            <value>/</value>
                          </list>
                        </property>
                        <property name="servletName" value="dispatcherServlet"/>
                      </bean>
                    </list>
                  </property>
                </bean>
              </property>
            </bean>
          </list>
        </property>
      </bean>
    </property>
  </bean>
</beans>

1 个答案:

答案 0 :(得分:1)

Web Fragment auto configuration is a feature of the WebAppContext's Configuration layers

在您的示例中,您既不使用也不使用。 您正在使用嵌入式意义上的Jetty,并且正在手动构建servlet列表。

您必须切换到通过WebAppContext构建应用程序,或者手动添加这些Web片段提供的功能。

要理解的重要一点是,Web片段是webapp描述符的片段,这是webapp的一个复杂特性,它是由WebAppContext跟踪的,这是由Configuration列表配置的。在特定WebAppContext中定义的层。