没有定义类型的唯一bean:期望的单个bean但找到0:

时间:2013-12-17 18:28:25

标签: spring autowired

我有一个基于弹簧的独立项目(PTSJMSProxy)。我引用了http://sahits.ch/blog/?p=2326

PTSJMSProxy 中,我有以下内容。

1)SimpleWriterService.java

package com.test;

import org.springframework.stereotype.Service;

@Service
public class SimpleWriterService {

    public void sayHello() {
        System.out.println("Hello Spring DI service!");
    }
}

2)ComponentConsumer.java

 package com.test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ComponentConsumer {

    @Autowired
    private SimpleWriterService service;

    public void consume() {

        service.sayHello();
    }

}

3)ProxyJMSClient.java

    package com.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ProxyJMSClient {

// I commented some portions,but working fine
// Example  @Autowired and also in the constructure  


    // @Autowired 
    private ComponentConsumer consumer;

    public ProxyJMSClient() {

        ApplicationContext context = new ClassPathXmlApplicationContext(
                "applicationContext.xml");
        // AutowireCapableBeanFactory acbFactory =
        // context.getAutowireCapableBeanFactory();
        // acbFactory.autowireBean(this);

        consumer = context.getBean(ComponentConsumer.class);
    }

    public void callMyJMSClient() {
        this.consumer.consume();
    }

}

4)Test.java

 package com.test;

public class Test {

    public static void main(String[] args) {

        (new ProxyJMSClient()).callMyJMSClient();
    }

}

5)applicationContext.xml

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

    <tx:annotation-driven />
    <context:annotation-config />
    <context:component-scan base-package="com.test"  />

</beans>

现在当我从eclipse Run As -Java应用程序调用Test.java时,我得到了预期的输出。

输出 - Hello Spring DI服务!

=============================================== ==============================

现在我创建了Jar导出为Jar的Jar。罐子名称 - PTSJMSProxy.jar

=============================================== ================================

我的目标是使用非spring java项目中的jar

=============================================== ================================

我在eclipse中创建了另一个java项目“ TestProxy

在该项目中,我添加了所有必需的Spring Jar和PTSJMSProxy.jar

创建了TestJMSProxy.java类

 package com.proxy.test;

    import com.wiley.fts.ProxyJMSClient;

    public class TestJMSProxy {

        public static void main(String[] args) {
            (new ProxyJMSClient()).callMyJMSClient();
        }
    }

当我跑步时 - 我得到以下例外

 log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.test.ComponentConsumer] is defined: expected single bean but found 0: 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:269)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1083)
    at com.wiley.fts.ProxyJMSClient.<init>(ProxyJMSClient.java:19)
    at com.proxy.test.TestJMSProxyJar.main(TestJMSProxyJar.java:8)

如何解决此问题

注意: -

PTSJMSProxy 是一个基于弹簧的项目 - 它有自己的 applicationContext.xml (参考点-5)

TestProxy 是一个非Spring Java项目 - 我使用 PTSJMSProxy Jar

PTSJMSProxy Jar folder structure

PTSJMSProxy jar包含相同级别的com,META-INF和applicationContext.xml

2 个答案:

答案 0 :(得分:2)

问题已解决。

这是由于spring配置xml文件的加载问题。

代码

String fileUrl = PTSJMSProxyClient.class.getClassLoader()
                .getResource(SPRING_JMS_CFG_FILE).toString();

        LOG.info("Spring jmsContext.xml file path :" +fileUrl);

        xmlApplicationContext = new ClassPathXmlApplicationContext(fileUrl);



        AutowireCapableBeanFactory acbFactory = xmlApplicationContext
                .getAutowireCapableBeanFactory();
        acbFactory.autowireBean(this);

        client = xmlApplicationContext.getBean(MessageSenderImpl.class);

答案 1 :(得分:0)

有时在 basePackages 注释中错误地定义 @ComponentScan 时会发生这种情况,如:

@ComponentScan("com.whodesire.model", "com.whodesire.util")

此处上面的内容将被视为单个包,如果您的项目中有多个要扫描的包,那么您必须提及 String[]

等包
@ComponentScan( { "com.whodesire.model" , "com.whodesire.util" } )