Guice在scala中注入注释

时间:2015-04-09 12:23:45

标签: java scala dependency-injection elasticsearch guice

我正在尝试使用scala创建一个自定义elasticsearch插件,方法是遵循以下tutorial(使用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链接23

但不幸的是,作为一个新手,我无法理解它并消除我的错误。有人能指出我需要在这里纠正的事情。

[EDITED:]也包括模块对象的代码。

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
  }
}

1 个答案:

答案 0 :(得分:1)

请改为尝试:

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
  }
}

注释位于构造函数而不是类对象上。你需要额外的一组parens,以便它不会尝试使用你的各种参数作为构造Inject注释的一部分。

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