Spring Core 之 Using JSR 330 Standard Annotations (使用JSR330标准注解)

一、Overview

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, You can add the following dependency to your file pom.xml:

Spring从3.0开始提供了对JSR-330标准注解的支持,这些注解和spring的注解被同等的对待,为了使用它们,你需要引入相关的jar包到类路径中。如果你使用的maven构建项目,你可以将以下依赖项添加到文件 pom.xml 中:

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

二、Dependency Injection with @Inject and @Named(使用@Inject和@Named进行依赖注入)

Instead of @Autowired, you can use @javax.inject.Inject as follows:

作为@Autowire的的替代,你可以向下面一样使用@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(...);
        // ...
    }
}

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:

和@Autowired一样,你可以在属性字段、方法级别和构造器级别使用@Inject,而且,你可以声明你的注入点作为一个提供者,允许通过 调用Provider.get() 按需访问更小范围的 bean 或延迟访问其他 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(...);
        // ...
    }
}

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:

如果你想使用限定名称对注入的依赖进行限定,你应该使用@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;
    }

    // ...
}

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 examples show how to use @Inject and @Nullable:

和@Autowired一样,@Inject也可以使用@Nullable,甚至和@Autowired相比更适用,因为@Inject没有required属性,下例展示了如何使用@Inject和@Nullable:

public class SimpleMovieLister {

    @Inject
    public void setMovieFinder(@Nullable MovieFinder movieFinder) {
        // ...
    }
}

三、@Named and @ManagedBean: Standard Equivalents to the @Component Annotation(@Named 和 @ManagedBean:@Component 注解的标准等价物)

Instead of @Component, you can use @javax.inject.Named or javax.annotation.ManagedBean.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:

作为@Component的替代,你可以使用 @javax.inject.Named 或者javax.annotation.ManagedBean,@Named也可以像@Component一样,在使用时不指定name值,如下所示:

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

    // ...
}

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.

和@Component相比,JSR-330 @Named and the JSR-250 ManagedBean 注解不能用来组合使用,也就是说不能用来自定义注解,你应该使用spring的模板模型注解来自定义注解,也就是@Component、@Controller那些。

四、Limitations of JSR-330 Standard Annotations(JSR-330标准注解的限制)

When you work with standard annotations, you should know that some significant features are not available, as the following table shows:

当你使用标准注解时,你应该清楚那些有意义的功能不能使用,如下表所示:

Springjavax.inject.*限制 / 说明
@Autowired@Inject@Inject 没有required属性,可用java8的optional替代
@Component@Named / @ManagedBean只能用来标明组件
@Scope(“singleton”)@SingletonJSR-330只提供了单例作用域的注解,要想将组件声明为其他作用域,需要使用spring的@Scope
@Qualifier@Qualifier / @Namedjavax.inject.Qualifier只是一个用于自定义注解的元注解,@Named可用于限定引入依赖
@Value-JSR-330没有类似的
@Required-JSR-330没有类似的
@Lazy-JSR-330没有类似的
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值