Spring裡面xml檔案中bean的依賴和繼承

不要假裝很努力,結果會證明一切發表於2020-10-03

繼承

  • bean繼承:兩個類之間大多數的屬性都相同,避免重複配置,通過bean標籤的parent屬性重用已有的Bean元素的配置資訊
  • 繼承指的是配置資訊的複用,和java類的繼承沒有關係

video.java(父類

package net.cybclass.sp.domain;

import java.util.List;
import java.util.Map;

/**
 * @author: wangxiaobo
 * @create: 2020-10-03 15:08
 **/
public class Video {
//    public Video(int id, String title) {
//        System.out.println("Video 建構函式被呼叫");
//        this.id = id;
//        this.title = title;
//    }
    private int id;
    private String title;
//    private List<String> chapterList;
//    private Map<Integer, String> videoMap;
//    public List<String> getChapterList() {
//        return chapterList;
//    }
//
//    public void setChapterList(List<String> chapterList) {
//        this.chapterList = chapterList;
//    }
//
//    public Map<Integer, String> getVideoMap() {
//        return videoMap;
//    }
//
//    public void setVideoMap(Map<Integer, String> videoMap) {
//        this.videoMap = videoMap;
//    }
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        System.out.println("Video setTitle被呼叫");
        this.title = title;
    }
}

 Video2.java(子類)

package net.cybclass.sp.domain;

/**
 * @author: wangxiaobo
 * @create: 2020-10-03 19:16
 **/
public class Video2 {
    private int id;
    private String title;
    private String summary;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getSummary() {
        return summary;
    }

    public void setSummary(String summary) {
        this.summary = summary;
    }

    @Override
    public String toString() {
        return "Video2{" +
                "id=" + id +
                ", title='" + title + '\'' +
                ", summary='" + summary + '\'' +
                '}';
    }
}

 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"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
    <bean id="video" class="net.cybclass.sp.domain.Video" scope="prototype">
        <property name="id" value="9"></property>
        <property name="title" value="Spring5.X課程"></property>

    </bean>
    <bean id="video2" class="net.cybclass.sp.domain.Video2" parent="video">
        <property name="summary" value="這個是summary"></property>
    </bean>
</beans>

app.java

package net.cybclass.sp;

import net.cybclass.sp.domain.Video;
import net.cybclass.sp.domain.Video2;
import net.cybclass.sp.domain.VideoOrder;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
 * @author: wangxiaobo
 * @create: 2020-10-03 15:11
 **/
public class app {
    public static void main(String[] args) {
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
//        Video video=(Video) applicationContext.getBean("video");
//        VideoOrder videoOrder= (VideoOrder) applicationContext.getBean("videoOrder");
//        System.out.println(video.getTitle());
//        System.out.println(videoOrder.getVideo());
//        System.out.println(video.getChapterList());
//        System.out.println(video.getVideoMap());
        Video2 video=(Video2) applicationContext.getBean("video2");
        System.out.println(video);
    }
}

執行:

依賴關係

depends-on

<?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.xsd">
    <bean id="video" class="net.cybclass.sp.domain.Video">
        <property name="id" value="8"></property>
        <property name="title" value="SpringBoot課程專題"></property>
    </bean>
    <bean id="video2" class="net.cybclass.sp.domain.Video2" parent="video">
        <property name="summary" value="這個是summary"></property>
    </bean>
    <!--設定兩個bean的關係,video要先於videoOrder例項化-->
    <bean id="videoOrder" class="net.cybclass.sp.domain.VideoOrder" depends-on="video">
        <property name="id" value="8"></property>
        <property name="outTradeNo" value="12312"></property>
        <property name="video" ref="video"></property>
    </bean>
</beans>

 

相關文章