最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java的Spring框架中bean的繼承與內(nèi)部bean的注入

 更新時(shí)間:2015年12月05日 16:25:21   投稿:goldensun  
這篇文章主要介紹了Java的Spring框架中bean的繼承與內(nèi)部bean的注入,Spring框架是Java的SSH三大web開(kāi)發(fā)框架之一,需要的朋友可以參考下

bean的定義繼承
bean定義可以包含很多的配置信息,包括構(gòu)造函數(shù)的參數(shù),屬性值,比如初始化方法,靜態(tài)工廠方法名等容器的具體信息。

子bean定義從父定義繼承配置數(shù)據(jù)。子的定義可以覆蓋一些值,或者根據(jù)需要添加其他。

Spring bean定義繼承無(wú)關(guān),與Java類的繼承,但繼承的概念是一樣的。你可以定義一個(gè)父bean定義為模板和其他孩子bean可以從父bean繼承所需的配置。

當(dāng)使用基于XML的配置元數(shù)據(jù),指明一個(gè)子bean定義使用所在的當(dāng)前屬性指定的父bean作為這個(gè)屬性的值。

例如:
讓我們使用Eclipse IDE,然后按照下面的步驟來(lái)創(chuàng)建一個(gè)Spring應(yīng)用程序:

2015125162144585.png (594×321)

以下是我們定義的“HelloWorld”豆里面有兩個(gè)屬性message1和message2配置文件beans.xml中。下一步“helloIndia”豆已經(jīng)被定義為“HelloWorld”的子bean使用parent屬性。該子bean繼承message2屬性原狀,并覆蓋message1 屬性,并引入多一個(gè)屬性message3。

<?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-3.0.xsd">

 <bean id="helloWorld" class="com.yiibai.HelloWorld">
  <property name="message1" value="Hello World!"/>
  <property name="message2" value="Hello Second World!"/>
 </bean>

 <bean id="helloIndia" class="com.yiibai.HelloIndia"
  parent="helloWorld">
  <property name="message1" value="Hello India!"/>
  <property name="message3" value="Namaste India!"/>
 </bean>

</beans>

這里是HelloWorld.java 文件的內(nèi)容:

package com.yiibai;

public class HelloWorld {
 private String message1;
 private String message2;

 public void setMessage1(String message){
  this.message1 = message;
 }

 public void setMessage2(String message){
  this.message2 = message;
 }

 public void getMessage1(){
  System.out.println("World Message1 : " + message1);
 }

 public void getMessage2(){
  System.out.println("World Message2 : " + message2);
 }
}

這里是HelloIndia.java文件的內(nèi)容:

package com.yiibai;

public class HelloIndia {
 private String message1;
 private String message2;
 private String message3;

 public void setMessage1(String message){
  this.message1 = message;
 }

 public void setMessage2(String message){
  this.message2 = message;
 }

 public void setMessage3(String message){
  this.message3 = message;
 }

 public void getMessage1(){
  System.out.println("India Message1 : " + message1);
 }

 public void getMessage2(){
  System.out.println("India Message2 : " + message2);
 }

 public void getMessage3(){
  System.out.println("India Message3 : " + message3);
 }
}

以下是MainApp.java文件的內(nèi)容:

package com.yiibai;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {
 public static void main(String[] args) {
  ApplicationContext context = 
    new ClassPathXmlApplicationContext("Beans.xml");

  HelloWorld objA = (HelloWorld) context.getBean("helloWorld");

  objA.getMessage1();
  objA.getMessage2();

  HelloIndia objB = (HelloIndia) context.getBean("helloIndia");
  objB.getMessage1();
  objB.getMessage2();
  objB.getMessage3();
 }
}

創(chuàng)建完成源代碼和bean配置文件,讓我們運(yùn)行應(yīng)用程序。如果一切順利,這將打印以下信息:

World Message1 : Hello World!
World Message2 : Hello Second World!
India Message1 : Hello India!
India Message2 : Hello Second World!
India Message3 : Namaste India!

