通过在XML中的<bean>里配置depends-on属性或者在一个类上使用注解@DependsOn,可以使一个Bean的产生依赖于其他几个Bean。请看如下代码:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="son" class="com.tyyd.lifecallbacks.domain.Son" depends-on="mother"></bean>
<bean id="mother" class="com.tyyd.lifecallbacks.domain.Mother"></bean>
</beans>
son这个Bean的产生依赖于mother这个Bean
本文详细介绍了在Spring框架中如何通过XML配置文件的<bean>元素或使用@DependsOn注解来实现Bean之间的依赖关系。通过具体示例展示了son Bean如何依赖于mother Bean的初始化。

3108

被折叠的 条评论
为什么被折叠?



