Ошибка Spring: не удается найти Spring NamespaceHandler для пространства имен XML-схемы

#spring

#spring

Вопрос:

Я разрабатываю приложение JavaFX с использованием Spring. Я полагаю, что моя проблема возникла из-за того, что, поскольку библиотеки Spring копируются в каталог, локальный для каталога проекта, проект является проектом JavaFX, Spring не может найти библиотеки, необходимые для анализа файла контекста приложения.

Ниже приведен файл Maven.

 <?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>co.bla</groupId>
    <artifactId>Bla</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>Bla</name>

    <properties>
        <org.springframework.version>3.1.4.RELEASE</org.springframework.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <mainClass>co.bla.MainApp</mainClass>
    </properties>

    <organization>
        <!-- Used as the 'Vendor' for JNLP generation -->
        <name>Bla</name>
    </organization>

    <dependencies>        
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>3.1.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
            <version>1.2.1.RELEASE</version>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-aop</artifactId>
                </exclusion>                
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-asm</artifactId>
                </exclusion>
            </exclusions>
        </dependency>                
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.1.2.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>4.1.2.Final</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.31</version>
        </dependency>        
    </dependencies>   

    <repositories>
        <repository>
            <id>JBoss repository</id>
            <url>http://repository.jboss.org/nexus/content/groups/public/</url>
        </repository>        
    </repositories>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.6</version>
                <executions>
                    <execution>
                        <id>unpack-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>unpack-dependencies</goal>
                        </goals>
                        <configuration>
                            <excludeScope>system</excludeScope>
                            <excludeGroupIds>junit,org.mockito,org.hamcrest</excludeGroupIds>
                            <outputDirectory>${project.build.directory}/classes</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <executions>
                    <execution>
                        <id>unpack-dependencies</id>

                        <phase>package</phase>
                        <goals>
                            <goal>exec</goal>
                        </goals>
                        <configuration>
                            <executable>${java.home}/../bin/javafxpackager</executable>
                            <arguments>
                                <argument>-createjar</argument>
                                <argument>-nocss2bin</argument>
                                <argument>-appclass</argument>
                                <argument>${mainClass}</argument>
                                <argument>-srcdir</argument>
                                <argument>${project.build.directory}/classes</argument>
                                <argument>-outdir</argument>
                                <argument>${project.build.directory}</argument>
                                <argument>-outfile</argument>
                                <argument>${project.build.finalName}.jar</argument>
                            </arguments>
                        </configuration>
                    </execution>
                </executions>  
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                    <compilerArguments>
                        <bootclasspath>${sun.boot.class.path}${path.separator}${java.home}/lib/jfxrt.jar</bootclasspath>
                    </compilerArguments>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>
  

Компиляция проекта приводит к следующему результату.

 Scanning for projects...

------------------------------------------------------------------------
Building Bla 1.0-SNAPSHOT
------------------------------------------------------------------------

--- maven-clean-plugin:2.4.1:clean (default-clean) @ Bla ---
Deleting C:BlaBlatarget

--- maven-resources-plugin:2.5:resources (default-resources) @ Bla ---
[debug] execute contextualize
Using 'UTF-8' encoding to copy filtered resources.
Copying 4 resources

--- maven-compiler-plugin:3.1:compile (default-compile) @ Bla ---
Changes detected - recompiling the module!
Compiling 5 source files to C:blaBlatargetclasses

--- maven-resources-plugin:2.5:testResources (default-testResources) @ Bla ---
[debug] execute contextualize
Using 'UTF-8' encoding to copy filtered resources.
skip non existing resourceDirectory C:blaBlasrctestresources

--- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ Bla ---
No sources to compile

--- maven-surefire-plugin:2.10:test (default-test) @ Bla ---
No tests to run.
Surefire report directory: C:blaBlatargetsurefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------

Results :

Tests run: 0, Failures: 0, Errors: 0, Skipped: 0


--- maven-jar-plugin:2.3.2:jar (default-jar) @ Bla ---
Building jar: C:blaBlatargetBla-1.0-SNAPSHOT.jar

