gpt4 book ai didi

java - Guice 在 scala 中注入(inject)注释

转载 作者:行者123 更新时间:2023-11-30 08:16:48 24 4
gpt4 key购买 nike

我正在尝试按照以下tutorial使用scala创建自定义elasticsearch插件(Java 语言)。

public class PlusSignAnalyzerProvider extends
AbstractIndexAnalyzerProvider {

/* Constructor. Nothing special here. */
@Inject
public PlusSignAnalyzerProvider(Index index,
@IndexSettings Settings indexSettings, Environment env,
@Assisted String name, @Assisted Settings settings) throws IOException {
super(index, indexSettings, name, settings);
}

/* This function needs to be overridden to return an instance of
* PlusSignAnalyzer.
*/
@Override
public PlusSignAnalyzer get() {
return this.analyzer;
}

/* Instance of PlusSignAnalyzer class that is returned by this class. */
protected PlusSignAnalyzer analyzer = new PlusSignAnalyzer();

/* Name to associate with this class. We will use this in
* PlusSignBinderProcessor.
*/
public static final String NAME = "plus_sign";
}

尝试在 scala 中实现此目的,我执行了以下操作:

@Inject class PlusSignAnalyzerProvider(index: Index,
@IndexSettings indexSettings: Settings,
env: Environment,
@AssistedInject @Assisted name: String,
@AssistedInject @Assisted settings: Settings)
extends AbstractIndexAnalyzerProvider[PlusSignAnalyzer](index, indexSettings, name, settings) {

val analyzer: PlusSignAnalyzer = new PlusSignAnalyzer()

def NAME = "plus_sign"

override def get(): PlusSignAnalyzer = {
this.analyzer
}
}

在运行时给我一个错误:

Exception in thread "main" org.elasticsearch.common.inject.CreationException: Guice creation errors:

1) Could not find a suitable constructor in com.xxxxxx.yyyyyyy.es.plugins.PlusSignAnalyzerProvider. Classes must have either one (and only one) constructor annotated with @Inject or a zero-argument constructor that is not private.
at com.xxxxxx.yyyyyyy.es.plugins.PlusSignAnalyzerProvider.class(Unknown Source)
at _unknown_

1 error
at org.elasticsearch.common.inject.internal.Errors.throwCreationExceptionIfErrorsExist(Errors.java:344)
at org.elasticsearch.common.inject.InjectorBuilder.initializeStatically(InjectorBuilder.java:151)
at org.elasticsearch.common.inject.InjectorBuilder.build(InjectorBuilder.java:102)
at org.elasticsearch.common.inject.Guice.createInjector(Guice.java:93)
at org.elasticsearch.common.inject.Guice.createInjector(Guice.java:70)
at org.elasticsearch.common.inject.ModulesBuilder.createInjector(ModulesBuilder.java:59)
at org.elasticsearch.node.internal.InternalNode.<init>(InternalNode.java:203)
at org.elasticsearch.node.NodeBuilder.build(NodeBuilder.java:159)

[编辑]:以下内容也不起作用:

class PlusSignAnalyzerProvider @Inject() (index: Index,
@IndexSettings indexSettings: Settings,
env: Environment,
@Assisted name: String,
@Assisted settings: Settings)
extends AbstractIndexAnalyzerProvider[PlusSignAnalyzer](index, indexSettings, name, settings) {

val analyzer: PlusSignAnalyzer = new PlusSignAnalyzer()

def NAME = "plus_sign"

override def get(): PlusSignAnalyzer = {
this.analyzer
}
}

抛出的错误是:

  Exception in thread "main" org.elasticsearch.common.inject.CreationException: Guice creation errors:

1) No implementation for java.lang.String annotated with @org.elasticsearch.common.inject.assistedinject.Assisted(value=) was bound.
while locating java.lang.String annotated with @org.elasticsearch.common.inject.assistedinject.Assisted(value=)
for parameter 3 at com.xxxxxx.yyyyyyy.es.plugins.PlusSignAnalyzerProvider.<init>(Unknown Source)
at _unknown_

