V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
pohvii
V2EX  ›  问与答

spring 4.1.4 - Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/

  •  
  •   pohvii · 2015-02-05 09:43:59 +08:00 · 4937 次点击
    这是一个创建于 3578 天前的主题,其中的信息可能已经有所发展或是发生改变。

    program(application, not web) works in IntelliJ, but when I package the program to a jar file, and use "java -jar xxx.jar" run it, it shows the Exception:

    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/tx]
    Offending resource: class path resource [spring-config.xml]
    

    maven pom.xml:

    <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <cxf.version>3.0.3</cxf.version>
    <spring.version>4.1.4.RELEASE</spring.version>
    <hibernate.version>4.3.8.Final</hibernate.version>
    </properties>
    <dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>${spring.version}</version>
    </dependency>
    

    spring spring-config.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:task="http://www.springframework.org/schema/task"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
       http://www.springframework.org/schema/task
       http://www.springframework.org/schema/task/spring-task-4.1.xsd">
    
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
    </bean>
    
    <bean id="transactionManager"
          class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    <tx:annotation-driven transaction-manager="transactionManager"/>
    
    <bean id="HibernateDao" class="com.dao.HibernateDao">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
    
    <bean id="MainApp" class="com.service.MainApp">
        <property name="dao" ref="HibernateDao"/>
    </bean>
    
    <task:annotation-driven/>
    </beans>
    

    main class

    package com.service;
    
    import com.dao.HibernateDao;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import org.springframework.scheduling.annotation.Scheduled;
    
    import java.text.SimpleDateFormat;
    import java.util.List;
    
    public class MainApp {
    
    HibernateDao dao;
    
    @Scheduled(cron = "0 0 12 * * ?")
    public void service() {
        // the code ...
    }
    
    public HibernateDao getDao() {
        return dao;
    }
    
    public void setDao(HibernateDao dao) {
        this.dao = dao;
    }
    
    public static void main(String[] args) {
        ApplicationContext context =
                new ClassPathXmlApplicationContext("spring-config.xml");
    }
    
    }
    

    add Artifacts as Add -> JAR -> From modules with dependencies... then choose Main Class and "extract to the target JAR"

    I build Artifacts and META-INF/MANFIEST.MF and a jar file created

    Manifest-Version: 1.0
    Main-Class: com.service.MainApp
    
    第 1 条附言  ·  2015-02-05 11:10:03 +08:00
    第 2 条附言  ·  2015-02-09 15:24:03 +08:00
    工程Demo地址:http://pan.baidu.com/s/1pJPuDYj
    8 条回复    2015-02-09 15:35:22 +08:00
    reeco
        1
    reeco  
       2015-02-05 10:31:29 +08:00 via iPhone
    看情况像是maven打包时没把依赖的spring一起放进去,你解压下jar包检查下spring有没有在里面
    feilaoda
        2
    feilaoda  
       2015-02-05 10:49:01 +08:00
    既然用maven了,怎么不用mvn package打包?
    你的pom.xml没贴全,不好评价是否有问题。
    pohvii
        3
    pohvii  
    OP
       2015-02-05 10:59:52 +08:00
    @reeco 有 org/springframework/
    pohvii
        4
    pohvii  
    OP
       2015-02-05 11:03:22 +08:00
    @feilaoda 我用 assembly:assembly 打包也有问题
    <?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>GAImage</groupId>
    <artifactId>GAImage</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <cxf.version>3.0.3</cxf.version>
    <spring.version>4.1.4.RELEASE</spring.version>
    <hibernate.version>4.3.8.Final</hibernate.version>
    </properties>

    <dependencies>

    <!-- Spring framework -->
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>${spring.version}</version>
    </dependency>
    <!-- Spring持久化 -->
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-orm</artifactId>
    <version>${spring.version}</version>
    </dependency>

    <!-- Hibernate framework -->
    <dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>${hibernate.version}</version>
    </dependency>
    <!-- Without maven dependency Hibernate just silently ignores C3P0 configuration. -->
    <dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-c3p0</artifactId>
    <version>${hibernate.version}</version>
    </dependency>
    <dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-ehcache</artifactId>
    <version>${hibernate.version}</version>
    </dependency>

    <!-- cxf framework -->
    <dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-frontend-jaxws</artifactId>
    <version>${cxf.version}</version>
    </dependency>
    <dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-transports-http</artifactId>
    <version>${cxf.version}</version>
    </dependency>

    <!-- oracle driver-->
    <dependency>
    <groupId>com.oracle</groupId>
    <artifactId>ojdbc6</artifactId>
    <version>11.2.0.3</version>
    </dependency>

    </dependencies>

    <repositories>
    <repository>
    <id>codelds</id>
    <url>https://code.lds.org/nexus/content/groups/main-repo</url>
    </repository>
    </repositories>
    <!-- build后来加的 -->
    <build>
    <plugins>
    <plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.5.3</version>
    <configuration>
    <appendAssemblyId>false</appendAssemblyId>
    <descriptorRefs>
    <descriptorRef>jar-with-dependencies</descriptorRef>
    </descriptorRefs>
    <archive>
    <manifest>
    <mainClass>com.service.MainApp</mainClass>
    </manifest>
    </archive>
    </configuration>
    <executions>
    <execution>
    <id>make-assembly</id>
    <phase>package</phase>
    <goals>
    <goal>assembly</goal>
    </goals>
    </execution>
    </executions>
    </plugin>
    </plugins>
    </build>
    </project>
    gaocegege
        5
    gaocegege  
       2015-02-05 11:18:30 +08:00
    我擦哥们,我们头像惊人的相似啊。。。

    可以试试:

    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-tx</artifactId>
    <version>4.1.0.RELEASE</version>
    </dependency>

    感觉解决这种问题的话[stackoverflow](http://stackoverflow.com/search?q=%5Bhttp%3A%2F%2Fwww.springframework.org%2Fschema%2Ftx)是更好的选择~
    feilaoda
        6
    feilaoda  
       2015-02-05 11:42:45 +08:00
    @pohvii
    @gaocegege 是的,这个错误是少spring-tx类库,加上就ok了
    feilaoda
        7
    feilaoda  
       2015-02-05 11:54:52 +08:00
    @pohvii
    错了, spring-orm里面是依赖spring-tx的,所以不需要加
    那就是 mvn package dependency:copy-dependencies 加上dependency:copy-dependencies参数,并且运行时需要加上classpath
    java -jar xxx.jar -classpath "xxxx/*"
    pohvii
        8
    pohvii  
    OP
       2015-02-09 15:35:22 +08:00
    @gaocegege
    就是解决不了才问的啊
    @feilaoda
    我把工程的demo放到百度云了,http://pan.baidu.com/s/1pJPuDYj,希望能看看
    另外用mvn package dependency:copy-dependencies 打包的话已经把依赖jar打包进去了,应该不需要再加-classpath参数
    我进入target目录后用 java -jar xxx.jar -classpath "dependency/*" 运行还是不行
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2760 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 11:44 · PVG 19:44 · LAX 03:44 · JFK 06:44
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.