父类Dao:
public class CommonDao {
private String a;
public void saveEntity() {
System.out.println("saveEntity----");
}
public String getA() {
return a;
}
public void setA(String a) {
this.a = a;
}
}
子类Dao:
public class PersonDao extends CommonDao {
}
applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<!-- 当一个bean标签中设置了abstract="true",则表示这个类不能被创建对象 -->
<bean id="commonDao" class="com.xxc.spring.exetend.CommonDao" abstract="true">
<property name="a" value="xxc"></property>
</bean>
<!-- 如果personDao在继承commonDao的同时想拥有commonDao里的属性值的时候,需要在子类的bean中配置 parent="commonDao",否则拿父类的属性为null -->
<bean id="personDao" class="com.xxc.spring.exetend.PersonDao" parent="commonDao"></bean>
</beans>
public class TestExtend {
@Test
public void testExtend(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
PersonDao personDao = (PersonDao)applicationContext.getBean("personDao");
System.out.println(personDao.getA());//如果想拥有父类的属性,那么在子类的bean标签中必须配置parent属性
personDao.saveEntity();
}
}