1.11. Using JSR 330 Standard Annotations

上一节 1.10. Annotation-based Container Configuration

目录

下一节 1.12. Java-based Container Configuration

1.11. Using JSR 330 Standard Annotations

1.11. 使用JSR330标准标签

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.

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

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:

如果你使用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>
1.11.1. Dependency Injection with @Inject and @Named
1.11.1. 使用@Inject 和 @Named的依赖注入

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

可以使用@javax.inject来代替@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(...);
        // ...
    }
}

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。
此外,您可以将注入点声明为提供者,从而允许按需访问作用域较短的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(...);
        // ...
    }
}

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

与@Autowired一样,@Inject也可以用在java.util.Optional或@Nullable上。
这在这里甚至更适用,因为@Inject没有required属性。下面两个例子展示了如何使用@Inject和@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
1.11.2. @Named和@ManagedBean:与@Component等价标准注释

Instead of @Component, you can use @javax.inject.Named or javax.annotation.ManagedBean, as the following example shows:

可以使用@javax.inject.Named 或者 javax.annotation.ManagedBean 而不是@Component,如下面的例子所示:

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:

非常普遍的做法是在不指定组件名称的情况下使用@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;
    }

    // ...
}

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:

当您使用@Named或@ManagedBean时,您可以使用与使用Spring注释
完全相同的方式来使用组件扫描,如下面的示例所示:

@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.

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

1.11.3. Limitations of JSR-330 Standard Annotations
1.11.3. SR-330 Standard Annotations的局限性

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

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

Table 6. Spring component model elements versus JSR-330 variants
Springjavax.inject.*javax.inject restrictions / comments
@Autowired@Inject@Inject has no ‘required’ attribute. Can be used with Java 8’s Optional instead.
@Component@Named / @ManagedBeanJSR-330 does not provide a composable model, only a way to identify named components.
@Scope(“singleton”)@SingletonThe 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 / @Namedjavax.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
ObjectFactoryProviderjavax.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.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值