如果没有继承,那么这两个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"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="address" class="com.atguigu.spring.autowire.Address"
p:city="beijing" p:street="nanjingroad"></bean>
<bean id="address2" class="com.atguigu.spring.autowire.Address"
p:city="beijing" p:street="dongjingroad"></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"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="address" class="com.atguigu.spring.autowire.Address"
p:city="beijing" p:street="nanjingroad"></bean>
<!--bean的继承,继承自address-->
<bean id="address2" p:street="dongjingroad" parent="address"></bean>
</beans>
可以写一个专门的抽象类型的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"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 抽象的bean,专门用来被继承的 -->
<bean id="address" class="com.atguigu.spring.autowire.Address"
p:city="beijing" abstract="true"></bean>
<!--bean的继承,继承自address-->
<bean id="address1" p:street="nanjingroad" parent="address"></bean>
<bean id="address2" p:street="dongjingroad" parent="address"></bean>
</beans>