Using JSR 330 Standard Annotations
1.11. Using JSR 330 Standard Annotations
Starting with Spring 3.0, Spring offers support for JSR-330 standard annotations (Dependency Injection). Those annotations are scanned in the same way as the Spring annotations. To use them, you need to have the relevant jars in your classpath.
If you use Maven, the
javax.inject
artifact is available in the standard Maven repository ( https://repo1.maven.org/maven2/javax/inject/javax.inject/1/). You can add the following dependency to your file pom.xml:<dependency> <groupId>javax.inject</groupId> <artifactId>javax.inject</artifactId> <version>1</version> </dependency>
1.11.1. Dependency Injection with `@Inject` and `@Named`
Instead of @Autowired
, you can use @javax.inject.Inject
as follows:
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(...);
// ...
}
}
As with @Autowired
, you can use @Inject
at the field level, method level and constructor-argument level. Furthermore, you may declare your injection point as a Provider
, allowing for on-demand access to beans of shorter scopes or lazy access to other beans through a Provider.get()
call. The following example offers a variant of the preceding example:
import javax.inject.Inject;
import javax.inject.Provider;
public class SimpleMovieLister {
private Provider<MovieFinder> movieFinder;
@Inject
public void setMovieFinder(Provider<MovieFinder> movieFinder) {
this.movieFinder = movieFinder;
}
public void listMovies() {
this.movieFinder.get().findMovies(...);
// ...
}
}
If you would like to use a qualified name for the dependency that should be injected, you should use the @Named
annotation, as the following example shows:
import javax.inject.Inject;
import javax.inject.Named;
public class SimpleMovieLister {
private MovieFinder movieFinder;
@Inject
public void setMovieFinder(@Named("main") MovieFinder movieFinder) {
this.movieFinder = movieFinder;
}
// ...
}
As with @Autowired
, @Inject
can also be used with java.util.Optional
or @Nullable
. This is even more applicable here, since @Inject
does not have a required
attribute. The following pair of examples show how to use @Inject
and @Nullable
:
public class SimpleMovieLister {
@Inject
public void setMovieFinder(Optional<MovieFinder> movieFinder) {
// ...
}
}
public class SimpleMovieLister {
@Inject
public void setMovieFinder(@Nullable MovieFinder movieFinder) {
// ...
}
}
1.11.2. `@Named` and `@ManagedBean`: Standard Equivalents to the `@Component` Annotation
Instead of @Component
, you can use @javax.inject.Named
or javax.annotation.ManagedBean
, as the following example shows:
import javax.inject.Inject;
import javax.inject.Named;
@Named("movieListener") // @ManagedBean("movieListener") could be used as well
public class SimpleMovieLister {
private MovieFinder movieFinder;
@Inject
public void setMovieFinder(MovieFinder movieFinder) {
this.movieFinder = movieFinder;
}
// ...
}
It is very common to use @Component
without specifying a name for the component. @Named
can be used in a similar fashion, as the following example shows:
import javax.inject.Inject;
import javax.inject.Named;
@Named
public class SimpleMovieLister {
private MovieFinder movieFinder;
@Inject
public void setMovieFinder(MovieFinder movieFinder) {
this.movieFinder = movieFinder;
}
// ...
}
When you use @Named
or @ManagedBean
, you can use component scanning in the exact same way as when you use Spring annotations, as the following example shows:
@Configuration
@ComponentScan(basePackages = "org.example")
public class AppConfig {
// ...
}
In contrast to
@Component
, the JSR-330@Named
and the JSR-250@ManagedBean
annotations are not composable. You should use Spring’s stereotype model for building custom component annotations.
1.11.3. Limitations of JSR-330 Standard Annotations
When you work with standard annotations, you should know that some significant features are not available, as the following table shows:
Spring | javax.inject.* | javax.inject restrictions / comments | javax.inject限制/说明 |
---|---|---|---|
@Autowired | @Inject | @Inject has no 'required' attribute. Can be used with Java 8’s Optional instead. |
@Inject没有'required'属性。可以与Java 8的可选选项一起使用。 |
@Component | @Named / @ManagedBean | JSR-330 does not provide a composable model, only a way to identify named components. | JSR-330不提供可组合模型,只提供一种识别命名组件的方法。 |
@Scope("singleton") | @Singleton | The JSR-330 default scope is like Spring’s prototype . However, in order to keep it consistent with Spring’s general defaults, a JSR-330 bean declared in the Spring container is a singleton by default. In order to use a scope other than singleton , you should use Spring’s @Scope annotation. javax.inject also provides a @Scope annotation. Nevertheless, this one is only intended to be used for creating your own annotations. |
JSR-330的默认作用域类似于Spring的原型。然而,为了使它与Spring的一般默认值保持一致,在Spring容器中声明的JSR-330 bean在默认情况下是单例的。为了使用作用域而不是单例,你应该使用Spring的@Scope注释。javax。inject还提供了@Scope注释。然而,这个选项仅用于创建您自己的注释。 |
@Qualifier | @Qualifier / @Named | javax.inject.Qualifier is just a meta-annotation for building custom qualifiers. Concrete String qualifiers (like Spring’s @Qualifier with a value) can be associated through javax.inject.Named . |
qualifier只是一个用于构建自定义限定符的元注释。具体的字符串限定符(像Spring的带有值的@Qualifier)可以通过javax.inject.Named进行关联。 |
@Value | - | no equivalent | 无 |
@Required | - | no equivalent | 无 |
@Lazy | - | no equivalent | 无 |
ObjectFactory | Provider | javax.inject.Provider is a direct alternative to Spring’s ObjectFactory , only with a shorter get() method name. It can also be used in combination with Spring’s @Autowired or with non-annotated constructors and setter methods. |
provider是Spring的ObjectFactory的直接替代品,只是有一个更短的get()方法名。它还可以与Spring的@Autowired或与无注释的构造函数和setter方法结合使用。 |