--- maven-dependency-plugin:2.6:unpack-dependencies (unpack-dependencies) @ Bla ---
Unpacking C:home-dir.m2repositoryorgspringframeworkspring-jdbc3.1.4.RELEASEspring-jdbc-3.1.4.RELEASE.jar to C:blaBlatargetclasses with includes "" and excludes ""
Unpacking C:home-dir.m2repositoryorgspringframeworkspring-orm3.1.4.RELEASEspring-orm-3.1.4.RELEASE.jar to C:blaBlatargetclasses with includes "" and excludes ""
Unpacking C:home-dir.m2repositoryorgspringframeworkspring-beans3.1.4.RELEASEspring-beans-3.1.4.RELEASE.jar to C:blaBlatargetclasses with includes "" and excludes ""
Unpacking C:home-dir.m2repositorymysqlmysql-connector-java5.1.31mysql-connector-java-5.1.31.jar to C:blaBlatargetclasses with includes "" and excludes ""
Unpacking C:home-dir.m2repositoryorgjbossspecjavaxtransactionjboss-transaction-api_1.1_spec1.0.0.Finaljboss-transaction-api_1.1_spec-1.0.0.Final.jar to C:blaBlatargetclasses with includes "" and excludes ""
Unpacking C:home-dir.m2repositorycommons-loggingcommons-logging1.1.1commons-logging-1.1.1.jar to C:blaBlatargetclasses with includes "" and excludes ""
Unpacking C:home-dir.m2repositoryorgspringframeworkspring-context-support3.1.4.RELEASEspring-context-support-3.1.4.RELEASE.jar to C:blaBlatargetclasses with includes "" and excludes ""
Unpacking C:home-dir.m2repositoryorghibernatecommonhibernate-commons-annotations4.0.1.Finalhibernate-commons-annotations-4.0.1.Final.jar to C:blaBlatargetclasses with includes "" and excludes ""
Unpacking C:home-dir.m2repositoryorgspringframeworkspring-core3.1.4.RELEASEspring-core-3.1.4.RELEASE.jar to C:blaBlatargetclasses with includes "" and excludes ""
Unpacking C:home-dir.m2repositoryorgspringframeworkdataspring-data-commons-core1.4.1.RELEASEspring-data-commons-core-1.4.1.RELEASE.jar to C:blaBlatargetclasses with includes "" and excludes ""
Unpacking C:home-dir.m2repositoryorgspringframeworkspring-aop3.1.4.RELEASEspring-aop-3.1.4.RELEASE.jar to C:blaBlatargetclasses with includes "" and excludes ""
Unpacking C:home-dir.m2repositoryorgspringframeworkspring-expression3.1.4.RELEASEspring-expression-3.1.4.RELEASE.jar to C:blaBlatargetclasses with includes "" and excludes ""
Unpacking C:home-dir.m2repositoryorghibernatehibernate-core4.1.2.Finalhibernate-core-4.1.2.Final.jar to C:blaBlatargetclasses with includes "" and excludes ""
Unpacking C:home-dir.m2repositoryorgspringframeworkdataspring-data-jpa1.2.1.RELEASEspring-data-jpa-1.2.1.RELEASE.jar to C:blaBlatargetclasses with includes "" and excludes ""
Unpacking C:home-dir.m2repositoryorghibernatehibernate-entitymanager4.1.2.Finalhibernate-entitymanager-4.1.2.Final.jar to C:blaBlatargetclasses with includes "" and excludes ""
Unpacking C:home-dir.m2repositoryorgspringframeworkspring-context3.1.4.RELEASEspring-context-3.1.4.RELEASE.jar to C:blaBlatargetclasses with includes "" and excludes ""
Unpacking C:home-dir.m2repositoryorgslf4jslf4j-api1.6.1slf4j-api-1.6.1.jar to C:blaBlatargetclasses with includes "" and excludes ""
Unpacking C:home-dir.m2repositoryorgspringframeworkspring-asm3.1.4.RELEASEspring-asm-3.1.4.RELEASE.jar to C:blaBlatargetclasses with includes "" and excludes ""
Unpacking C:home-dir.m2repositorydom4jdom4j1.6.1dom4j-1.6.1.jar to C:blaBlatargetclasses with includes "" and excludes ""
Unpacking C:home-dir.m2repositoryorgaspectjaspectjrt1.6.12aspectjrt-1.6.12.jar to C:blaBlatargetclasses with includes "" and excludes ""
Unpacking C:home-dir.m2repositoryantlrantlr2.7.7antlr-2.7.7.jar to C:blaBlatargetclasses with includes "" and excludes ""
Unpacking C:home-dir.m2repositoryorgjbossloggingjboss-logging3.1.0.GAjboss-logging-3.1.0.GA.jar to C:blaBlatargetclasses with includes "" and excludes ""
Unpacking C:home-dir.m2repositoryorghibernatejavaxpersistencehibernate-jpa-2.0-api1.0.1.Finalhibernate-jpa-2.0-api-1.0.1.Final.jar to C:blaBlatargetclasses with includes "" and excludes ""
Unpacking C:home-dir.m2repositoryaopallianceaopalliance1.0aopalliance-1.0.jar to C:blaBlatargetclasses with includes "" and excludes ""
Unpacking C:home-dir.m2repositoryorgslf4jjcl-over-slf4j1.6.1jcl-over-slf4j-1.6.1.jar to C:blaBlatargetclasses with includes "" and excludes ""
Unpacking C:home-dir.m2repositoryorgjavassistjavassist3.15.0-GAjavassist-3.15.0-GA.jar to C:blaBlatargetclasses with includes "" and excludes ""
Unpacking C:home-dir.m2repositoryorgspringframeworkspring-tx3.1.4.RELEASEspring-tx-3.1.4.RELEASE.jar to C:blaBlatargetclasses with includes "" and excludes ""

