<?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-3.0.xsd">
[color=gray]<!-- 配置父Bean -->[/color]
<bean id="superBean" abstract="true">
<property name="name" value="Bruce"/>
</bean>
[color=gray]<!-- 从父Bean继承name属性 -->[/color]
<bean id="goods1" class="com.bean.Goods" parent="superBean">
<property name="category" value="6"/>
</bean>
[color=gray]<!-- 覆盖父Bean继承的name属性 -->[/color]
<bean id="goods2" class="com.bean.Goods" parent="superBean">
<property name="name" value="Phil"/>
<property name="category" value="6"/>
</bean>
</beans>
//Goods.java文件
package com.bean;
public class Goods {
private String name;
private Integer category;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getCategory() {
return category;
}
public void setCategory(Integer category) {
this.category = category;
}
public String toString() {
return "Name:" + getName() + " Category:" + getCategory();
}
}
//测试
public static void main(String[] args) {
XmlBeanFactory app = new XmlBeanFactory(new ClassPathResource("beans-config.xml"));
Goods g = (Goods)app.getBean("goods1");
System.out.println(g);
Goods g2 = (Goods)app.getBean("goods2");
System.out.println(g2);
}
/**Output:
Name:Bruce Category:6
Name:Phil Category:6
*/
<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-3.0.xsd">
[color=gray]<!-- 配置父Bean -->[/color]
<bean id="superBean" abstract="true">
<property name="name" value="Bruce"/>
</bean>
[color=gray]<!-- 从父Bean继承name属性 -->[/color]
<bean id="goods1" class="com.bean.Goods" parent="superBean">
<property name="category" value="6"/>
</bean>
[color=gray]<!-- 覆盖父Bean继承的name属性 -->[/color]
<bean id="goods2" class="com.bean.Goods" parent="superBean">
<property name="name" value="Phil"/>
<property name="category" value="6"/>
</bean>
</beans>
//Goods.java文件
package com.bean;
public class Goods {
private String name;
private Integer category;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getCategory() {
return category;
}
public void setCategory(Integer category) {
this.category = category;
}
public String toString() {
return "Name:" + getName() + " Category:" + getCategory();
}
}
//测试
public static void main(String[] args) {
XmlBeanFactory app = new XmlBeanFactory(new ClassPathResource("beans-config.xml"));
Goods g = (Goods)app.getBean("goods1");
System.out.println(g);
Goods g2 = (Goods)app.getBean("goods2");
System.out.println(g2);
}
/**Output:
Name:Bruce Category:6
Name:Phil Category:6
*/