Yesterday, I came accross a weird error when working with a Spring-enabled application. It was working fine within Eclipe but wouldn't run on the command line:
Exception in thread "main" org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/p]
I knew that Spring classes were in the classpath, but I didn't know why they were not found, or rather why the association between the namespace and the handler was not established.
By looking at Spring's documentation, I found that explained that you needed to implement the interface to map a namespace to a handler. I quickly found the handler that was not mapped: .
The class is instanciated by reflection which means I couldn't use Eclipse to find callers. Instead, I simply put a breakpoint on the init()
method and ran the application. That way, I found that was instanciating the handler, and that the method was responsible for loading namespace-handler mappings.
The code is quite clear: it loads all the files named META-INF/spring.handlers
. And here it is! There is more than one file named this way: there is one in spring-beans, one in spring-context, one in spring-aop, etc.
The issue is that, to make the application work outside Eclipse, we use the . It expands all the dependent JARs into the final JAR. This means it cannot put several files with the same name in the package and it has to choose one of the spring.handlers
files.
As a workaround, I created my own spring.handlers
with the content from all thespring.handlers
files I could find in Spring packages. You could wonder how this works because this is only one more file to choose from, but it looks like that the local file is picked first and is not overriden by files from dependent packages!
Here is the file I created:
http\://www.springframework.org/schema/p=org.springframework.beans.factory.xml.SimplePropertyNamespaceHandlerhttp\://www.springframework.org/schema/util=org.springframework.beans.factory.xml.UtilNamespaceHandlerhttp\://www.springframework.org/schema/aop=org.springframework.aop.config.AopNamespaceHandlerhttp\://www.springframework.org/schema/context=org.springframework.context.config.ContextNamespaceHandlerhttp\://www.springframework.org/schema/jee=org.springframework.ejb.config.JeeNamespaceHandlerhttp\://www.springframework.org/schema/lang=org.springframework.scripting.config.LangNamespaceHandler
I found a few references to this issue:
[From]