使用 @Configuration 注解
@Configuration 是一个类级别的注解,表示一个对象是 bean 定义的来源。@Configuration 类通过带有 @Bean 注解的方法声明 bean。对 @Configuration 类上的 @Bean 方法的调用也可以用于定义 bean 间的依赖关系。有关一般性介绍,请参见基本概念:@Bean 和 @Configuration。
注入 Bean 间依赖
当 bean 之间存在依赖关系时,表达这种依赖关系就像一个 bean 方法调用另一个方法一样简单,如下例所示
-
Java
-
Kotlin
@Configuration
public class AppConfig {
@Bean
public BeanOne beanOne() {
return new BeanOne(beanTwo());
}
@Bean
public BeanTwo beanTwo() {
return new BeanTwo();
}
}
@Configuration
class AppConfig {
@Bean
fun beanOne() = BeanOne(beanTwo())
@Bean
fun beanTwo() = BeanTwo()
}
在前面的示例中,beanOne 通过构造函数注入接收对 beanTwo 的引用。
这种声明 bean 间依赖关系的方法仅在 @Bean 方法在 @Configuration 类中声明时才有效。您不能通过使用普通的 @Component 类来声明 bean 间依赖关系。 |
查找方法注入
如前所述,查找方法注入是一种高级功能,您应很少使用。它在单例作用域的 bean 依赖于原型作用域的 bean 的情况下很有用。使用 Java 进行这种类型的配置提供了实现此模式的自然方式。以下示例展示了如何使用查找方法注入
-
Java
-
Kotlin
public abstract class CommandManager {
public Object process(Object commandState) {
// grab a new instance of the appropriate Command interface
Command command = createCommand();
// set the state on the (hopefully brand new) Command instance
command.setState(commandState);
return command.execute();
}
// okay... but where is the implementation of this method?
protected abstract Command createCommand();
}
abstract class CommandManager {
fun process(commandState: Any): Any {
// grab a new instance of the appropriate Command interface
val command = createCommand()
// set the state on the (hopefully brand new) Command instance
command.setState(commandState)
return command.execute()
}
// okay... but where is the implementation of this method?
protected abstract fun createCommand(): Command
}
通过使用 Java 配置,您可以创建 CommandManager 的子类,其中抽象的 createCommand() 方法被重写,使其查找新的(原型)命令对象。以下示例展示了如何实现
-
Java
-
Kotlin
@Bean
@Scope("prototype")
public AsyncCommand asyncCommand() {
AsyncCommand command = new AsyncCommand();
// inject dependencies here as required
return command;
}
@Bean
public CommandManager commandManager() {
// return new anonymous implementation of CommandManager with createCommand()
// overridden to return a new prototype Command object
return new CommandManager() {
protected Command createCommand() {
return asyncCommand();
}
}
}
@Bean
@Scope("prototype")
fun asyncCommand(): AsyncCommand {
val command = AsyncCommand()
// inject dependencies here as required
return command
}
@Bean
fun commandManager(): CommandManager {
// return new anonymous implementation of CommandManager with createCommand()
// overridden to return a new prototype Command object
return object : CommandManager() {
override fun createCommand(): Command {
return asyncCommand()
}
}
}
关于 Java 配置内部工作原理的更多信息
考虑以下示例,它展示了一个带有 @Bean 注解的方法被调用两次
-
Java
-
Kotlin
@Configuration
public class AppConfig {
@Bean
public ClientService clientService1() {
ClientServiceImpl clientService = new ClientServiceImpl();
clientService.setClientDao(clientDao());
return clientService;
}
@Bean
public ClientService clientService2() {
ClientServiceImpl clientService = new ClientServiceImpl();
clientService.setClientDao(clientDao());
return clientService;
}
@Bean
public ClientDao clientDao() {
return new ClientDaoImpl();
}
}
@Configuration
class AppConfig {
@Bean
fun clientService1(): ClientService {
return ClientServiceImpl().apply {
clientDao = clientDao()
}
}
@Bean
fun clientService2(): ClientService {
return ClientServiceImpl().apply {
clientDao = clientDao()
}
}
@Bean
fun clientDao(): ClientDao {
return ClientDaoImpl()
}
}
clientDao() 在 clientService1() 中被调用一次,在 clientService2() 中被调用一次。由于此方法创建并返回 ClientDaoImpl 的新实例,您通常会期望有两个实例(每个服务一个)。这肯定会有问题:在 Spring 中,实例化后的 bean 默认具有 singleton 作用域。这就是神奇之处:所有 @Configuration 类在启动时都通过 CGLIB 被子类化。在子类中,子方法在调用父方法并创建新实例之前,会首先检查容器中是否存在任何缓存的(作用域)bean。
| 该行为可能因 bean 的作用域而异。我们这里讨论的是单例。 |
|
无需将 CGLIB 添加到您的类路径中,因为 CGLIB 类已在 |
|
由于 CGLIB 在启动时动态添加功能,因此存在一些限制。特别是,配置类不能是 final 的。但是,配置类允许任何构造函数,包括使用 如果您希望避免 CGLIB 施加的任何限制,请考虑在非 |