簡體   English   中英

以jar形式運行Spring應用,找不到bean

[英]Running Spring app as jar, beans not found

使用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的設置不太兼容。 此設置的最佳選擇是使用單罐Maven插件,並按照其網站上的說明進行操作: https : //code.google.com/p/onejar-maven-plugin/

該插件將創建運行在Eclipse Maven項目中的jar文件。

無論如何,感謝大家在幫助解決我的問題方面的投入。

通常應將應用程序上下文xml放在程序包層次結構的根文件夾中。 因此,包裝結構應如下所示。

myjar.jar/applicationContext.xml 

如果您希望將其放在資源文件夾下,則應完全一樣地引用它。例如,如果applicationContext.xml文件位於資源文件夾中,則可以通過這樣的類路徑對其進行訪問

classpath:resources/applicationContext.xml

我有同樣的問題,在eclipse中工作正常,但是當我導出JAR時,它給了我這個錯誤:

由以下原因引起:org.springframework.beans.factory.NoSuchBeanDefinitionException:未定義[MyBean]類型的唯一bean:預期為單個bean,但找到0:

解決我不得不聲明豆子的問題

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

安裝了組件掃描

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM