日期:2014-05-18  浏览次数:20629 次

spring项目打成可运行的jar后不能运行问题
本帖最后由 student_2008 于 2013-11-29 13:02:20 编辑
当前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

------解决方案--------------------
把你的代码和xml都贴出来。