bean继承:
两个类之间大多数的属性都相同,避免重复配置,通过bean标签的parent属性重用已
有的Bean元素的配置信息 继承指的是配置信息的复用,和Java类的继承没有关系
代码:
<bean id="video" class="work.yspan.sp.domain.Video" scope="singleton">
<property name="id" value="9"/>
<property name="title" value="Spring 5.x课程"/>
</bean>
<bean id="video2" class="work.yspan.sp.domain.Video2" parent="video">
<property name="summary" value="这是summary"></property>
</bean>
测试代码:
private static void testExtend(ApplicationContext applicationContext){
Video2 video2=(Video2) applicationContext.getBean("video2");
System.out.println(video2.getSummary());
System.out.println(video2.getTitle());
System.out.println(video2.getId());
}
测试效果截图:
属性依赖:
如果类A是作为类B的属性, 想要类A比类B先实例化,设置两个Bean的依赖关系
<bean id="video" class="work.yspan.sp.domain.Video" scope="singleton">
<property name="id" value="9"/>
<property name="title" value="Spring 5.x课程"/>
</bean>
<bean id="videoOrder" class="work.yspan.sp.domain.VideoOrder" depends-on="video">
<property name="id" value="8"/>
<property name="outTradeNo" value="165xa1s1afa51"/>
<property name="video" ref="video"/>
</bean>