Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't locate import javax.inject.Inject package

I'm trying to implement Dagger as a dependency injector in an IntelliJ project, but my code is failing on:

import javax.inject.Inject; 

Intellij is finding the 'javax' package, but not the 'inject' package, so it fails.

I am new to Android, so I apologize if this is a no brainer, but can anyone tell me why the inject package is not being found?

like image 559
Indigo Nai Avatar asked Oct 31 '13 23:10

Indigo Nai


People also ask

What is javax inject inject?

Package javax. inject. This package specifies a means for obtaining objects in such a way as to maximize reusability, testability and maintainability compared to traditional approaches such as constructors, factories, and service locators (e.g., JNDI).

What is javax inject provider?

javax.injectProvides instances of T . Typically implemented by an injector. For any type T that can be injected, you can also inject Provider<T> . Compared to injecting T directly, injecting Provider<T> enables: retrieving multiple instances.

How do you inject an object in Java?

There is a 3rd way to inject dependencies in Java, and it is called Field Injection . The only way for field injection to work is: Mutating the field because it's a non-private and non-final field. Mutating a final/private field using reflection.

What is the use of @inject annotation?

The @Inject annotation lets us define an injection point that is injected during bean instantiation. Injection can occur via three different mechanisms. A bean can only have one injectable constructor.


1 Answers

Dagger depends on JSR 330, the Java standard annotations which are used for dependency injection (think: @Inject, @Singleton, etc.).

This is a separate jar that you have to include. If you were using a build system with integrated dependency management (Maven, Gradle, Ant+Ivy, sbt) you'd get this for free. If you're still copying around jars then you have to add it manually.

You can download the latest jar from Maven central (at the bottom).

like image 153
Jake Wharton Avatar answered Sep 22 '22 04:09

Jake Wharton