如果你在這里看到,我們沒(méi)有通過(guò)message2同時(shí)創(chuàng)建“helloIndia”的bean,但它通過(guò)了,因?yàn)閎ean定義的繼承。

bean定義模板:
您可以創(chuàng)建可以在不會(huì)花太多功夫被其他子bean定義的bean定義模板。在定義bean定義模板,不應(yīng)指定類屬性,并應(yīng)與真值指定如下所示的抽象屬性:

<?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-3.0.xsd">

 <bean id="beanTeamplate" abstract="true">
  <property name="message1" value="Hello World!"/>
  <property name="message2" value="Hello Second World!"/>
  <property name="message3" value="Namaste India!"/>
 </bean>

 <bean id="helloIndia" class="com.yiibai.HelloIndia"
  parent="beanTeamplate">
  <property name="message1" value="Hello India!"/>
  <property name="message3" value="Namaste India!"/>
 </bean>

</beans>

父bean不能被實(shí)例化它自己,因?yàn)樗遣煌暾?,而且它也明確地標(biāo)記為抽象。當(dāng)一個(gè)定義是抽象的這個(gè)樣子,它只是作為一個(gè)純粹的模板bean定義,充當(dāng)子定義的父定義使用。

注入內(nèi)部bean
正如你所知道的Java內(nèi)部類是其他類的范圍內(nèi)定義的,同樣,內(nèi)部bean是被其他bean的范圍內(nèi)定義的bean。因此<property/>或<constructor-arg/>元素內(nèi)<bean/>元件被稱為內(nèi)部bean和它如下所示。

<?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-3.0.xsd">

 <bean id="outerBean" class="...">
  <property name="target">
   <bean id="innerBean" class="..."/>
  </property>
 </bean>

</beans>

例如:
我們使用Eclipse IDE,然后創(chuàng)建一個(gè)Spring應(yīng)用程序,

這里是TextEditor.java文件的內(nèi)容:

package com.yiibai;

public class TextEditor {
 private SpellChecker spellChecker;

 // a setter method to inject the dependency.
 public void setSpellChecker(SpellChecker spellChecker) {
  System.out.println("Inside setSpellChecker." );
  this.spellChecker = spellChecker;
 }
 // a getter method to return spellChecker
 public SpellChecker getSpellChecker() {
  return spellChecker;
 }

 public void spellCheck() {
  spellChecker.checkSpelling();
 }
}

下面是另外一個(gè)相關(guān)的類文件SpellChecker.java內(nèi)容:

package com.yiibai;

public class SpellChecker {
 public SpellChecker(){
  System.out.println("Inside SpellChecker constructor." );
 }

 public void checkSpelling(){
  System.out.println("Inside checkSpelling." );
 }
 
}

以下是MainApp.java文件的內(nèi)容:

package com.yiibai;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {
 public static void main(String[] args) {
  ApplicationContext context = 
    new ClassPathXmlApplicationContext("Beans.xml");

  TextEditor te = (TextEditor) context.getBean("textEditor");

  te.spellCheck();
 }
}

以下是配置文件beans.xml文件里面有配置為基于setter 注入,但使用內(nèi)部bean:

<?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-3.0.xsd">

 <!-- Definition for textEditor bean using inner bean -->
 <bean id="textEditor" class="com.yiibai.TextEditor">
  <property name="spellChecker">
   <bean id="spellChecker" class="com.yiibai.SpellChecker"/>
  </property>
 </bean>

</beans>

創(chuàng)建源代碼和bean配置文件來(lái)完成,讓我們運(yùn)行應(yīng)用程序。如果一切順利,這將打印以下信息:

Inside SpellChecker constructor.
Inside setSpellChecker.
Inside checkSpelling.

