繁体   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