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.

Allow for annotation to replace empty no-args constructor with Inject

See original GitHub issue

There has been a lot of discussion about the no-args @Inject constructors already. To that end, I would like to propose an alternative solution to the boilerplate-y looking constructor - an annotation that would explicitly mark the class as injectable by Dagger, even without @Inject marked constructor. Both options could live on, for those who prefer one or another.

Something like this:

import dagger.Injectable;

@Injectable
public class MyService {
    // ...
}

If the annotation is present, Dagger would allow for “default” no-args constructor to be used.

Related to #1105, #1409 and discussed in square#412.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5

github_iconTop GitHub Comments

2reactions
ronshapirocommented, Feb 1, 2019

I don’t think adding support for something that is so similar to an already standard solution makes sense. Then readers have to discern why one solution was used and not the other. As it is, @Inject MyService() {} is just as easy to write, plus it will work with all other JSR 330 injectors.

0reactions
ronshapirocommented, Mar 25, 2019

Each new feature adds maintenance cost and conceptual overhead. Some consider it flexibility, others consider it confusing. We’ve opted for fewer concepts unless there’s a clear benefit (runtime, compile time static analysis options, etc). I don’t think this has much benefits besides some personal style.

If we allow custom binding annotations at some point, this would be a great candidate for something that you could add to your project (but keep outside of Dagger).

Read more comments on GitHub >

github_iconTop Results From Across the Web

If I use field injection with Guice, why can I only use the no arg ...
So to answer your question, no, it's not possible to request Guice to inject your constructor with a String argument without annotating it...
Read more >
@NoArgsConstructor, @RequiredArgsConstructor ...
Overview. This set of 3 annotations generate a constructor that will accept 1 parameter for certain fields, and simply assigns this parameter to...
Read more >
Constructor Injection in Spring with Lombok - Baeldung
A constructor doesn't have to be annotated as long as there is only one in a component and Spring can unambiguously choose it...
Read more >
Dagger and @Inject on constructors — do or don't?
Now during maintenance when a constructor argument needs added or removed. Not ideal, every change really requires three changes, and let's not ......
Read more >
S1186 empty constructor - Google Groups
I'm referring to the use case in which there is more than one constructor and one is no-arg Jax-RS or not. Adding a...
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