question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Field injection does not work for all classes

See original GitHub issue

I steped into a limitation with field injection during the introduction of the new elasticsearch client for version 7.X. The class RestHighLevelClient is not injectable because it contains final fields:

WELD-001480: Bean type class org.elasticsearch.client.RestHighLevelClient is not proxyable because it contains a final method public final org.elasticsearch.action.index.IndexResponse org.elasticsearch.client.RestHighLevelClient.index(org.elasticsearch.action.index.IndexRequest,org.elasticsearch.client.RequestOptions) throws java.io.IOException - Producer Method [RestHighLevelClient] with qualifiers [@Default @Named @Any] declared as [[BackedAnnotatedMethod] @Produces @ApplicationScoped @Named public static io.apiman.manager.test.server.TestCdiFactory.provideStorageJestClient()]

Using a wrapper for the class works:

2020-04-01 11_44_14-apiman  C__Projekte_apiman  -  _manager_test_api_src_main_java_io_apiman_manag

But I personally not like this approach that much, because we have an unnecessary getter call to get the client from the wrapper. I would prefer to avoid injections by passing the client via parameter of the EsStorage initilize method or constructor e.g. here: https://github.com/apiman/apiman/blob/8e8fc6d8a64b270ed536b1df0ffd43137b89f854/manager/test/api/src/main/java/io/apiman/manager/test/server/TestCdiFactory.java#L123

@EricWittmann @msavy What you are thinking about this?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
EricWittmanncommented, Apr 1, 2020

I don’t have a strong preference here. Injection is great when it works well but it doesn’t always. In this case feel free to update the initialize method to accept the client as a parameter. Or, if it makes sense, just have the EsStorage class instantiate the client directly.

0reactions
EricWittmanncommented, Apr 6, 2020

Ah yes good point - there may be a test vs. production difference that I’m not thinking about. Either way, as long as you consider that option (even if you choose the alternative) I’m happy. 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Dagger2 Field Injection not working - Stack Overflow
I am trying to get a simple java application working but field injection does not work. I have a class Main , that...
Read more >
Field injection is not recommended – Spring IOC - Marc Nuri
Field-based dependency injection won't work on fields that are declared final/immutable as this fields must be instantiated at class ...
Read more >
Dependency injection and programmatic lookup
Getter and setter methods are not required for field injection to work (unlike with JSF managed beans). Dependency injection always occurs when ...
Read more >
Stop using Field injection in Spring applications
Let's take a look at a few of Spring's dependency injection methods: constructor injection, Field injection and Setter injection.
Read more >
Constructor injection should be used instead of field injection
Field injection seems like a tidy way to get your classes what they need to do their jobs, but it's really a NullPointerException...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found