运行Spring应用程序作为jar,找不到bean

时间:2014-06-05 19:00:45

标签: java eclipse spring hibernate jar

使用eclipse时,我没有错误,但是一旦我将项目导出为可运行的jar文件并尝试使用java -jar myjar.jar运行它,它就会给我这个错误。

Jun 05, 2014 1:46:22 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing     org.springframework.context.support.ClassPathXmlApplicationContext@112f8578: startup date     [Thu Jun 05 13:46:22 CDT 2014]; root of context hierarchy
Jun 05, 2014 1:46:22 PM     org.springframework.beans.factory.support.DefaultListableBeanFactory     preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@56e3cbb9: defining beans []; root of factory hierarchy
Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named   'jobTaskService'is defined 
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:575) 
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1114)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:279)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:198)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1121)
at com.tw.JobQueue.App.main(App.java:22)

但是,当我使用Eclipse时,不会发生此错误。

以下是我的一些代码

的applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" 
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" 
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd">

<context:component-scan base-package="com.tw.JobQueue" />
<context:annotation-config />

<tx:annotation-driven />

<bean id="emailService" class="com.tw.JobQueue.service.EmailService">
</bean>

<bean id="emailJob" name="EmailJob" class="com.tw.JobQueue.job.EmailJob" scope="prototype">
    <property name="emailService" ref="emailService" />
</bean>

<bean id = "jobWorker" class="com.tw.JobQueue.job.JobWorker" scope="prototype">
</bean>

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/test" />
    <property name="username" value="root" />
    <property name="password" value="root" />
</bean>

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource"></property>
    <property name="annotatedClasses">
        <list>
            <value>com.tw.JobQueue.model.JobLog</value>
            <value>com.tw.JobQueue.model.JobTask</value>
            <value>com.tw.JobQueue.model.JobType</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
            <prop key="hibernate.show_sql">true</prop>
        </props>
    </property>
</bean>

<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
    <property name="host" value="smtp.gmail.com" />
    <property name="port" value="587" />
    <property name="username" value="zacharyphilley@gmail.com" />
    <property name="password" value="password" />
    <property name="javaMailProperties">
        <props>
            <prop key="mail.smtp.auth">true</prop>
            <prop key="mail.smtp.starttls.enable">true</prop>
            <prop key="mail.smtp.ssl.trust">smtp.gmail.com</prop>
        </props>
    </property>
</bean>

<bean id="transactionManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager"
    p:sessionFactory-ref="sessionFactory">
</bean>

public class App {

public static void main(String[] args) {

    int numThreads = 10;

    if(args.length > 0){
        numThreads = Integer.parseInt(args[0]);
    }

    ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("classpath*:**/applicationContext*.xml");

    IJobTaskService jobTaskService = context.getBean("jobTaskService", IJobTaskService.class);

JobTaskService.java

@Service("jobTaskService")
public class JobTaskService implements IJobTaskService {

非常感谢任何和所有帮助。

**********************编辑已解决*********************** ***********************

事实证明,Eclipse的创建jar的方法与我使用maven,hibernate和spring的设置并不是很兼容。此设置的最佳选择是使用one-jar maven插件并按照其网站上的说明操作: https://code.google.com/p/onejar-maven-plugin/

这个插件将创建jar文件,就像在eclipse maven项目中一样运行。

无论如何,感谢大家帮助解决我的问题。

2 个答案:

答案 0 :(得分:0)

应用程序上下文xml通常应放在包层次结构的根文件夹中。所以包装结构应该是这样的。

myjar.jar/applicationContext.xml 

如果你想把它放在资源文件夹下,那么请参考它...就像... 如果applicationContext.xml文件在resources文件夹中,则可以通过类路径访问它,如此

classpath:resources/applicationContext.xml

答案 1 :(得分:0)

我有同样的问题,在eclipse工作正常,但是当我导出JAR时它给了我这个错误:

引起:org.springframework.beans.factory.NoSuchBeanDefinitionException:没有类型的唯一bean [MyBean]已定义:预期的单个bean但找到0:

解决问题我必须声明bean

<bean id="odiAuxDAO" class="<my_package>.OdiAuxDAO"/>

insted of component-scan

<context:component-scan base-package="com.<my_package>" />