# Can dependencies be injected into a native script plugin?

**URL:** <https://discuss.elastic.co/t/can-dependencies-be-injected-into-a-native-script-plugin/2385>\
**Category:** Elasticsearch\
**Created:** [June 10, 2015, 10:13pm UTC](https://discuss.elastic.co/t/can-dependencies-be-injected-into-a-native-script-plugin/2385 "2015-06-10T22:13:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tikue](https://avatars.discourse-cdn.com/v4/letter/t/b9e5f3/32.png) [@tikue](https://discuss.elastic.co/u/tikue)\
**Post date:** [June 10, 2015, 10:13pm UTC](https://discuss.elastic.co/t/can-dependencies-be-injected-into-a-native-script-plugin/2385/1 "2015-06-10T22:13:15Z")

</div>

I have a script factory class that has some dependencies that I usually manage with a Spring context. I understand ElasticSearch uses Guice internally, so I sorta naively thought things might just work if I use the javax.inject annotations.

This is what I tried:

```
@Named
public static class Factory implements NativeScriptFactory {
  @Inject
  private Foo foo;
  
  @Inject
  private Bar bar;

  public Factory() {}

  @Override
  public ExecutableScript newScript(Map<String, Object> params) {
    // use foo and bar to create the script
  }
}

```

As you might expect, I get `NullPointerException`s when I perform a function\_score query using this particular script. Is there any way to accomplish what I'm trying to do here?

---

<div class="post-metadata">

**Author:** ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)\
**Post date:** [June 11, 2015, 1:44am UTC](https://discuss.elastic.co/t/can-dependencies-be-injected-into-a-native-script-plugin/2385/2 "2015-06-11T01:44:09Z")

</div>

Use constructor injection and it should work. You might have to use a  
different @Inject. I don't remember the package. I wouldn't be surprised if  
the javax one works though.

---

<div class="post-metadata">

**Author:** ![tikue](https://avatars.discourse-cdn.com/v4/letter/t/b9e5f3/32.png) [@tikue](https://discuss.elastic.co/u/tikue)\
**Post date:** [June 11, 2015, 2:58am UTC](https://discuss.elastic.co/t/can-dependencies-be-injected-into-a-native-script-plugin/2385/3 "2015-06-11T02:58:30Z")

</div>

Yep, constructor injection ended up working! `javax.inject` didn't work for me, but the elasticsearch version did. Unfortunately I realized that I had some beans that had `@Value`-annotated arguments derived from system properties, so it wasn't straightforward to get those working with Guice. For now I've resorted to creating a SpringContextModule class that extends elasticsearch's `AbstractModule` and contains a @Provides method for the spring context.

---

<div class="post-metadata">

**Author:** ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)\
**Post date:** [June 11, 2015, 3:43am UTC](https://discuss.elastic.co/t/can-dependencies-be-injected-into-a-native-script-plugin/2385/4 "2015-06-11T03:43:18Z")

</div>

> [@tikue](#):
>
> For now I've resorted to creating a SpringContextModule class that extends elasticsearch's AbstractModule and contains a @Provides method for the spring context.

That sounds like what I'd try, yes.

---

<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 6, 2017, 12:08am UTC](https://discuss.elastic.co/t/can-dependencies-be-injected-into-a-native-script-plugin/2385/5 "2017-07-06T00:08:17Z")

</div>


