No unique bean of type is defined: expected single bean but found 0

转载 2015年07月10日 19:09:44


http://stackoverflow.com/questions/20641932/no-unique-bean-of-type-is-defined-expected-single-bean-but-found-0


I have one spring based standalone project (PTSJMSProxy). I refer http://sahits.ch/blog/?p=2326

In the PTSJMSProxy I have followings.

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>

Now when I invoke Test.java from eclipse Run As -Java Application I get the expected out put.

Output - Hello Spring DI service!

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

Now I created the Jar with Eclipse export as Jar. Jar Name -PTSJMSProxy.jar

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

My objective is to use this jar from a non spring java project

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

I created another java project in eclipse "TestProxy"

In that project I add all the required Spring Jar and PTSJMSProxy.jar

Created TestJMSProxy.java class

 package com.proxy.test;

    import com.wiley.fts.ProxyJMSClient;

    public class TestJMSProxy {

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

When I run - I get following exceptions

 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)

How can I resolve this

NOTE: -

PTSJMSProxy is a spring based project - which has its own applicationContext.xml (Refer point no -5)

TestProxy is a NON Spring Java project - where I use PTSJMSProxy Jar

PTSJMSProxy Jar folder structure

PTSJMSProxy jar contains com,META-INF and applicationContext.xml under same level

shareimprove this question
 
 
Does your TestJMSProxyJar project have its own applicationContext.xml? –  Sotirios Delimanolis Dec 17 '13 at 19:06
 
Also post the contents of your jar file, its directory structure. –  Sotirios Delimanolis Dec 17 '13 at 19:49
 
TestProxy(TestJMSProxyJar)is Non Spring project where I use PTSJMSProxy jar –  bubai Dec 18 '13 at 6:33

The problem is resolved.

This is due to the loading problem of spring configuration xml file.

Code

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

spring依赖注入单元测试:expected single matching bean but found 2

异常信息:org.springframework.beans.factory.UnsatisfiedDependencyException:Caused by: org.springframework...
  • hjjzhangkui
  • hjjzhangkui
  • 2010年12月03日 11:22
  • 20765

expected single matching bean but found 2

//代码片段 @Autowired private CrmUserForCoopAgent crmUserForCoopAgent; 错误信息:org.springframewor...
  • kirrin
  • kirrin
  • 2015年08月21日 11:33
  • 14906

使用dubbo对外暴露接口,实现类同时实现两个接口后 @Autowire失败,提示expected single matching bean but found 2解决方案

当使用dubbo对外暴露接口时,为了方便继承和实现底层方法,另一方面专门对外提供一套接口方法,这时接口实现类同时实现了两个接口: @Service("ICentAccountInfoService")...
  • paradise003
  • paradise003
  • 2016年06月02日 17:06
  • 7919

No qualifying bean of type is defined: expected single matching bean but found 2

最近在Spring的注解中,尝试获取一个Bean出现了如题所示的错误。 NoUniqueBeanDefinitionException: No qualifying bean of type is ...
  • qq_18875541
  • qq_18875541
  • 2017年04月06日 16:08
  • 1127

paip . 解决spring No unique bean of type [com.mijie.homi.search.service.index.MoodUserIndexService]

paip . 解决spring No unique bean of type   [com.mijie.homi.search.service.index.MoodUserIndexService] ...
  • attilax
  • attilax
  • 2014年01月10日 16:09
  • 5428

使用dubbo对外暴露接口,实现类同时实现两个接口后 @Autowire失败,提示expected single matching bean but found 2解决方案

当使用dubbo对外暴露接口时,为了方便继承和实现底层方法,另一方面专门对外提供一套接口方法,这时接口实现类同时实现了两个接口: @Service("ICentAccountInfoService")...
  • paradise003
  • paradise003
  • 2016年06月02日 17:06
  • 7919

Spring开发常见异常及原因列表

异常:Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire method: pu...
  • lxb_champagne
  • lxb_champagne
  • 2013年08月22日 10:25
  • 4708

spring对redis的操作

spring对redis的操作spring对redis的基本操作包括添加string对象,hash对象,list对象,删除key,记录。import java.util.UUID; import...
  • khuangliang
  • khuangliang
  • 2016年12月15日 18:19
  • 516

深入浅出JMS(四)--Spring和ActiveMQ整合的完整实例

第一篇博文深入浅出JMS(一)–JMS基本概念,我们介绍了JMS的两种消息模型:点对点和发布订阅模型,以及消息被消费的两个方式:同步和异步,JMS编程模型的对象,最后说了JMS的优点。 第二篇博文深...
  • u014756827
  • u014756827
  • 2016年09月06日 13:47
  • 816

Spring Quartz 定时器任务

Spring Quartz 定时器任务本文介绍了Spring Quartz使用定时器任务的2种方式 基于XML文件的方式 基于注解的方式 Spring Quartz部分有如下一些核心概念: Sched...
  • upshi
  • upshi
  • 2017年02月27日 20:29
  • 415
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:No unique bean of type is defined: expected single bean but found 0
举报原因:
原因补充:

(最多只允许输入30个字)