0

我正在尝试使用 Weld SE 在 SE 应用程序中注入 Spring 数据存储库(在本例中为 Elasticsearch)。

存储库

public interface EmployeeRepository extends ElasticsearchRepository<Employee, String> {
List<Employee> findEmployeesByAge(int age);
List<Employee> findEmployeesByName(String name);
List<Employee> findEmployeesBySkillsIn(List skills);
}

定义 ElasticsearchTemplate:

@ApplicationScoped
public class ElasticsearchTemplateProducer {

@Produces
public ElasticsearchOperations createElasticsearchTemplate() {

    Client client = null;

    try {
        client = TransportClient.builder().build()
                .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));
    } catch (UnknownHostException e) {
        e.printStackTrace();
    }

    return new ElasticsearchTemplate(client);
}

}

Main(应用程序使用 org.jboss.weld.environment.se.StartMain 执行)很简单:

public class Main {

@Inject
private Service repository;

public static void main(@Observes ContainerInitialized event){
}
}

依赖项是:

<dependencies>
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-elasticsearch</artifactId>
        <version>2.0.2.RELEASE</version>
        <exclusions>
            <exclusion>
                <artifactId>spring-context</artifactId>
                <groupId>org.springframework</groupId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.jboss.weld.se</groupId>
        <artifactId>weld-se-core</artifactId>
        <version>2.3.2.Final</version>
    </dependency>
    <dependency>
        <groupId>org.jboss</groupId>
        <artifactId>jandex</artifactId>
        <version>1.2.2.Final</version>
    </dependency>
</dependencies>

我已经排除了 spring 上下文,但无论我是否排除上下文,我都会得到相同的异常。

在资源\META_INF\beans.xml 中:

<?xml version="1.0" encoding="UTF-8"?>
   <beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
                       http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
   version="1.1" bean-discovery-mode="all"
   xmlns:weld="http://jboss.org/schema/weld/beans">
<weld:scan>
    <weld:exclude name="org.jboss.weld.**" />
</weld:scan>

抛出的异常是:

javax.enterprise.inject.UnsatisfiedResolutionException: Unable to resolve a bean for 'org.springframework.data.elasticsearch.core.ElasticsearchOperations' with qualifiers [@javax.enterprise.inject.Default(), @javax.enterprise.inject.Any()].
at org.springframework.data.elasticsearch.repository.cdi.ElasticsearchRepositoryExtension.createRepositoryBean(ElasticsearchRepositoryExtension.java:75)
at org.springframework.data.elasticsearch.repository.cdi.ElasticsearchRepositoryExtension.afterBeanDiscovery(ElasticsearchRepositoryExtension.java:63)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.jboss.weld.injection.StaticMethodInjectionPoint.invoke(StaticMethodInjectionPoint.java:88)
at org.jboss.weld.injection.MethodInvocationStrategy$SpecialParamPlusBeanManagerStrategy.invoke(MethodInvocationStrategy.java:144)
at org.jboss.weld.event.ObserverMethodImpl.sendEvent(ObserverMethodImpl.java:309)
at org.jboss.weld.event.ExtensionObserverMethodImpl.sendEvent(ExtensionObserverMethodImpl.java:124)
at org.jboss.weld.event.ObserverMethodImpl.sendEvent(ObserverMethodImpl.java:287)
at org.jboss.weld.event.ObserverMethodImpl.notify(ObserverMethodImpl.java:265)
at org.jboss.weld.event.ObserverNotifier.notifySyncObservers(ObserverNotifier.java:271)
at org.jboss.weld.event.ObserverNotifier.notify(ObserverNotifier.java:260)
at org.jboss.weld.event.ObserverNotifier.fireEvent(ObserverNotifier.java:154)
at org.jboss.weld.event.ObserverNotifier.fireEvent(ObserverNotifier.java:148)
at org.jboss.weld.bootstrap.events.AbstractContainerEvent.fire(AbstractContainerEvent.java:53)
at org.jboss.weld.bootstrap.events.AbstractDefinitionContainerEvent.fire(AbstractDefinitionContainerEvent.java:42)
at org.jboss.weld.bootstrap.events.AfterBeanDiscoveryImpl.fire(AfterBeanDiscoveryImpl.java:61)
at org.jboss.weld.bootstrap.WeldStartup.deployBeans(WeldStartup.java:423)
at org.jboss.weld.bootstrap.WeldBootstrap.deployBeans(WeldBootstrap.java:83)
at org.jboss.weld.environment.se.Weld.initialize(Weld.java:557)
at org.jboss.weld.environment.se.StartMain.go(StartMain.java:44)
at org.jboss.weld.environment.se.StartMain.main(StartMain.java:53)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)

at org.jboss.weld.bootstrap.events.AbstractDefinitionContainerEvent.fire(AbstractDefinitionContainerEvent.java:44)
at org.jboss.weld.bootstrap.events.AfterBeanDiscoveryImpl.fire(AfterBeanDiscoveryImpl.java:61)
at org.jboss.weld.bootstrap.WeldStartup.deployBeans(WeldStartup.java:423)
at org.jboss.weld.bootstrap.WeldBootstrap.deployBeans(WeldBootstrap.java:83)
at org.jboss.weld.environment.se.Weld.initialize(Weld.java:557)
at org.jboss.weld.environment.se.StartMain.go(StartMain.java:44)
at org.jboss.weld.environment.se.StartMain.main(StartMain.java:53)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)

这个问题的原因是什么?

4

1 回答 1

0

经过一番调试,我注意到在处理 bean 阶段,CDI 扩展 ElasticsearchRepositoryExtension 中的 elasticsearchOperationsMap 中没有存储任何 bean,这会导致稍后抛出异常。

目前,我正在使用一种解决方法。@NoRepositoryBean我在界面中添加注释:

@NoRepositoryBean
public interface EmployeeRepository extends ElasticsearchRepository<Employee, String>

然后使用以下@Produces方法:

@Produces
public EmployeeRepository getEmployeeRepository() {

    Client client = null;

    try {
        client = TransportClient.builder().build()
                .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));
    } catch (UnknownHostException e) {
        e.printStackTrace();
    }


    ElasticsearchRepositoryFactory elasticsearchRepositoryFactory = new ElasticsearchRepositoryFactory(new ElasticsearchTemplate(client));

    return elasticsearchRepositoryFactory.getRepository(EmployeeRepository.class);
}
于 2016-09-02T13:36:08.350 回答