关于继承
1、如果一个类在spring配置文件中,但是不想让整个类创建对象,则用abstract="true"
2、如果让一个子类拥有父类的属性,则parent="父类id"
例子
spring的配置文件applicationContext.xml
<?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-2.5.xsd"> <bean id="parent" class="extend.Parent" abstract="true"> <property name="s" value="sssssssss"></property> </bean> <bean id="son" class="extend.Son" parent="parent"></bean> </beans>
父类Parent
public class Parent { public String s; public String getS() { return s; } public void setS(String s) { this.s = s; } public void show(){ System.out.println("parent show"); } }
子类Son
public class Son extends Parent{ public void printS(){ System.out.println(getS()); } }
测试类
public class Test_extend { @Test public void test(){ ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); Son son = (Son) context.getBean("son"); son.show(); son.printS(); } }
时间: 2024-10-20 21:21:21