spring项目打成可运行的jar后不能运行问题

J2EE 码拜 9年前 (2015-04-02) 971次浏览 0个评论
 

当前spring3.2版本
项目一共2个类
Main
读取配置,获取service对象,调用方法。
Service
一个方法,简单输出。

2种方式,在eclipse中直接运行main都没问题

方式1:
使用配置式,把bean用xml方式写在配置文件中
把项目打成jar包后,直接运行,从main中可以获取service并且调用成功

方式2:

是不是spring注解在jar里面不能被识别?

使用注解式,在Service中使用@Component
把项目打成jar包后,不能运行,错误信息为:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [a.Serv] is defined: expected single bean but found 0:
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:271)
        at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1101)
        at a.Main.main(Main.java:32)
        ... 5 more
spring项目打成可运行的jar后不能运行问题
26分
把你的代码和xml都贴出来。
spring项目打成可运行的jar后不能运行问题

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;


public class Main {
	private static ApplicationContext context = null;
	static final String[] path={"classpath:applicationContext.xml"};
    static final String[] classpath={"applicationContext.xml"};
    
    private static void init(){
        try {
            context = new FileSystemXmlApplicationContext(path);
        } catch (Exception e) {
            try {
                context = new FileSystemXmlApplicationContext(classpath);
            } catch (Exception e2) {
                try {
                    context = new ClassPathXmlApplicationContext(classpath);
                } catch (Exception e3) {
                    e3.printStackTrace();
                }
            }
        }
    }
    
	public static void main(String[] args) {
		init();
		System.out.println(context==null);
		((Serv)context.getBean(Serv.class)).t();
	}
}




import org.springframework.stereotype.Component;



@Component
public class Serv {
	public void t(){
		System.out.println("sssssss");
	}
}


application.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
	xmlns:aop="http://www.springframework.org/schema/aop"  
	xsi:schemaLocation="  
	 	http://www.springframework.org/schema/aop    
  		http://www.springframework.org/schema/aop/spring-aop-2.0.xsd   
        http://www.springframework.org/schema/beans   
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
        http://www.springframework.org/schema/context   
        http://www.springframework.org/schema/context/spring-context-3.0.xsd  
        http://www.springframework.org/schema/tx   
        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  
        http://www.springframework.org/schema/jdbc   
        http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd  
        http://www.springframework.org/schema/data/jpa   
        http://www.springframework.org/schema/data/jpa/spring-jpa.xsd  
        http://www.springframework.org/schema/jee   
        http://www.springframework.org/schema/jee/spring-jee-3.0.xsd  
        ">


	<context:annotation-config />
	<aop:aspectj-autoproxy/>
	<context:component-scan base-package="a" >
	</context:component-scan>


	<!-- <bean id="serv" class="a.Serv"></bean> -->


</beans>
spring项目打成可运行的jar后不能运行问题
我试过了。没有问题啊。可以运行

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:aop="http://www.springframework.org/schema/aop"  
    xsi:schemaLocation="  
         http://www.springframework.org/schema/aop    
          http://www.springframework.org/schema/aop/spring-aop-2.0.xsd   
        http://www.springframework.org/schema/beans   
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
        http://www.springframework.org/schema/context   
        http://www.springframework.org/schema/context/spring-context-3.0.xsd  
        http://www.springframework.org/schema/tx   
        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  
        http://www.springframework.org/schema/jdbc   
        http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd  
        http://www.springframework.org/schema/data/jpa   
        http://www.springframework.org/schema/data/jpa/spring-jpa.xsd  
        http://www.springframework.org/schema/jee   
        http://www.springframework.org/schema/jee/spring-jee-3.0.xsd  
        ">
 
 
    <context:annotation-config />
    <aop:aspectj-autoproxy/>
    <context:component-scan base-package="com.wanmei.spring.test" >
    </context:component-scan>
     
     
    <!-- <bean id="serv" class="a.Serv"></bean> -->
     
     
</beans>


package com.wanmei.spring.test;

import org.springframework.stereotype.Component;

@Component
public class Serv {
    public void t(){
        System.out.println("sssssss");
    }
}


package com.wanmei.spring.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class Bootstrap {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		String configLocation = "E:/workspace_java/test/src/com/wanmei/spring/test/applicationContext.xml";
		ApplicationContext context = new FileSystemXmlApplicationContext(configLocation);
		context.getBean(Serv.class).t();
	}

}

spring项目打成可运行的jar后不能运行问题
我是用eclipse导出的,export->runnable->package required
你是这种么
spring项目打成可运行的jar后不能运行问题
引用 4 楼 student_2008 的回复:

我是用eclipse导出的,export->runnable->package required
你是这种么

嗯。我照着你说的样子做了测试。确实,存在你说的问题。具体问题我还没有找到。
不过,正如你说的:在eclipse中运行是正常的,eclipse导出之后就有问题了。应该是在eclipse运行时存在某个“eclipse的环境因素”,程序可以正常运行。经过eclipse导出之后,这个“环境因素”丢失了,就无法运行了。或者是eclipse的导出存在问题,也会导致这样的问题。
我也没有找到问题。如果你很着急的话,还是用spring的xml里面进行bean的配置吧,不采用“注释”的方式。
如果你的问题解决了,请在这里跟帖。我也很想知道为什么。谢谢!


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明spring项目打成可运行的jar后不能运行问题
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!