其他分享
首页 > 其他分享> > Spring文档之使用 JSR 330 标准注解

Spring文档之使用 JSR 330 标准注解

作者:互联网

目录
从 Spring 3.0 开始,Spring 提供对 JSR-330 标准注解(依赖注入)的支持。这些注释的扫描方式与 Spring 注释相同。要使用它们,您需要在类路径中包含相关的 jar。

注意:

如果您使用 Maven,则该javax.inject工件在标准 Maven 存储库 ( https://repo1.maven.org/maven2/javax/inject/javax.inject/1/ ) 中可用 。您可以将以下依赖项添加到文件 pom.xml 中:

<dependency>
    <groupId>javax.inject</groupId>
    <artifactId>javax.inject</artifactId>
    <version>1</version>
</dependency>

2.11.1. 依赖注入@Inject@Named

代替@Autowired,您可以使用@javax.inject.Inject如下:

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(...);
        // ...
    }
}

与@Autowired一样,您可以在字段级、方法级和构造函数参数级使用@Inject。
此外,您可以将注入点声明为提供者,从而允许按需访问作用域更短的bean,或通过Provider.get()调用延迟访问其他bean。
下面的例子提供了上一个例子的变体:

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(...);
        // ...
    }
}

如果你想为应该注入的依赖项使用限定名,你应该使用@Named注释,如下例所示:

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;
    }

    // ...
}

与@Autowired一样,@Inject也可以与java.util.Optional或@Nullable一起使用。这在这里更适用,因为@Inject没有required属性。下面的两个例子展示了如何使用@Inject和@Nullable:

public class SimpleMovieLister {

    @Inject
    public void setMovieFinder(Optional<MovieFinder> movieFinder) {
        // ...
    }
}
publInstead of @Component, you can use @javax.inject.Named or javax.annotation.ManagedBean, as the following example shows:
    @Inject
    public void setMovieFinder(@Nullable MovieFinder movieFinder) {
        // ...
    }
}

2.11.2. @Named和@ManagedBean:与@Component注释的标准等价物

代替 @Component, 你可以使用 @javax.inject.Namedjavax.annotation.ManagedBean, 如下例所示:

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;
    }

    // ...
}

在不指定组件名称的情况下使用@Component是很常见的。@Named可以以类似的方式使用,如下例所示:

import javax.inject.Inject;
import javax.inject.Named;

@Named
public class SimpleMovieLister {

    private MovieFinder movieFinder;

    @Inject
    public void setMovieFinder(MovieFinder movieFinder) {
        this.movieFinder = movieFinder;
    }

    // ...
}

当你使用' @Named '或' @ManagedBean '时,你可以像使用Spring注释一样使用组件扫描,如下面的例子所示:

@Configuration
@ComponentScan(basePackages = "org.example")
public class AppConfig  {
    // ...
}

注意:

与@Component相比,JSR-330 @Named和JSR-250 ManagedBean注释是不可组合的。
您应该使用Spring的原型模型来构建自定义组件注释。

2.11.3. JSR-330 标准注解的限制

当你使用标准注释时,你应该知道一些重要的特性是不可用的,如下表所示:

Spring javax.inject.* javax.inject restrictions / comments
@Autowired @Inject @Inject has no 'required' attribute. Can be used with Java 8’s Optional instead.
@Component @Named / @ManagedBean JSR-330 does not provide a composable model, only a way to identify named components.
@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.
@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.
@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.

标签:Named,Spring,330,public,Inject,inject,JSR,javax,movieFinder
来源: https://www.cnblogs.com/Ishton/p/15391620.html