|
此版本仍在开发中,尚未稳定。如需最新的稳定版本,请使用 Spring Framework 7.0.6! |
使用 @Autowired
|
JSR 330 的 |
您可以将 @Autowired 注解应用于构造函数,如下例所示:
-
Java
-
Kotlin
public class MovieRecommender {
private final CustomerPreferenceDao customerPreferenceDao;
@Autowired
public MovieRecommender(CustomerPreferenceDao customerPreferenceDao) {
this.customerPreferenceDao = customerPreferenceDao;
}
// ...
}
class MovieRecommender @Autowired constructor(
private val customerPreferenceDao: CustomerPreferenceDao)
|
在一个目标bean仅定义了一个构造器的情况下,该构造器上的 |
您可以将@Autowired注解应用于传统的setter方法,如下例所示:
-
Java
-
Kotlin
public class SimpleMovieLister {
private MovieFinder movieFinder;
@Autowired
public void setMovieFinder(MovieFinder movieFinder) {
this.movieFinder = movieFinder;
}
// ...
}
class SimpleMovieLister {
@set:Autowired
lateinit var movieFinder: MovieFinder
// ...
}
您可以将@Autowired应用于具有任意名称和多个参数的方法,如下例所示:
-
Java
-
Kotlin
public class MovieRecommender {
private MovieCatalog movieCatalog;
private CustomerPreferenceDao customerPreferenceDao;
@Autowired
public void prepare(MovieCatalog movieCatalog,
CustomerPreferenceDao customerPreferenceDao) {
this.movieCatalog = movieCatalog;
this.customerPreferenceDao = customerPreferenceDao;
}
// ...
}
class MovieRecommender {
private lateinit var movieCatalog: MovieCatalog
private lateinit var customerPreferenceDao: CustomerPreferenceDao
@Autowired
fun prepare(movieCatalog: MovieCatalog,
customerPreferenceDao: CustomerPreferenceDao) {
this.movieCatalog = movieCatalog
this.customerPreferenceDao = customerPreferenceDao
}
// ...
}
您可以将 @Autowired 应用于字段,甚至可以将其与构造函数结合使用,如下例所示:
-
Java
-
Kotlin
public class MovieRecommender {
private final CustomerPreferenceDao customerPreferenceDao;
@Autowired
private MovieCatalog movieCatalog;
@Autowired
public MovieRecommender(CustomerPreferenceDao customerPreferenceDao) {
this.customerPreferenceDao = customerPreferenceDao;
}
// ...
}
class MovieRecommender @Autowired constructor(
private val customerPreferenceDao: CustomerPreferenceDao) {
@Autowired
private lateinit var movieCatalog: MovieCatalog
// ...
}
|
确保您的目标组件(例如, 对于通过XML定义的bean或通过类路径扫描找到的组件类,容器通常提前知道具体的类型。然而,对于使用 |
您还可以通过将 @Autowired 注解添加到期望该类型数组的字段或方法上来指示 Spring 从 ApplicationContext 提供该类型的全部 beans,如下例所示:
-
Java
-
Kotlin
public class MovieRecommender {
@Autowired
private MovieCatalog[] movieCatalogs;
// ...
}
class MovieRecommender {
@Autowired
private lateinit var movieCatalogs: Array<MovieCatalog>
// ...
}
类型集合也适用此规则,如下例所示:
-
Java
-
Kotlin
public class MovieRecommender {
private Set<MovieCatalog> movieCatalogs;
@Autowired
public void setMovieCatalogs(Set<MovieCatalog> movieCatalogs) {
this.movieCatalogs = movieCatalogs;
}
// ...
}
class MovieRecommender {
@Autowired
lateinit var movieCatalogs: Set<MovieCatalog>
// ...
}
|
您的目标Bean可以实现 您可以将 请注意,配置类上的 请注意,标准的 |
即使已键入的Map个实例,只要期望的键类型为String,也可以进行自动装配。
映射值都是预期类型的bean,键则是相应的bean名称,如下例所示:
-
Java
-
Kotlin
public class MovieRecommender {
private Map<String, MovieCatalog> movieCatalogs;
@Autowired
public void setMovieCatalogs(Map<String, MovieCatalog> movieCatalogs) {
this.movieCatalogs = movieCatalogs;
}
// ...
}
class MovieRecommender {
@Autowired
lateinit var movieCatalogs: Map<String, MovieCatalog>
// ...
}
默认情况下,当没有可用的匹配候选 Bean 时,自动装配会失败。在声明了数组、集合或映射的情况下,至少需要一个匹配的元素。
默认行为是将带注解的方法和字段视为指示必需的依赖项。您可以按照以下示例中的方法更改此行为,使框架通过将其标记为非必需来跳过无法满足的注入点(即,通过将 required 属性设置为 @Autowired 为 false):
-
Java
-
Kotlin
public class SimpleMovieLister {
private MovieFinder movieFinder;
@Autowired(required = false)
public void setMovieFinder(MovieFinder movieFinder) {
this.movieFinder = movieFinder;
}
// ...
}
class SimpleMovieLister {
@Autowired(required = false)
var movieFinder: MovieFinder? = null
// ...
}
|
如果其依赖项(或在多个参数的情况下,其中一个依赖项)不可用,则不会调用非必需方法。在这种情况下,非必需字段将不会被填充,保留其默认值。 换句话说,将 |
注入的构造函数和工厂方法参数是一个特殊情况,因为 required 属性在 @Autowired 中由于 Spring 的构造函数解析算法可能涉及多个构造函数,因此含义略有不同。构造函数和工厂方法参数默认是必需的,但在单个构造函数的情况下有一些特殊规则,例如多元素注入点(数组、集合、映射)如果没有匹配的 bean 可用,会解析为空实例。这允许一种常见的实现模式,即所有依赖项都可以在唯一的多参数构造函数中声明——例如,声明为一个没有 @Autowired 注解的公共构造函数。
|
任何给定的bean类只能有一个构造函数声明 |
另外,您也可以通过Java的java.util.Optional来表达某个依赖关系的非必需性,如下例所示:
public class SimpleMovieLister {
@Autowired
public void setMovieFinder(Optional<MovieFinder> movieFinder) {
...
}
}
您也可以使用参数级别的 @Nullable 注解(任何种类,来自任何包 — 例如,JSpecify 提供的 org.jspecify.annotations.Nullable)或直接利用 Kotlin 内置的空安全支持:
-
Java
-
Kotlin
public class SimpleMovieLister {
@Autowired
public void setMovieFinder(@Nullable MovieFinder movieFinder) {
...
}
}
class SimpleMovieLister {
@Autowired
var movieFinder: MovieFinder? = null
// ...
}
您也可以使用 @Autowired 来表示那些已知的可解析依赖接口:
BeanFactory、ApplicationContext、Environment、ResourceLoader、
ApplicationEventPublisher 和 MessageSource。这些接口及其扩展接口,
例如 ConfigurableApplicationContext 或 ResourcePatternResolver,会自动被解析,无需特殊配置。
下面的示例演示了如何自动注入一个 ApplicationContext 对象:
-
Java
-
Kotlin
public class MovieRecommender {
@Autowired
private ApplicationContext context;
public MovieRecommender() {
}
// ...
}
class MovieRecommender {
@Autowired
lateinit var context: ApplicationContext
// ...
}
|
数字 这些类型必须通过使用XML或Spring |