Spring裡面xml檔案中bean的依賴和繼承
繼承
- 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>
相關文章
- Spring:Bean的scope作用域案例講解以及Bean之間的依賴和繼承(3)SpringBean繼承
- Spring Bean 定義繼承SpringBean繼承
- Spring 中 bean 的迴圈依賴SpringBean
- maven中的傳遞依賴和傳遞依賴的解除Maven
- Scala 多繼承 & 依賴注入繼承依賴注入
- Struts 2 攔截器中得到bean以及讀取WEB-INF下的spring的xml檔案BeanWebSpringXML
- 深入淺出Spring Boot 起步依賴和自動配置Spring Boot
- Spring 配置繼承Spring繼承
- Spring-Context之九:在bean定義中使用繼承SpringContextBean繼承
- C++中公有繼承、保護繼承、私有繼承的區別C++繼承
- HIBERNATE裡面怎麼繼承一對多的關係呢繼承
- spring中bean.xml的http://www.springframework.org/schema/bean報錯SpringBeanXMLHTTPFramework
- JavaScript中的繼承JavaScript繼承
- PostgreSQL中的繼承SQL繼承
- JS中的繼承JS繼承
- java中的繼承Java繼承
- 【Spark篇】--Spark中的寬窄依賴和Stage的劃分Spark
- C++標準庫中檔案流類的繼承關係C++繼承
- Spring系列第十三講 使用繼承簡化bean配置(abstract & parent)Spring繼承Bean
- 獲取spring裡的beanSpringBean
- java中繼承Java中繼繼承
- Java Spring Beans.xml裡的Bean定義是如何被解析出來的JavaSpringBeanXML
- odoo 繼承(owl繼承、web繼承、view繼承)Odoo繼承WebView
- 聊聊JS中的繼承JS繼承
- JS中的繼承(上)JS繼承
- JS中的繼承(下)JS繼承
- Solidity中的繼承Solid繼承
- C++中的繼承C++繼承
- spring application.xml中載入配置檔案SpringAPPXML
- onethink自定義外掛 怎麼在模板裡面使用模板的繼承?繼承
- Spring中Bean及@Bean的理解SpringBean
- 面對面講述JavaScript之繼承的實現JavaScript繼承
- 再探迴圈依賴 → Spring 是如何判定原型迴圈依賴和構造方法迴圈依賴的?Spring原型構造方法
- Spring中的BeanSpringBean
- Spring系列第四講 xml中bean定義詳解SpringXMLBean
- Spring AOP 在 XML檔案中實現 AspectJ 攔截SpringXML
- maven新建Spring MVC + MyBatis + Oracle的Web專案中pom.xml檔案MavenSpringMVCMyBatisOracleWebXML
- 怎麼解析 xml 檔案,把裡面某個元素,自動生成其 xpathXML