--- exec-maven-plugin:1.2.1:exec (unpack-dependencies) @ Bla ---

--- maven-install-plugin:2.3.1:install (default-install) @ Bla ---
Installing C:blaBlatargetBla-1.0-SNAPSHOT.jar to C:home-dir.m2repositorycoblaBla1.0-SNAPSHOTBla-1.0-SNAPSHOT.jar
Installing C:blaBlapom.xml to C:home-dir.m2repositorycoblaBla1.0-SNAPSHOTBla-1.0-SNAPSHOT.pom
------------------------------------------------------------------------
BUILD SUCCESS
------------------------------------------------------------------------
Total time: 1:39.198s
Finished at: Sun Jul 06 10:31:34 MMT 2014
Final Memory: 14M/37M
------------------------------------------------------------------------
  

Запуск проекта приводит к этой ошибке:

 java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.javafx.main.Main.launchApp(Main.java:698)
    at com.javafx.main.Main.main(Main.java:871)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:403)
    at com.sun.javafx.application.LauncherImpl.access$000(LauncherImpl.java:47)
    at com.sun.javafx.application.LauncherImpl$1.run(LauncherImpl.java:115)
    at java.lang.Thread.run(Thread.java:744)
Caused by: org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/context]
Offending resource: class path resource [application-context.xml]

    at org.springframework.beans.factory.parsing.FailFastProblemReporter.error(FailFastProblemReporter.java:68)
    at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:85)
    at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:80)
    at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.error(BeanDefinitionParserDelegate.java:316)
    at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1420)
    at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1413)
    at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:184)
    at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.doRegisterBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:140)
    at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.registerBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:111)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.registerBeanDefinitions(XmlBeanDefinitionReader.java:493)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:390)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:334)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:174)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:209)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:243)
    at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:127)
    at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:93)
    at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:131)
    at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:530)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:444)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
    at co.infovilla.pos.ivendor.MainApp.start(MainApp.java:19)
    at com.sun.javafx.application.LauncherImpl$5.run(LauncherImpl.java:319)
    at com.sun.javafx.application.PlatformImpl$5.run(PlatformImpl.java:216)
    at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:179)
    at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:176)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:176)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:76)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:17)
    at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:67)
    ... 1 more
  

Ответ №1:

Недавно я столкнулся с такой же проблемой. Вероятно, проблема с файлами spring.handlers и spring.schemas в каталоге META-INF.

В исходных файлах отсутствует ссылка на http://www.springframework.org/schema/context

Плагин Maven shade (как предложено здесь) сделал свое дело за меня

 <plugin>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.3</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <transformers>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                <resource>META-INF/spring.handlers</resource>
                            </transformer>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                <resource>META-INF/spring.schemas</resource>
                            </transformer>
                        </transformers>
                    </configuration>
                </execution>
            </executions>
        </plugin>