Spring参考文档翻译13--IOC容器11

1.11. Using JSR 330 Standard Annotations

使用 JSR 330 标准注释

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 ( Central Repository: 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>
如果您使用 Maven,则该javax.inject工件在标准 Maven 存储库 ( Central Repository: javax/inject/javax.inject/1 ) 中可用。您可以将以下依赖项添加到文件 pom.xml 中:

1.11.1. Dependency Injection with @Inject and @Named

依赖注入@Inject@Named

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

而不是@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(...);
        // ...
    }
}

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,从而允许按需访问范围更短的 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.Optionalor 一起使用@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

@Named和:注释@ManagedBean的标准等效项@Component

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

@Component您可以使用@javax.inject.Namedor代替javax.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;
    }
​
    // ...
}

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:

使用@Namedor@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

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.*javax.inject restrictions / comments
@Autowired@Inject@Inject has no 'required' attribute. Can be used with Java 8’s Optional instead.
@Inject没有“必需”属性。可以与 Java 8 一起使用Optional
@Component@Named / @ManagedBeanJSR-330 does not provide a composable model, only a way to identify named components.
JSR-330 不提供可组合模型,仅提供一种识别命名组件的方法。
@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.
JSR-330 默认范围类似于 Spring 的prototype. 但是,为了保持它与 Spring 的一般默认值一致,在 Spring 容器中声明的 JSR-330 bean 是singleton默认的。为了使用 以外的范围singleton,您应该使用 Spring 的@Scope注解。javax.inject还提供了一个 @Scope注解。然而,这个仅用于创建您自己的注释。
@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.
javax.inject.Qualifier只是用于构建自定义限定符的元注释。具体String的限定符(如@Qualifier带有值的 Spring)可以通过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.
javax.inject.Provider是 Spring 的直接替代品,只是方法名称ObjectFactory更短。get()它还可以与 Spring@Autowired或未注释的构造函数和 setter 方法结合使用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot 面试题通常涉及以下内容: 1. 什么是 Spring Boot? Spring Boot 是 Spring 的扩展,它旨在简化 Spring 应用程序的开发,消除了繁琐的配置。它提供了自动配置功能,使得开发者可以快速启动和运行应用程序。 2. Spring Boot 的优点是什么? - 简化配置:Spring Boot 提供了自动配置功能,通过约定大于配置的原则,减少了繁琐的配置工作。 - 快速开发:Spring Boot 提供了大量的启动器(starter),这些启动器包含了常用的库和框架,开发者可以快速集成和使用。 - 微服务支持:Spring Boot 对于构建微服务架构提供了良好的支持,包括服务注册与发现、负载均衡等。 - 健康检查与监控:Spring Boot 提供了 Actuator 执行器 API,可以用于监控应用程序的状态和性能指标。 3. Spring Boot 的核心特性有哪些? Spring Boot 提供了各种功能强大的启动器,包括但不限于: - spring-boot-starter-web:用于创建 REST API 的启动器。 - spring-boot-starter-data-jpa:用于连接 SQL 数据库的启动器。 - spring-boot-starter-data-mongodb:用于连接 MongoDB 的启动器。 - spring-boot-starter-aop:用于应用面向方面编程的启动器。 - spring-boot-starter-security:用于实现安全性,如基于角色的身份验证。 - spring-boot-starter-test:用于实现单元测试的启动器。 4. 什么是 Bean?在 Spring Boot 中如何使用 Bean? Bean 是指在 Spring Boot 上下文中由 Spring IOC 容器管理的对象。在 Spring Boot 中,我们可以使用 "@Bean" 注解来声明一个 Bean。使用该注解,我们可以创建并初始化一个普通的 Java 对象,并将其添加到 Spring 容器中进行管理。 以上是关于 Spring Boot 面试题的一些基本内容,希望对你有帮助。如需了解更多细节,请参考相关文档或进一步查阅资料。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值