我有一個基於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
貴'TestJMSProxyJar'項目有自己的'applicationContext.xml'? –
也發佈你的jar文件的內容,它的目錄結構。 –
TestProxy(TestJMSProxyJar)是非Spring項目,我使用PTSJMSProxy jar – bubai