关闭

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

881人阅读 评论(0) 收藏 举报
分类:


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

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:15406次
    • 积分:243
    • 等级:
    • 排名:千里之外
    • 原创:8篇
    • 转载:11篇
    • 译文:0篇
    • 评论:1条
    文章分类
    最新评论