相關(guān)文章

  • 一文搞懂JMeter engine中HashTree的配置問(wèn)題

    一文搞懂JMeter engine中HashTree的配置問(wèn)題

    本文主要介紹了JMeter engine中HashTree的配置,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • Mybatis-plus插入數(shù)據(jù)遇到主鍵沒(méi)有默認(rèn)值的情況

    Mybatis-plus插入數(shù)據(jù)遇到主鍵沒(méi)有默認(rèn)值的情況

    這篇文章主要介紹了Mybatis-plus插入數(shù)據(jù)遇到主鍵沒(méi)有默認(rèn)值的情況,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • 關(guān)于Java中的klass和class

    關(guān)于Java中的klass和class

    這篇文章主要介紹了關(guān)于Java中klass和class的區(qū)別,vm加載的字節(jié)碼,也就是.class文件,被加載到方法區(qū)里面,叫Kclass,是一個(gè)C++對(duì)象,含有類的信息、虛方法表等,需要的朋友可以參考下
    2023-08-08
  • Spring?main方法中如何調(diào)用Dao層和Service層的方法

    Spring?main方法中如何調(diào)用Dao層和Service層的方法

    這篇文章主要介紹了Spring?main方法中調(diào)用Dao層和Service層的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • Maven搭建springboot項(xiàng)目的方法步驟

    Maven搭建springboot項(xiàng)目的方法步驟

    這篇文章主要介紹了Maven搭建springboot項(xiàng)目的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-04-04
  • JAVA 十六進(jìn)制與字符串的轉(zhuǎn)換

    JAVA 十六進(jìn)制與字符串的轉(zhuǎn)換

    筆者前幾日在開(kāi)服過(guò)程中需要將字符串轉(zhuǎn)化成為16進(jìn)制的字符串,在網(wǎng)上找到了一些方法嘗試之后,均發(fā)現(xiàn)存在一個(gè)問(wèn)題-->字符串轉(zhuǎn)為16進(jìn)制后再轉(zhuǎn)回來(lái),英文正常,中文出現(xiàn)亂碼
    2009-05-05
  • java高級(jí)應(yīng)用:線程池的全面講解(干貨)

    java高級(jí)應(yīng)用:線程池的全面講解(干貨)

    這篇文章主要介紹了java高級(jí)應(yīng)用:線程池的全面講解(干貨),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-02-02
  • Java中List集合對(duì)象去重及按屬性去重的8種方法

    Java中List集合對(duì)象去重及按屬性去重的8種方法

    這篇文章主要介紹了Java中List集合對(duì)象去重及按屬性去重的8種方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一地的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-09-09
  • 淺談Java8 判空新寫法

    淺談Java8 判空新寫法

    在開(kāi)發(fā)過(guò)程中很多時(shí)候會(huì)遇到判空校驗(yàn),如果不做判空校驗(yàn)則會(huì)產(chǎn)生NullPointerException異常,本文就來(lái)介紹一下Java8 判空新寫法,感興趣的可以了解一下
    2021-09-09
  • 深入解析Java的Spring框架中bean的依賴注入

    深入解析Java的Spring框架中bean的依賴注入

    這篇文章主要介紹了Java的Spring框架中bean的依賴注入,講解了以構(gòu)造函數(shù)為基礎(chǔ)的依賴注入和基于setter方法的依賴注入的方式,需要的朋友可以參考下
    2015-12-12

最新評(píng)論

江安县| 永嘉县| 长海县| 汽车| 孝昌县| 深州市| 高雄市| 盖州市| 济宁市| 崇左市| 剑川县| 砀山县| 威远县| 砚山县| 犍为县| 吉安县| 鸡西市| 东丽区| 盐源县| 龙游县| 南和县| 洛南县| 辽宁省| 云龙县| 历史| 太谷县| 西乌珠穆沁旗| 唐山市| 伊宁县| 汾西县| 淮南市| 上饶市| 英德市| 陵川县| 华亭县| 疏勒县| 贞丰县| 新干县| 怀安县| 驻马店市| 喀喇沁旗|