Spring错误:无法找到XML架构命名空间的Spring NamespaceHandler

时间:2014-07-06 04:18:22

标签: spring

我正在使用Spring开发JavaFX应用程序。我相信我的问题源于这样一个事实,即由于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:\Bla\Bla\target

--- 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:\bla\Bla\target\classes

--- 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:\bla\Bla\src\test\resources

--- 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:\bla\Bla\target\surefire-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:\bla\Bla\target\Bla-1.0-SNAPSHOT.jar

--- maven-dependency-plugin:2.6:unpack-dependencies (unpack-dependencies) @ Bla ---
Unpacking C:\home-dir\.m2\repository\org\springframework\spring-jdbc\3.1.4.RELEASE\spring-jdbc-3.1.4.RELEASE.jar to C:\bla\Bla\target\classes with includes "" and excludes ""
Unpacking C:\home-dir\.m2\repository\org\springframework\spring-orm\3.1.4.RELEASE\spring-orm-3.1.4.RELEASE.jar to C:\bla\Bla\target\classes with includes "" and excludes ""
Unpacking C:\home-dir\.m2\repository\org\springframework\spring-beans\3.1.4.RELEASE\spring-beans-3.1.4.RELEASE.jar to C:\bla\Bla\target\classes with includes "" and excludes ""
Unpacking C:\home-dir\.m2\repository\mysql\mysql-connector-java\5.1.31\mysql-connector-java-5.1.31.jar to C:\bla\Bla\target\classes with includes "" and excludes ""
Unpacking C:\home-dir\.m2\repository\org\jboss\spec\javax\transaction\jboss-transaction-api_1.1_spec\1.0.0.Final\jboss-transaction-api_1.1_spec-1.0.0.Final.jar to C:\bla\Bla\target\classes with includes "" and excludes ""
Unpacking C:\home-dir\.m2\repository\commons-logging\commons-logging\1.1.1\commons-logging-1.1.1.jar to C:\bla\Bla\target\classes with includes "" and excludes ""
Unpacking C:\home-dir\.m2\repository\org\springframework\spring-context-support\3.1.4.RELEASE\spring-context-support-3.1.4.RELEASE.jar to C:\bla\Bla\target\classes with includes "" and excludes ""
Unpacking C:\home-dir\.m2\repository\org\hibernate\common\hibernate-commons-annotations\4.0.1.Final\hibernate-commons-annotations-4.0.1.Final.jar to C:\bla\Bla\target\classes with includes "" and excludes ""
Unpacking C:\home-dir\.m2\repository\org\springframework\spring-core\3.1.4.RELEASE\spring-core-3.1.4.RELEASE.jar to C:\bla\Bla\target\classes with includes "" and excludes ""
Unpacking C:\home-dir\.m2\repository\org\springframework\data\spring-data-commons-core\1.4.1.RELEASE\spring-data-commons-core-1.4.1.RELEASE.jar to C:\bla\Bla\target\classes with includes "" and excludes ""
Unpacking C:\home-dir\.m2\repository\org\springframework\spring-aop\3.1.4.RELEASE\spring-aop-3.1.4.RELEASE.jar to C:\bla\Bla\target\classes with includes "" and excludes ""
Unpacking C:\home-dir\.m2\repository\org\springframework\spring-expression\3.1.4.RELEASE\spring-expression-3.1.4.RELEASE.jar to C:\bla\Bla\target\classes with includes "" and excludes ""
Unpacking C:\home-dir\.m2\repository\org\hibernate\hibernate-core\4.1.2.Final\hibernate-core-4.1.2.Final.jar to C:\bla\Bla\target\classes with includes "" and excludes ""
Unpacking C:\home-dir\.m2\repository\org\springframework\data\spring-data-jpa\1.2.1.RELEASE\spring-data-jpa-1.2.1.RELEASE.jar to C:\bla\Bla\target\classes with includes "" and excludes ""
Unpacking C:\home-dir\.m2\repository\org\hibernate\hibernate-entitymanager\4.1.2.Final\hibernate-entitymanager-4.1.2.Final.jar to C:\bla\Bla\target\classes with includes "" and excludes ""
Unpacking C:\home-dir\.m2\repository\org\springframework\spring-context\3.1.4.RELEASE\spring-context-3.1.4.RELEASE.jar to C:\bla\Bla\target\classes with includes "" and excludes ""
Unpacking C:\home-dir\.m2\repository\org\slf4j\slf4j-api\1.6.1\slf4j-api-1.6.1.jar to C:\bla\Bla\target\classes with includes "" and excludes ""
Unpacking C:\home-dir\.m2\repository\org\springframework\spring-asm\3.1.4.RELEASE\spring-asm-3.1.4.RELEASE.jar to C:\bla\Bla\target\classes with includes "" and excludes ""
Unpacking C:\home-dir\.m2\repository\dom4j\dom4j\1.6.1\dom4j-1.6.1.jar to C:\bla\Bla\target\classes with includes "" and excludes ""
Unpacking C:\home-dir\.m2\repository\org\aspectj\aspectjrt\1.6.12\aspectjrt-1.6.12.jar to C:\bla\Bla\target\classes with includes "" and excludes ""
Unpacking C:\home-dir\.m2\repository\antlr\antlr\2.7.7\antlr-2.7.7.jar to C:\bla\Bla\target\classes with includes "" and excludes ""
Unpacking C:\home-dir\.m2\repository\org\jboss\logging\jboss-logging\3.1.0.GA\jboss-logging-3.1.0.GA.jar to C:\bla\Bla\target\classes with includes "" and excludes ""
Unpacking C:\home-dir\.m2\repository\org\hibernate\javax\persistence\hibernate-jpa-2.0-api\1.0.1.Final\hibernate-jpa-2.0-api-1.0.1.Final.jar to C:\bla\Bla\target\classes with includes "" and excludes ""
Unpacking C:\home-dir\.m2\repository\aopalliance\aopalliance\1.0\aopalliance-1.0.jar to C:\bla\Bla\target\classes with includes "" and excludes ""
Unpacking C:\home-dir\.m2\repository\org\slf4j\jcl-over-slf4j\1.6.1\jcl-over-slf4j-1.6.1.jar to C:\bla\Bla\target\classes with includes "" and excludes ""
Unpacking C:\home-dir\.m2\repository\org\javassist\javassist\3.15.0-GA\javassist-3.15.0-GA.jar to C:\bla\Bla\target\classes with includes "" and excludes ""
Unpacking C:\home-dir\.m2\repository\org\springframework\spring-tx\3.1.4.RELEASE\spring-tx-3.1.4.RELEASE.jar to C:\bla\Bla\target\classes 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:\bla\Bla\target\Bla-1.0-SNAPSHOT.jar to C:\home-dir\.m2\repository\co\bla\Bla\1.0-SNAPSHOT\Bla-1.0-SNAPSHOT.jar
Installing C:\bla\Bla\pom.xml to C:\home-dir\.m2\repository\co\bla\Bla\1.0-SNAPSHOT\Bla-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 个答案:

答案 0 :(得分:2)

我最近遇到了同样的问题。 META-INF目录中的spring.handlers和spring.schemas文件可能存在问题。

原始文件缺少对http://www.springframework.org/schema/context

的引用

Maven shade插件(如建议here)为我做了诀窍

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