2016-10-23 57 views
0

我已經使用註釋驅動配置生成了Spring Boot應用程序。Spring Boot中的web.xml無法找到XML名稱空間的Spring NamespaceHandler

但是我想用xml配置來配置一些Spring Security,因爲我已經有了一個XML文件。

所以我創建的web.xml文件:

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 



</web-app> 

,並添加@ImportResource("classpath:web.xml")@SpringBootApplication

@SpringBootApplication 
@ImportResource("classpath:web.xml") 
public class Application extends WebMvcConfigurerAdapter { 

    public static void main(String[] args) { 
     SpringApplication.run(new Class[] { Application.class, WebAppInitializer.class }, args); 
    } 
} 

然後我得到:

2016-10- 23 13:01:52.888錯誤9120 --- [main] osboot.SpringApplication:應用程序啓動失敗

org.springframework.beans.factory.parsing.BeanDefinitionParsingException: 配置問題:找不到春天NamespaceHandler爲 XML schema命名空間[http://java.sun.com/xml/ns/javaee]犯規 資源:類路徑的資源[web.xml文件]

哪裏不對?

回答

1

web.xml沒有spring bean配置文件,所以你不能在spring bean配置中包含它(你不能使用@ImportResource("classpath:web.xml"))!

  • web.xml是配置您的servlet容器(例如tomcat)。
  • 春豆XML文件來配置Spring

所以兩者是不同的。

彈簧@ImportResource註解用於導入spring bean配置,但不適用於servelt容器配置!

+0

好,顯然我有一些差距,因爲我習慣於註釋解決方案。那麼,我怎麼能在web.xml中配置過濾器並使其能夠與Spring Boot一起工作呢? – dragonfly

+0

正確,web.xml將配置您的servlet容器(例如tomcat)。相反,你可以這樣做,例如:@ImportResource(「classpath:applicationContext.xml」) –

0

此代碼演示瞭如何利用現有的XML(不web.xml)中的配置文件在主春季啓動應用程序(或者你已經是你需要使用一些Java配置):

@ImportResource("classpath:applicationContext.xml") 
@Configuration 
public class SimpleConfiguration { 
@Autowired 
Connection connection; //This comes from the applicationContext.xml file. 
@Bean 
Database getDatabaseConnection(){ 
return connection.getDBConnection(); 
} 
// Mode code here.... 
} 
相關問題