dragonfly
dragonfly

Reputation: 17773

web.xml in Spring Boot throws Unable to locate Spring NamespaceHandler for XML schema namespace

I've generated Spring Boot application with annotation driven configuration.

However I wanted to configure some of the Spring Security with xml configuration because I have already a XML file that does it.

So I've created web.xml file:

<?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>

and added @ImportResource("classpath:web.xml") to my @SpringBootApplication class

@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);
    }
}

And then I get:

2016-10-23 13:01:52.888 ERROR 9120 --- [ main] o.s.boot.SpringApplication : Application startup failed

org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://java.sun.com/xml/ns/javaee] Offending resource: class path resource [web.xml]

What is wrong?

Upvotes: 0

Views: 728

Answers (2)

AchillesVan
AchillesVan

Reputation: 4356

This code shows how you can use existing XML (not web.xml)configuration files in your main Spring Boot application(or maybe you have already some Java config that you need to use):

@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....
}

Upvotes: 0

Ralph
Ralph

Reputation: 120821

The web.xml is no spring beans configuration file, so you must not include it in the spring bean configuration (you must not use @ImportResource("classpath:web.xml") )!

  • The web.xml is to configure your servlet container (for example tomcat).
  • The spring beans xml files are to configure Spring

So both are different.

Springs @ImportResource annotation is for importing spring bean configurations but not for servelt container configurations!

Upvotes: 1

Related Questions