0

我正在嘗試使用jetty-maven-plugin和no-xml彈簧配置,但在使用jettys自動類重新加載時遇到了問題。當我使用web.xml時,一切正常。重新加載jetty-maven-plugin 503的

WebApplicationInitializer.java替換舊的web.xml

public class WebAppInitializer implements WebApplicationInitializer { 
    @Override 
    public void onStartup(ServletContext container) {   
    AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext(); 
    dispatcherContext.register(DispatcherModule.class); 

    ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", new DispatcherServlet(dispatcherContext)); 
    dispatcher.setLoadOnStartup(1); 
    dispatcher.addMapping("/"); 
    } 
} 

ApplicationModule annotated with @Configuration用於調度的servlet提供配置。

@Configuration 
@EnableWebMvc 
@ComponentScan("com.id.try") 
public class DispatcherModule { 
    @Bean 
    public InternalResourceViewResolver internalResourceViewResolver() { 
    InternalResourceViewResolver resolver = new InternalResourceViewResolver(); 
    resolver.setPrefix("/WEB-INF/pages/"); 
    resolver.setSuffix(".jsp"); 
    return resolver; 
    } 
} 

最後我POM文件拉依賴關係和配置jetty-maven-plugin

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 
    <!-- Initial Config: Truncated --> 
    <properties> 
    <spring.version>3.2.3.RELEASE</spring.version> 
    <jetty.plugin.version>8.1.11.v20130520</jetty.plugin.version> 
    <java.version>1.7</java.version> 
    <javax.servlet-api.version>3.0.1</javax.servlet-api.version> 
    </properties> 

    <dependencies> 
    <!-- Dependencies Truncated: versions are above --> 
    </dependencies> 

    <build> 
    <plugins> 
     <plugin> 
     <groupId>org.mortbay.jetty</groupId> 
     <artifactId>jetty-maven-plugin</artifactId> 
     <version>${jetty.plugin.version}</version> 
     <configuration> 
      <scanIntervalSeconds>1</scanIntervalSeconds> 
      <webApp> 
      <contextPath>/</contextPath> 
      </webApp> 
     </configuration> 
     </plugin> 
    </plugins> 
    </build> 
</project> 

的問題是,當我在一個控制器改變的東西作爲比喻爲一個字符串後重新編譯,碼頭會做,因爲它應重新加載類時,檢測到更改並重新啓動,但它有一個NullPointerException

java.lang.NullPointerException 
    at com.id.tryconfiguration.WebAppInitializer.onStartup(WebAppInitializer.java:18) 
    at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:180) 
    at org.eclipse.jetty.plus.annotation.ContainerInitializer.callStartup(ContainerInitializer.java:106) 
    at org.eclipse.jetty.annotations.ServletContainerInitializerListener.doStart(ServletContainerInitializerListener.java:107) 
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64) 
    at org.eclipse.jetty.util.component.AggregateLifeCycle.doStart(AggregateLifeCycle.java:81) 
    at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:58) 
    at org.eclipse.jetty.server.handler.HandlerWrapper.doStart(HandlerWrapper.java:96) 
    at org.eclipse.jetty.server.handler.ScopedHandler.doStart(ScopedHandler.java:115) 
    at org.eclipse.jetty.server.handler.ContextHandler.startContext(ContextHandler.java:756) 
    at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:249) 
    at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1252) 
    at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:710) 
    at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:494) 
    at org.mortbay.jetty.plugin.JettyWebAppContext.doStart(JettyWebAppContext.java:293) 
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64) 
    at org.mortbay.jetty.plugin.JettyRunMojo.restartWebApp(JettyRunMojo.java:435) 
    at org.mortbay.jetty.plugin.JettyRunMojo$1.filesChanged(JettyRunMojo.java:394) 
    at org.eclipse.jetty.util.Scanner.reportBulkChanges(Scanner.java:691) 
    at org.eclipse.jetty.util.Scanner.reportDifferences(Scanner.java:551) 
    at org.eclipse.jetty.util.Scanner.scan(Scanner.java:403) 
    at org.eclipse.jetty.util.Scanner$1.run(Scanner.java:353) 
    at java.util.TimerThread.mainLoop(Timer.java:555) 
    at java.util.TimerThread.run(Timer.java:505) 

開始我試圖排除配置類es和WebApplicationInitializer.java但它不能解決問題。

如何讓SpringMVC使用no-xml和jetty class reloader?

回答

0

通過更改jetty-maven-plugin和源代碼解決。

<groupId>org.eclipse.jetty</groupId> 
<jetty.plugin.version>9.0.4.v20130625</jetty.plugin.version>