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
reeco 2015-02-05 10:31:29 +08:00 via iPhone
看情况像是maven打包时没把依赖的spring一起放进去,你解压下jar包检查下spring有没有在里面
|
2
feilaoda 2015-02-05 10:49:01 +08:00
既然用maven了,怎么不用mvn package打包?
你的pom.xml没贴全,不好评价是否有问题。 |
4
pohvii OP @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> |
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)是更好的选择~ |
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/*" |
8
pohvii OP @gaocegege
就是解决不了才问的啊 @feilaoda 我把工程的demo放到百度云了,http://pan.baidu.com/s/1pJPuDYj,希望能看看 另外用mvn package dependency:copy-dependencies 打包的话已经把依赖jar打包进去了,应该不需要再加-classpath参数 我进入target目录后用 java -jar xxx.jar -classpath "dependency/*" 运行还是不行 |