对于最新的稳定版本,请使用 Spring Framework 6.2.0spring-doc.cadn.net.cn

使用@Configuration注解

@Configuration是类级注解,指示对象是 bean 定义。@Configuration类通过@Bean-注释 方法。调用@Bean方法@Configuration类还可用于定义 bean 间依赖关系。看基本概念:@Bean@Configuration以获取一般介绍。spring-doc.cadn.net.cn

注入 bean 间依赖关系

当 bean 彼此依赖时,表达这种依赖性就很简单 就像让一个 bean 方法调用另一个 bean 方法一样,如下例所示:spring-doc.cadn.net.cn

@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通过构造函数 注射。spring-doc.cadn.net.cn

这种声明 bean 间依赖关系的方法仅在@Bean方法 在@Configuration类。不能声明 bean 间依赖关系 通过使用 Plain@Component类。

查找方法注入

如前所述,查找方法注入是一个 您应该很少使用的高级功能。它在 singleton 范围的 bean 依赖于原型范围的 bean。使用 Java 实现此目的 type 配置为实现此模式提供了一种自然的方法。这 以下示例显示了如何使用 Lookup 方法注入:spring-doc.cadn.net.cn

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()方法被覆盖,以便它查找新的 (prototype) 命令对象。以下示例显示了如何执行此作:spring-doc.cadn.net.cn

@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带注释的方法被调用两次:spring-doc.cadn.net.cn

@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.在子类中,子方法首先检查容器是否有任何 在调用 Parent 方法并创建新实例之前缓存 (作用域) bean。spring-doc.cadn.net.cn

根据 Bean 的范围,行为可能会有所不同。我们正在交谈 关于单例 这里.

没有必要将 CGLIB 添加到您的类路径中,因为 CGLIB 类已重新打包 在org.springframework.cglib包中,并直接包含在spring-core罐。spring-doc.cadn.net.cn

由于 CGLIB 在 startup-time 的 Startup-time 中。特别是,配置类不能是 final。但是,任何 允许在配置类上使用构造函数,包括使用@Autowired或 用于 default 注入的单个非 default 构造函数声明。spring-doc.cadn.net.cn

如果您希望避免任何 CGLIB 施加的限制,请考虑声明您的@Beannon-@Configuration类(例如,在 PLAIN 上@Component类 ),或者使用@Configuration(proxyBeanMethods = false).之间的跨方法调用@Bean方法 不会被拦截,因此您必须完全依赖 constructor 或 method 级别。spring-doc.cadn.net.cn