# Injection for ActionPlugin

**URL:** <https://discuss.elastic.co/t/injection-for-actionplugin/184251>\
**Category:** Elasticsearch\
**Created:** [June 4, 2019, 8:43pm UTC](https://discuss.elastic.co/t/injection-for-actionplugin/184251 "2019-06-04T20:43:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chris\_H1](https://avatars.discourse-cdn.com/v4/letter/c/a9adbd/32.png) [@Chris\_H1](https://discuss.elastic.co/u/Chris_H1)\
**Post date:** [June 4, 2019, 8:43pm UTC](https://discuss.elastic.co/t/injection-for-actionplugin/184251/1 "2019-06-04T20:43:23Z")

</div>

A plugin we use has stopped working after upgrading to 5.6 from 5.1. The plugin adds a RestHandler. Here is the old constructor for the RestHandler:  
public class TranslateAction implements RestHandler {

```
	private final Provider<aTranslator> aTranslatorProvider;
	private final Provider<bTranslator> bTranslatorProvider;

    @Inject
    public TranslateAction(final RestController controller, 
            final Provider<aTranslator> aTranslatorProvider,
            final Provider<bTranslator> bTranslatorProvider) {

        controller.registerHandler(Method.GET, "/_translate", this);
        controller.registerHandler(Method.POST, "/_translate", this);
        this.aTranslatorProvider = aTranslatorProvider;
        this.bTranslatorProvider = bTranslatorProvider;
    }
...

```

In 5.6 upon startup we get the exception

> org.elasticsearch.common.inject.CreationException: Guice creation errors:
> 
> 1. Could not find a suitable constructor in org.elasticsearch.rest.RestController. Classes must have either one (and only one) constructor annotated with @Inject or a zero-argument constructor that is not private.

Fine. Elsewhere on this site (I cannot find it now) I found a suggestion to NOT inject the RestController but instead have th Plugin implement ActionPlugin and override getRestHandlers in a method that would look like this:

```
    @Override 
public List<RestHandler> getRestHandlers(Settings settings, RestController restController, ClusterSettings clusterSettings,
        IndexScopedSettings indexScopedSettings, SettingsFilter settingsFilter,
        IndexNameExpressionResolver indexNameExpressionResolver, Supplier<DiscoveryNodes> nodesInCluster) {
    List<RestHandler> list = new ArrayList<>();
    
    Injector injector = Guice.createInjector(new C3POModule());
    TranslateAction ta = injector.getInstance(TranslateAction.class);
    restController.registerHandler(Method.GET, "/_translate", ta);
    restController.registerHandler(Method.POST, "/_translate", ta);
    list.add(ta);
    
    return Collections.unmodifiableList(list);
}

```

BUT, the problem now is that the TranslateAction class depended on GuiceInjection and gets new errors. One of the dependencies has an @Inject for org.elasticsearch.client.Client. We get an error now saying no implementation for Client is bound.  
Prior to this change, the Client injected fine, I believe due to the bind calls in org.elasticsearch.node.Node (line 459):  
b.bind(NodeClient.class).toInstance(client);

Without being good at Guice or knowing the proper vocab, I believe the error is this line in the new code:

```
    Injector injector = Guice.createInjector(new C3POModule());

```

and instead we need to get the injector created in Node. Is this possible?

Further info:  
The plugin relies on the NodeClient class to get an IndicesAdminClient to peform analyze requests.

---

<div class="post-metadata">

**Author:** ![rjernst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rjernst/32/6363_2.png) [@rjernst](https://discuss.elastic.co/u/rjernst)\
**Post date:** [June 5, 2019, 6:47pm UTC](https://discuss.elastic.co/t/injection-for-actionplugin/184251/2 "2019-06-05T18:47:37Z")

</div>

You should construct your handler without using Guice. The NodeClient is passed in at runtime to rest handlers, for example in `prepareRequest`, so you should not need it at construction.

---

<div class="post-metadata">

**Author:** ![Chris\_H1](https://avatars.discourse-cdn.com/v4/letter/c/a9adbd/32.png) [@Chris\_H1](https://discuss.elastic.co/u/Chris_H1)\
**Post date:** [June 5, 2019, 8:19pm UTC](https://discuss.elastic.co/t/injection-for-actionplugin/184251/3 "2019-06-05T20:19:26Z")

</div>

Thanks for the reply Ryan. This is what I needed!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 3, 2019, 8:24pm UTC](https://discuss.elastic.co/t/injection-for-actionplugin/184251/4 "2019-07-03T20:24:40Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
