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.

@Inject constructor plus scope annotation can't provide local singleton

See original GitHub issue

I 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:closed
  • Created 7 years ago
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
netdpbcommented, Apr 18, 2016

An upcoming version of Dagger 2 will report a compile-time error if you put a scope annotation on an @Inject constructor.

3reactions
tbroyercommented, Apr 11, 2016

All I’m saying is that you have to put your @ActivityScope on the DemoDirectInjectDependency class, not its constructor.

Read more comments on GitHub >

github_iconTop 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 >

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