【问题标题】:Running Spring app as jar, beans not found将 Spring 应用程序作为 jar 运行,找不到 bean
【发布时间】:2014-07-26 22:01:20
【问题描述】:

使用 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 项目中一样运行。

无论如何,感谢大家为帮助解决我的问题提供的意见。

【问题讨论】:

  • jar 是否包含上下文文件?
  • 是的,它在 myjar.jar/recources/applicationContext.xml 中
  • correc包中的类是com.tw.JobQueue吗?
  • 此项目中的所有类要么在 com.tw.JobQueue 中,要么在 com.tw.JobQueue 中的一个包中(例如 com.tw.JobQueue.dao)

标签: java eclipse spring hibernate jar


【解决方案1】:

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

myjar.jar/applicationContext.xml 

如果您希望将其放在资源文件夹下,请完全按此方式引用它。例如 如果 applicationContext.xml 文件在资源文件夹中,则可以像这样通过类路径访问它

classpath:resources/applicationContext.xml

【讨论】:

    【解决方案2】:

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

    原因:org.springframework.beans.factory.NoSuchBeanDefinitionException:没有唯一的bean类型 [MyBean] 已定义:预期为单个 bean,但发现为 0:

    为了解决我必须声明 bean 的问题

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

    组件扫描

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-28
      • 1970-01-01
      • 2017-01-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多