14_Xml继承

【工程截图】

 

【Person.java】

package com.HigginCui;

public class Person {
    private String name;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

【Studnet.java】

package com.HigginCui;

public class Student extends Person{
    public void studentSay(){
        System.out.println(this.getName());
    }
}

【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 id="person" class="com.HigginCui.Person">
        <property name="name">
            <value>HigginCui</value>
        </property>
    </bean>
    <!-- parent属性说明了student这个bean继承于person这个bean中的属性的值
     若不加parent="person"这句,testStudent中的输出值为null
--> <bean id="student" class="com.HigginCui.Student" parent="person"> </bean> </beans>

【testStudent.java】

package com.HigginCui.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.HigginCui.Student;

public class testStudent {
    @Test
    public void test(){
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        Student student=(Student) context.getBean("student");
        student.studentSay();
    }
}

【运行结果】

HigginCui

 

posted @ 2016-06-13 01:28  HigginCui  阅读(397)  评论(0编辑  收藏  举报