通过在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