簡體   English   中英

自動裝配Spring 3.1獨立應用程序,返回“未定義[xxxx]類型的唯一bean”異常

[英]Autowiring a Spring 3.1 standalone application returning “No unique bean of type [x.x.x.x] is defined” exception

我正在編寫一個獨立的應用程序(不是基於Web的應用程序),它將使用以下命令從命令提示符處運行:

java -jar myapplication.jar 

我在eclipse上將其開發為Maven項目,因此Eclipse檢索了所有依賴庫。 如果我右鍵單擊主類,然后選擇“運行方式”>“ Java應用程序”,則可以正常運行。

現在,我的問題是我需要將應用程序部署為單個jar文件。 為此,我使用Eclipse將應用程序導出為“ Runnable Jar”(即通過“ export命令”)。 這生成了jar文件。 我查看了jar文件,所有類和相關的jar文件都在jar文件中。 Spring應用程序上下文文件也位於頂級文件夾的jar文件中。 jar文件的“內部”如下所示:

- com
    - myapp
      - service
         - MyAppService.class
      - dao
         - MyAppDataDao.class   
      - MyMainClass.class


- META-INF
- org
- application-context-components.xml
- log4j.properties
- [several jar files for spring, log4j, velocity etc)

我嘗試使用以下命令運行jar文件,它給了我這個錯誤:

Exception in thread "main" java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.myapp.MyMainClass] is defined: expected single bean but found 0:
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:257)
        at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1012)
        at com.myapp.MyMainClass.init(MyMainClass.java:44)
        at com.myapp.MyMainClass.main(MyMainClass.java:65)
        ... 5 more

com.myapp.MyMainClass文件位於jar文件中,具有正確的名稱。 包中的類是自動裝配的。 我認為我一定錯過了注釋中的某些內容,或者可能是應用程序上下文文件中的某些內容。 類的結構和使用的注釋如下所示:

MyMainClass

@Component
public class MyMainClass{

    @Autowired
    MyAppService myAppService;

    public static void main(String args[]){
        try{
            context = new     ClassPathXmlApplicationContext(properties.get("app.context"));

 MyMainClass mymainClass = context.getBean(MyMainClass.class);
 mymainClass.myAppService.getData()....
 ....
            }catch(Exception e){
                throw new CWAException(fname + ":" + e);
            }
        }
    }

app.context屬性返回應用程序上下文文件的名稱。

MyAppService

@Service
public class MyAppService{

    @Autowired
    MyAppDataDao myAppDataDao;

    ---
    ---
    ---
}

MyAppDataDao

@Repository("myAppDataDao;")
public class MyAppDataDao {

    getData(){
    }

    ---
    ---
    ---
}

應用程序上下文文件如下所示

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="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">

    <!-- Auto scan the components -->
    <context:component-scan base-package="com.myapp" />

    <bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean" 
          p:resourceLoaderPath="file://C:\template" 
          p:preferFileSystemAccess="true"/>  

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName"><value>oracle.jdbc.driver.OracleDriver</value></property>
        <property name="url"><value>jdbc:oracle:thin:@x.x.x.x:x</value></property>
        <property name="username"><value>xxx</value></property>
        <property name="password"><value>xxx</value></property>
    </bean>              
</beans>

查看該錯誤,我猜想自動布線沒有啟動,但我無法弄清楚配置中的錯誤。 該應用程序位於打包的jar文件中,我正在使用ClassPathXmlApplicationContext加載該文件,因此它應該找到它。 我也不明白為什么它可以在eclipse上運行,但是在導出后卻無法運行。

有任何想法嗎?

Spring3.0 atleast中的PathMatchingResourcePatternResolver類不會在jar中搜索標記有自動裝配注釋的類。

將jar所在的目錄添加到類路徑。

它將檢測類並作為bean加載。

暫無
暫無

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

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