2017-08-09 69 views
0

這是我的入門應用程序類Spring引導應用程序。起動器類找不到.xml文件

@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class }) 
@ImportResource("classpath : web.xml") 
public class WebPortalApplication { 
    public static void main(String[] args) { 
     SpringApplication.run(WebPortalApplication.class, args); 
    } 
} 

這是我的項目目錄結構

enter image description here

,當我運行的應用程序將打印此

Caused by: java.io.FileNotFoundException: class path resource [classpath : web.xml] cannot be opened because it does not exist 
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:172) ~[spring-core-4.3.10.RELEASE.jar:4.3.10.RELEASE] 
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:330) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE] 

有人可以幫我解決這個問題嗎?

+2

@ImportResource註釋用於Spring bean定義文件。 如果您試圖導入部署描述符(正如我從文件名「web.xml」中假設的那樣),我建議您使用Spring Boot提供的任何嵌入式Tomcat/Jetty/Undertow服務器,這樣您就可以贏得不再需要部署描述符。請參閱http://docs.spring.io/spring-boot/docs/1.5.x/reference/htmlsingle/#boot-features-developing-web-applications – aaguilera

回答

0

您不需要導入web.xml,因爲它不是彈簧配置,它是一個部署描述符。

將項目包裝類型從jar更改爲war servlet容器應自動取回您的web.xml

的pom.xml

- <packaging>jar</packaging> 
+ <packaging>war</packaging> 
0

春天開機,你不必寫在Spring配置 'web.xml' 中,你可以這樣寫:

@SpringBootApplication 
public class WebPortalApplication{ 
    public static void main(String[] args) { 
     SpringApplication.run(WebPortalApplication.class, args); 
    } 
} 

你應該把你的WebPortalApplication.class放入你想要的Spring對象中。

+0

好的,親愛的湯姆,但在XML文件中的所有員工呢? 。我從mvc切換到引導,這4個xml文件做了很多工作:創建bean,攔截器,上下文等。現在如何做,以便所有這些操作執行其任務? –

+0

那麼,關於這個問題,你可以參考這個鏈接: https://stackoverflow.com/questions/31082981/spring-boot-adding-http-request-interceptors。 或春季啓動官方網站,https://projects.spring.io/spring-boot/。 –

相關問題