Starting with spring 3.0, spring begins to support annotations of the JSR-330 standard (dependency injection). These annotations and spring annotations are scanned in a way that developers only need to configure the relevant jar packages in classpath.
If a developer uses MAVEN to manage a project, javax.inject
artifact is available in the Maven repository (http://repo1.maven.org/maven2/javax/inject/javax.inject/1/). Developers only need to refer to this dependency in the Pom.
<dependency> <groupId>javax.inject</groupId> <artifactId>javax.inject</artifactId> <version>1</version></dependency>
Pass
@Inject
And
@Named
Make Dependency Injection
In JSR-330, the @javax.inject.Inject
same responsibilities as in spring @Autowired
:
import javax.inject.Inject;public class SimpleMovieLister { private MovieFinder movieFinder; @Inject public void setMovieFinder(MovieFinder movieFinder) { this.movieFinder = movieFinder; } public void listMovies() { this.movieFinder.findMovies(...); ... }}
and @Autowired
consistent, developers can use @Inject
dependency injection in instance variables, methods, and construction parameter levels. Also, developers can declare injections as Provider
, by Provider.get()
requesting those beans that are either short-scoped or deferred-initialized. For example, the following examples:
import javax.inject.Inject;import javax.inject.Provider;public class SimpleMovieLister { private Provider<MovieFinder> movieFinder; public void listMovies() { this.movieFinder.get().findMovies(...); ... }}
If the developer wants to qualify the injected bean by name, you can use @Named
annotations:
import javax.inject.Inject;import javax.inject.Named;publicclass SimpleMovieLister { private MovieFinder movieFinder; @Inject publicvoidsetMovieFinder(@Named("main") MovieFinder movieFinder) { this.movieFinder = movieFinder; } // ...}
@Named
: Equivalent to
@Component
Annotations
In JSR-330, the responsibilities in spring are similar to the @javax.inject.Named
@Component
following:
import javax.inject.Inject;import javax.inject.Named;@Named("movieListener")publicclass SimpleMovieLister { private MovieFinder movieFinder; @Inject publicvoidsetMovieFinder(MovieFinder movieFinder) { this.movieFinder = movieFinder; } // ...}
@Component
Often do not need to specify the name of the use, the @Named
annotations are the same:
import javax.inject.Inject;import javax.inject.Named;@Namedpublicclass SimpleMovieLister { private MovieFinder movieFinder; @Inject publicvoidsetMovieFinder(MovieFinder movieFinder) { this.movieFinder = movieFinder; } // ...}
@Named
You can also use component scans when using annotations:
"org.example")public class AppConfig { ...}
Unlike spring's @Component
components, JSR-330 Named
annotations cannot be combined and use spring's component annotations if you want to customize component annotations.
Limitations of JSR-330 Standard annotations
When using JSR-330 standard annotations, it is also necessary to understand the different points of the spring annotations, as shown in the following table:
Spring |
javax.inject.* |
Javax.inject Restrictions |
@Autowired |
@Inject |
@Inject Annotations have no required attributes, but can be replaced by Java 8 Optional |
@Component |
@Named |
The jsr_330 standard does not provide a composite model, there is only one way to identify the component |
@Scope ("singleton") |
@Singleton |
JSR-330 The default scope is similar to spring, prototype However, why it is consistent with spring's default, beans in the JSR-330 standard are also singleton by default in spring. If you want to use a non-singleton scope, the developer should use spring @Scope annotations. java.inject an annotation is also provided @Scope , however, this annotation can only be used when creating a custom scope. |
@Qualifier |
@Qualifier/@Named |
javax.inject.Qualifier is just a meta-annotation used to build a custom qualifier. A string qualifier (such as in spring @Qualifier ) can be used javax.inject.Named to implement the |
@Value |
- |
Unequal price |
@Required |
- |
Unequal price |
@Lazy |
- |
Unequal price |
Objectfactory |
Provider |
javax.inject.Provider Is ObjectFactory another spring option, which get() Provider can be used in combination with spring by means of proxy. @Autowired |
JSR-330 Standard Annotations