Injection with @Resource

Spring 还支持在字段或 Bean 属性 setter 方法上使用 JSR-250 @Resource 注解(jakarta.annotation.Resource)进行注入。这是 Jakarta EE 中的常见模式:例如,在 JSF 管理的 Bean 和 JAX-WS 端点中。Spring 也支持此模式的 Spring 管理对象。

Spring also supports injection by using the JSR-250 @Resource annotation (jakarta.annotation.Resource) on fields or bean property setter methods. This is a common pattern in Jakarta EE: for example, in JSF-managed beans and JAX-WS endpoints. Spring supports this pattern for Spring-managed objects as well.

@Resource 获取一个名称属性。默认情况下,Spring 将该值解释为要注入的 Bean 名称。换句话说,它遵循按名称语义,如下面的示例所示:

@Resource takes a name attribute. By default, Spring interprets that value as the bean name to be injected. In other words, it follows by-name semantics, as demonstrated in the following example:

Java
public class SimpleMovieLister {

	private MovieFinder movieFinder;

	@Resource(name="myMovieFinder") (1)
	public void setMovieFinder(MovieFinder movieFinder) {
		this.movieFinder = movieFinder;
	}
}
1 This line injects a @Resource.
Kotlin
class SimpleMovieLister {

	@Resource(name="myMovieFinder") (1)
	private lateinit var movieFinder:MovieFinder
}
2 This line injects a @Resource.

如果没有明确指定名称,则默认名称将从字段名称或 setter 方法派生。对于字段,它获取字段名称。对于 setter 方法,它获取 Bean 属性名称。以下示例将在其 setter 方法中注入名为 movieFinder 的 Bean:

If no name is explicitly specified, the default name is derived from the field name or setter method. In case of a field, it takes the field name. In case of a setter method, it takes the bean property name. The following example is going to have the bean named movieFinder injected into its setter method:

  • Java

  • Kotlin

public class SimpleMovieLister {

	private MovieFinder movieFinder;

	@Resource
	public void setMovieFinder(MovieFinder movieFinder) {
		this.movieFinder = movieFinder;
	}
}
class SimpleMovieLister {

	@set:Resource
	private lateinit var movieFinder: MovieFinder

}

使用注释提供的名称被 ApplicationContext 解析为一个 bean 名称,CommonAnnotationBeanPostProcessor 了解该名称。如果您显式配置 Spring 的https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/jndi/support/SimpleJndiBeanFactory.html[SimpleJndiBeanFactory],这些名称可以通过 JNDI 解析。但是,我们建议您依赖默认行为并使用 Spring 的 JNDI 查找功能来保留间接级别。

The name provided with the annotation is resolved as a bean name by the ApplicationContext of which the CommonAnnotationBeanPostProcessor is aware. The names can be resolved through JNDI if you configure Spring’s SimpleJndiBeanFactory explicitly. However, we recommend that you rely on the default behavior and use Spring’s JNDI lookup capabilities to preserve the level of indirection.

在没有明确指定名称的情况下独家使用 @Resource 时,与 @Autowired 类似,@Resource 将查找主类型匹配,而不是特定命名的 Bean,并解析众所周知的可解析依赖项:BeanFactoryApplicationContextResourceLoaderApplicationEventPublisherMessageSource 接口。

In the exclusive case of @Resource usage with no explicit name specified, and similar to @Autowired, @Resource finds a primary type match instead of a specific named bean and resolves well known resolvable dependencies: the BeanFactory, ApplicationContext, ResourceLoader, ApplicationEventPublisher, and MessageSource interfaces.

因此,在以下示例中,customerPreferenceDao 字段首先查找名为“customerPreferenceDao”的 bean,然后回退到类型`CustomerPreferenceDao` 的主类型匹配:

Thus, in the following example, the customerPreferenceDao field first looks for a bean named "customerPreferenceDao" and then falls back to a primary type match for the type CustomerPreferenceDao:

Java
public class MovieRecommender {

	@Resource
	private CustomerPreferenceDao customerPreferenceDao;

	@Resource
	private ApplicationContext context; (1)

	public MovieRecommender() {
	}

	// ...
}
1 The context field is injected based on the known resolvable dependency type: ApplicationContext.
Kotlin
class MovieRecommender {

	@Resource
	private lateinit var customerPreferenceDao: CustomerPreferenceDao


	@Resource
	private lateinit var context: ApplicationContext (1)

	// ...
}
2 The context field is injected based on the known resolvable dependency type: ApplicationContext.