我定制了一个spring xd处理器模块,尝试使用spring data elasticsearch链接到我的模块中的elasticsearch。 unittest没问题,但是当在单节点中部署时,抛出异常:
- Exception encountered during context initialization - cancelling refresh attempt
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.integration.
config.TransformerFactoryBean#0': Cannot create inner bean 'com.eheluo.bigdata.wenxin.ToElasticsearch#0' of type [com.eh
eluo.bigdata.wenxin.ToElasticsearch] while setting bean property 'targetObject'; nested exception is org.springframework
.beans.factory.BeanCreationException: Error creating bean with name 'com.eheluo.bigdata.wenxin.ToElasticsearch#0': Injec
tion of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Coul
d not autowire field: private org.springframework.data.elasticsearch.core.ElasticsearchTemplate com.eheluo.bigdata.wenxi
n.ToElasticsearch.elasticsearchTemplate; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionExcep
tion: No qualifying bean of type [org.springframework.data.elasticsearch.core.ElasticsearchTemplate] found for dependenc
y: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.spr
ingframework.beans.factory.annotation.Autowired(required=true)}
找不到bean org.springframework.data.elasticsearch.core.ElasticsearchTemplate。
我的pom文件是:
<parent>
<groupId>org.springframework.xd</groupId>
<artifactId>spring-xd-module-parent</artifactId>
<version>1.1.1.RELEASE</version>
<relativePath></relativePath>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
<version>1.2.1.RELEASE</version>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</exclusion>
</exclusions>
</dependency>
....
我的自定义模块代码是:
@Component
public class ToElasticsearch {
private Logger logger = Logger.getLogger(ToElasticsearch.class);
@Autowired
private ElasticsearchTemplate elasticsearchTemplate;
public String process(NewDataInterface data){
NewData newData = new NewData();
BeanUtils.copyProperties(data, newData);
String docid = elasticsearchTemplate.index(genIndexQuery(newData));
logger.debug("save to es");
return docid;
}
我的application.properties是:
# options_class=com.eheluo.bigdata.wenxin.ToElasticsearch
# ELASTICSEARCH (ElasticsearchProperties})
# The cluster name (defaults to elasticsearch)
spring.data.elasticsearch.cluster-name=elasticsearch
# The address(es) of the server node (comma-separated; if not specified starts a client node)
spring.data.elasticsearch.cluster-nodes=127.0.0.1:9300
# if spring data repository support is enabled
spring.data.elasticsearch.repositories.enabled=true
我的module.properties没有导出选项 我的module.xml是:
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/integration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:elasticsearch="http://www.springframework.org/schema/data/elasticsearch"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/data/elasticsearch http://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch-1.0.xsd
">
<channel id="input"/>
<transformer input-channel="input" output-channel="output">
<beans:bean class="com.eheluo.bigdata.wenxin.ToElasticsearch" />
</transformer>
<channel id="output"/>
</beans:beans>
我的单位测试是:
public class ToElasticsearchTest {
private static SingleNodeApplication application;
private static int RECEIVE_TIMEOUT = 5000;
private static String moduleName = "toelasticsearch";
@BeforeClass
public static void setUp() {
RandomConfigurationSupport randomConfigSupport = new RandomConfigurationSupport();
application = new SingleNodeApplication().run();
SingleNodeIntegrationTestSupport singleNodeIntegrationTestSupport = new SingleNodeIntegrationTestSupport(application);
singleNodeIntegrationTestSupport.addModuleRegistry(new SingletonModuleRegistry(ModuleType.processor, moduleName));
}
@Test
public void test() {
String streamName = "toElasticsearchTest";
String newID = UUID.randomUUID().toString();
NewDataInterface newData = new NewDataPOJO();
newData.setsomthing("testtesttest");
newData.setId(newID);
Multiset<String> words = ConcurrentHashMultiset.create();
words.add("1");
words.add("2");
words.add("3");
words.add("4", 2);
newData.setsomthinx(words);
String processingChainUnderTest = moduleName;
SingleNodeProcessingChain chain = chain(application, streamName, processingChainUnderTest);
chain.sendPayload(newData);
String result = (String) chain.receivePayload(RECEIVE_TIMEOUT);
assertEquals(newID, result);
chain.destroy();
}
}
单元测试总是正常:测试运行:1,失败:0,错误:0,跳过:0
但无法在单节点rutime中工作。
为什么它在运行时找不到ElasticsearchTemplate?