|
对于最新稳定版本,请使用 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)
|
从 Spring Framework 4.3 开始,如果目标 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
// ...
}
你也可以将该注解应用于具有任意名称和多个参数的方法上,如下例所示:
-
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 或通过类路径扫描发现的组件类,容器通常能提前知道其具体类型。然而,对于 |
你还可以通过在期望接收该类型数组的字段或方法上添加 ApplicationContext 注解,指示 Spring 从 @Autowired 中提供所有特定类型的 bean,如下例所示:
-
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,也可以被自动装配。
该 Map 的值包含所有符合期望类型的 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 8 的 java.util.Optional 来表达某个特定依赖项的非必需性,如下例所示:
public class SimpleMovieLister {
@Autowired
public void setMovieFinder(Optional<MovieFinder> movieFinder) {
...
}
}
从 Spring Framework 5.0 开始,你还可以使用 @Nullable 注解(任意包中的任意类型的注解——例如来自 JSR-305 的 javax.annotation.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
// ...
}
|
|