使用 @Configuration 注解

@Configuration 是一个类级别的注解,指示对象是 bean 定义的来源。@Configuration 类通过 @Bean 注解的方法声明 bean。对 @Configuration 类上的 @Bean 方法的调用也可以用于定义 bean 间的依赖。有关一般性介绍,请参阅 基本概念:@Bean@Configuration

注入 Bean 间依赖

当 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 类已在 org.springframework.cglib 包下重新打包,并直接包含在 spring-core JAR 中。

由于 CGLIB 在启动时动态添加功能,因此存在一些限制。特别是,配置类不能是最终的(final)。但是,配置类允许任何构造函数,包括使用 @Autowired 或声明单个非默认构造函数进行默认注入。 如果您更喜欢避免任何 CGLIB 施加的限制,请考虑在非 @Configuration 类(例如,在普通的 @Component 类中)上声明您的 @Bean 方法,或者通过使用 @Configuration(proxyBeanMethods = false) 注解您的配置类。在这种情况下,@Bean 方法之间的跨方法调用将不会被拦截,因此您必须完全依赖于构造函数或方法级别的依赖注入。