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

I am writing a standalone application (not web based) which will be run from the command prompt using the command: 我正在编写一个独立的应用程序(不是基于Web的应用程序),它将使用以下命令从命令提示符处运行:

java -jar myapplication.jar 

I developed the application on eclipse as a Maven project so Eclipse retrieves all the dependant libraries. 我在eclipse上将其开发为Maven项目,因此Eclipse检索了所有依赖库。 If i right click the main class and select "Run As ">"Java Application" it works fine. 如果我右键单击主类,然后选择“运行方式”>“ Java应用程序”,则可以正常运行。

Now the problem I have is I need to deploy the application as a single jar file. 现在,我的问题是我需要将应用程序部署为单个jar文件。 To do this, I used Eclipse to export the application as a "Runnable Jar" (ie via the "export command"). 为此,我使用Eclipse将应用程序导出为“ Runnable Jar”(即通过“ export命令”)。 This generated the jar file. 这生成了jar文件。 I looked into the jar file and all the classes and the dependent jar files are in the jar file. 我查看了jar文件,所有类和相关的jar文件都在jar文件中。 The Spring application context file is also in the jar file in the top level folder. Spring应用程序上下文文件也位于顶级文件夹的jar文件中。 The "inside" of the jar file looks like this: 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)

I tried running the jar file using the following command and it gave me this error: 我尝试使用以下命令运行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

The file com.myapp.MyMainClass is in the jar file with the correct name. com.myapp.MyMainClass文件位于jar文件中,具有正确的名称。 The classes in the package are autowired. 包中的类是自动装配的。 I think that i must have missed something in the annotations or maybe something in the application context file. 我认为我一定错过了注释中的某些内容,或者可能是应用程序上下文文件中的某些内容。 The structure of the classes and the annotations used is shown below: 类的结构和使用的注释如下所示:

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);
            }
        }
    }

The app.context property returns the name of the application context file. app.context属性返回应用程序上下文文件的名称。

MyAppService MyAppService

@Service
public class MyAppService{

    @Autowired
    MyAppDataDao myAppDataDao;

    ---
    ---
    ---
}

MyAppDataDao MyAppDataDao

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

    getData(){
    }

    ---
    ---
    ---
}

The application context file looks like this 应用程序上下文文件如下所示

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

Looking at the error I would guess that the Autowiring is not kicking in but I cant figure out where in the configuration I got it wrong. 查看该错误,我猜想自动布线没有启动,但我无法弄清楚配置中的错误。 The application is in the packaged jar file and I am loading the file using ClassPathXmlApplicationContext so it should find it. 该应用程序位于打包的jar文件中,我正在使用ClassPathXmlApplicationContext加载该文件,因此它应该找到它。 I also don't understand why it works on eclipse but not after it has been exported. 我也不明白为什么它可以在eclipse上运行,但是在导出后却无法运行。

Any ideas? 有任何想法吗?

The class PathMatchingResourcePatternResolver in Spring3.0 atleast does not search for the classes marked with autowiring annotation inside jars. Spring3.0 atleast中的PathMatchingResourcePatternResolver类不会在jar中搜索标记有自动装配注释的类。

Add the directory in which the jar is present to the classpath. 将jar所在的目录添加到类路径。

It will detect the classes and load as beans. 它将检测类并作为bean加载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Spring 3.1.x和MockMvc模拟/自动装配bean - Mocking/autowiring beans with Spring 3.1.x and MockMvc 没有定义名为[x]的bean - No bean named [x] is defined 无法连接到SMTP主机:xxxx,端口:java中的25 - Could not connect to SMTP host: x.x.x.x, port: 25 in java Spring 3.1.x里程碑存储库 - Spring 3.1.x milestone repository Spring 3 MVC + Hibernate 3.5.x:SessionFactory的自动装配和依赖注入 - Spring 3 MVC + Hibernate 3.5.x : Autowiring and Dependency Injection of SessionFactory 嵌套类中的spring Bean Overriding 2.1.x @Configuration创建@Bean创建失败,并且&#39;已经定义了具有该名称的bean&#39; - spring Bean Overriding 2.1.x in nested class @Configuration @Bean creation fails with 'A bean with that name has already been defined' java.security.cert.CertificateException:找不到与IP地址xxxx匹配的使用者替代名称 - java.security.cert.CertificateException: No subject alternative names matching IP address x.x.x.x found x的表达式被装入X? - The expression of type x is boxed into X? Spring 错误“名为 'x' 的 Bean 应为 'y' 类型,但实际上是 [com.sun.proxy.$Proxy] 类型” - Spring error “Bean named 'x' is expected to be of type 'y', but was actually of type [com.sun.proxy.$Proxy]” Spring 3.x + SimpleJdbcTemplate:返回多列 - Spring 3.x + SimpleJdbcTemplate : Returning multiple columns
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM