码迷,mamicode.com
首页 > 编程语言 > 详细

Spring里面bean的依赖和继承

时间:2020-07-15 01:17:46      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:odi   trade   img   ring   domain   override   span   import   loading   

继承

  • bean继承:两个类之间大多数的属性都相同,避免重复配置,通过bean标签的parent属性重用已有的Bean元素的配置信息
  • 继承指的是配置信息的复用,和java类的继承没有关系

video.java(父类

package net.cybclass.sp.domain;

public class Video {
    private int id;
    private String title;
    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;
    }
}

Video2.java(子类)

package net.cybclass.sp.domain;

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"
       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>
</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;

public class app {
    public static void main(String[] args) {
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
//        VideoOrder video=(VideoOrder) applicationContext.getBean("videoOrder");
//        System.out.println(video.getVideo());

        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>

 

Spring里面bean的依赖和继承

标签:odi   trade   img   ring   domain   override   span   import   loading   

原文地址:https://www.cnblogs.com/chenyanbin/p/13303172.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!