Junior — Middle
Kuidas rakendatakse sõltuvuste süstimist Daggeris, kasutades annotatsioone sõltuvuste komponentide sees?
sobes.tech AI
Vastus AI-lt
In Dagger, dependency injection via annotations within dependent components is implemented using the annotations @Component and @Subcomponent.
- The main component (
@Component) provides dependencies. - The dependent component (
@Subcomponent) is declared within the main and can inherit dependencies.
Example:
@Subcomponent
public interface ChildComponent {
void inject(SomeClass target);
}
@Component
public interface ParentComponent {
ChildComponent childComponent();
}
Here, ChildComponent depends on ParentComponent and can use its dependencies. Annotations allow Dagger to generate code for automatic dependency injection into the necessary classes.