2013-12-17 26 views
3

我有一个基于Spring的独立项目(PTSJMSProxy)。我指http://sahits.ch/blog/?p=2326没有定义类型的唯一bean:期望单个bean,但找到0:

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> 

现在,当我调用Test.java从Eclipse的运行方式-Java应用我得到预期的出来放。

输出 - 你好春天DI服务!

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

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

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

我的目标是从非弹簧java项目中使用这个罐子

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

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

在该项目中,我添加所有所需的弹簧罐和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是一个基于Spring项目 - 它有自己的applicationContext 。XML(参考点无-5)

TestProxy是一个非的Spring Java项目 - 在这里我使用PTSJMSProxy

PTSJMSProxy Jar folder structure 

PTSJMSProxy罐子包含COM,META-INF和的applicationContext。相同级别的xml

+0

贵'TestJMSProxyJar'项目有自己的'applicationContext.xml'? –

+0

也发布你的jar文件的内容,它的目录结构。 –

+0

TestProxy(TestJMSProxyJar)是非Spring项目,我使用PTSJMSProxy jar – bubai

回答

2

问题已解决。

这是由于弹簧配置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); 
0

有时当你定义basePackages错误内@ComponentScan注解喜欢它也会发生:

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

这里以上人会认为是单包,如果有多个软件包在您的项目中扫描,那么你必须提到像String[]

@ComponentScan({ "com.whodesire.model" , "com.whodesire.util" }) 
相关问题