2) No implementation for org.elasticsearch.common.settings.Settings annotated with @org.elasticsearch.common.inject.assistedinject.Assisted(value=) was bound.
while locating org.elasticsearch.common.settings.Settings annotated with @org.elasticsearch.common.inject.assistedinject.Assisted(value=)
for parameter 4 at com.xxxxxx.yyyyyyy.es.plugins.PlusSignAnalyzerProvider.<init>(Unknown Source)
at _unknown_

3) No implementation for org.elasticsearch.common.settings.Settings annotated with @org.elasticsearch.index.settings.IndexSettings() was bound.
while locating org.elasticsearch.common.settings.Settings annotated with @org.elasticsearch.index.settings.IndexSettings()
for parameter 1 at com.xxxxxx.yyyyyyy.es.plugins.PlusSignAnalyzerProvider.<init>(Unknown Source)
at _unknown_

4) Could not find a suitable constructor in org.elasticsearch.index.Index. Classes must have either one (and only one) constructor annotated with @Inject or a zero-argument constructor that is not private.
at org.elasticsearch.index.Index.class(Unknown Source)
while locating org.elasticsearch.index.Index
for parameter 0 at com.xxxxxx.yyyyyyy.es.plugins.PlusSignAnalyzerProvider.<init>(Unknown Source)
at _unknown_

4 errors
at org.elasticsearch.common.inject.internal.Errors.throwCreationExceptionIfErrorsExist(Errors.java:344)
at org.elasticsearch.common.inject.InjectorBuilder.initializeStatically(InjectorBuilder.java:151)
at org.elasticsearch.common.inject.InjectorBuilder.build(InjectorBuilder.java:102)
at org.elasticsearch.common.inject.Guice.createInjector(Guice.java:93)
at org.elasticsearch.common.inject.Guice.createInjector(Guice.java:70)
at org.elasticsearch.common.inject.ModulesBuilder.createInjector(ModulesBuilder.java:59)
at org.elasticsearch.node.internal.InternalNode.<init>(InternalNode.java:203)
at org.elasticsearch.node.NodeBuilder.build(NodeBuilder.java:159)

在谷歌搜索中,我最终找到了 SO 链接 2 , 3

但不幸的是,作为一个新手,我无法从中理解并消除我的错误。有人可以指出我这里需要纠正什么吗?

[编辑:]还包括模块对象的代码。

import org.elasticsearch.common.inject.AbstractModule

class PlusSignBinderModule extends AbstractModule {
override def configure(): Unit = {
bind(classOf[PlusSignAnalyzerProvider]).asEagerSingleton()
}
}

插件代码:

class MyFirstAnalyzerPlugin extends AbstractPlugin {
override def description(): String = {
"Custom elasticsearch analyzer plugin"
}

override def name(): String = {
"my-analyzer"
}

override def modules(): Collection[Class[_ <: Module]] = {
val modules: Collection[Class[_ <: Module]] = Lists.newArrayList()
modules.add(classOf[PlusSignBinderModule])
modules
}
}

最佳答案

试试这个:

class PlusSignAnalyzerProvider @AssistedInject() (index: Index,
@IndexSettings indexSettings: Settings,
env: Environment,
@Assisted name: String,
@Assisted settings: Settings)
extends AbstractIndexAnalyzerProvider[PlusSignAnalyzer](index, indexSettings, name, settings) {

val analyzer: PlusSignAnalyzer = new PlusSignAnalyzer()

def NAME = "plus_sign"

override def get(): PlusSignAnalyzer = {
this.analyzer
}
}

注释位于构造函数而不是类对象上。您需要额外的一组括号,这样它就不会尝试使用您的各种参数作为 Inject 注释构造的一部分。

您还需要确保其中的其他元素已绑定(bind)(IndexSettingsEnvironment),因为您尚未发布您的 Module 对象,我无法判断它们是否正确绑定(bind),以及您是否使用正确的语义来正确执行辅助注入(inject)。

关于java - Guice 在 scala 中注入(inject)注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29538400/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com