Spring基于XML的DI-异类抽象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="xiaoming" class="com.hk.spring.di12.Student">
<property name="name" value="小明"/>
<property name="age" value="18"/>
<property name="school" value="九江学院"/>
<property name="department" value="计算机科学与技术学院"/>
</bean>
<bean id="teacher" class="com.hk.spring.di12.Teacher">
<property name="name" value="Mike"/>
<property name="workAge" value="8"/>
<property name="school" value="九江学院"/>
<property name="department" value="计算机科学与技术学院"/>
</bean>
</beans>
发现有冗余,不过类不一样,优化
优化
<?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 -->
<bean id="baseBean" abstract="true">
<property name="school" value="九江学院"/>
<property name="department" value="计算机科学与技术学院"/>
</bean>
<bean id="xiaoming" class="com.hk.spring.di12.Student" parent="baseBean">
<property name="name" value="小明"/>
<property name="age" value="18"/>
</bean>
<bean id="teacher" class="com.hk.spring.di12.Teacher" parent="baseBean">
<property name="name" value="Mike"/>
<property name="workAge" value="8"/>
</bean>
</beans>