@Inject constructor plus scope annotation can't provide local singleton
See original GitHub issueI have a dependency class which has a @Inject constructor:
public class DemoDirectInjectDependency {
private final Context mContext;
@ActivityScope
@Inject
public DemoDirectInjectDependency(Context context) {
mContext = context;
Timber.d("new DemoDirectInjectDependency");
}
}
Although I annotate it with @ActivityScope
, it will be created multiple times when I inject it into multiple places.
The generated inject code looks like below:
this.demoDirectInjectDependencyProvider =
DemoDirectInjectDependency_Factory.create(provideContextProvider);
We can see the provider is not a ScopedProvider
.
Full code could be found here.
Of course I could create a @Provide
method in a @Module
class, which could utilize the scope annotation. I just wonder could dagger2 utilize the scope annotation at constructor and create a ScopedProvider
to provide this dependency?
Issue Analytics
- State:
- Created 7 years ago
- Comments:9 (1 by maintainers)
Top Results From Across the Web
Dependency injection with Dagger 2 - Custom scopes
In short - scopes give us “local singletons” which live as long as scope itself. Just to be clear - there are no...
Read more >Weld - CDI Reference Implementation
As you've guessed, the @Inject annotation has something to do with dependency injection! @Inject may be applied to a constructor or method of...
Read more >How make AppModule works in Dagger 2 with kotlin App
Try removing @ActivityScoped from your repositories. Also, regarding your provideDB - I would change the return type to LocalDatabase (without ...
Read more >Eager Initialization of Singletons - Micronaut Documentation
Micronaut will automatically discover dependency injection metadata on the classpath and wire the beans together according to injection points you define.
Read more >Injecting Prototype Beans into a Singleton Instance in Spring
By default, Spring beans are singletons. The problem arises when we try to wire beans of different scopes. For example, a prototype bean...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
An upcoming version of Dagger 2 will report a compile-time error if you put a scope annotation on an
@Inject
constructor.All I’m saying is that you have to put your
@ActivityScope
on theDemoDirectInjectDependency
class, not its constructor.