JAVA 核心筆記 || [xxx] Spring 之 Bean 繼承

Bean 繼承

用法

App.java

import com.mj.bean.BeanDog;
import com.mj.bean.BeanLife;
import com.mj.bean.BeanSay;
import com.mj.bean.BeanAnimal;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import java.io.FileNotFoundException;

public class App {



    public static void main(String args[]) throws FileNotFoundException{
        //ClassPathXmlApplicationContext
        ApplicationContext context = new ClassPathXmlApplicationContext("Bean.xml");
        BeanSay sa = (BeanSay) context.getBean("BeanSay");
        sa.setMsg("__Spring");
        sa.talk();

        //FileSystemXmlApplicationContext
        ApplicationContext fileContext = new FileSystemXmlApplicationContext("/src/Bean.xml");
        BeanSay sa1 = (BeanSay) fileContext.getBean("BeanSay");
        sa1.setMsg("=Spring====");
        sa1.talk();

        ApplicationContext animalContext = new ClassPathXmlApplicationContext("Bean.xml");
        BeanAnimal ani =  (BeanAnimal) animalContext.getBean("Animal");
        ani.setAnimalName("dog");
        ani.showAnimal();

        BeanAnimal animal =  (BeanAnimal) animalContext.getBean("Animal");
        animal.setAnimalName("pig");
        animal.showAnimal();

        // init method   destroy method
        BeanLife life = (BeanLife) context.getBean("BeanLife");
        life.showName();

        //BeanDog 繼承  BeanAnimal
        BeanDog dog = (BeanDog)context.getBean("BeanDog");
        dog.showAnimal();

    }
}


BeanAnimal.java

package com.mj.bean;

public class BeanAnimal {
    private String animalName = "-animal-";
    private String skin = "-animal skin-";

    public void setAnimalName(String animalName) {
        this.animalName = animalName;
    }

    public void setSkin(String skin) {
        this.skin = skin;
    }

    public  void showAnimal(){
        System.out.println(animalName);
        System.out.println(skin);
    }

    public  void showSkin(){
        System.out.println("=animal skin="+skin);
    }
}

BeanDog.java

package com.mj.bean;

public class BeanDog {

    private String animalName = "";
    private String skin = "";

    public void setSkin(String skin) {
        this.skin = skin;
    }

    public void setAnimalName(String animalName) {
        this.animalName = animalName;
    }

    public void showAnimal(){
        System.out.println(animalName);
        System.out.println(skin);
    }

    public  void init(){

    }

    public void destroy(){

    }
}

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

    <bean id = "BeanSay" class = "com.mj.bean.BeanSay">
        <property name = "msg" value = "===Spring==="/>
    </bean>

    <!-- scope 為 單例  -->
    <bean id = "Animal" class = "com.mj.bean.BeanAnimal" scope="singleton" >
        <property name = "animalName" value = "(_-_)"/>
        <property name="skin" value="animal skin" />
    </bean>

    <bean id = "BeanLife" class = "com.mj.bean.BeanLife" scope="prototype"  init-method="init" destroy-method="destroy">
        <property name = "beanName" value = "BeanLife"/>
    </bean>

    <bean id="BeanDog"  class="com.mj.bean.BeanDog" parent="Animal">
        <property name="skin" value="=dog skin=" />
    </bean>

</beans>

運行

(_-_)
=dog skin=
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1.1 Spring IoC容器和bean簡介 本章介紹了Spring Framework實現(xiàn)的控制反轉(zhuǎn)(IoC)...
    起名真是難閱讀 2,672評論 0 8
  • 1.1 spring IoC容器和beans的簡介 Spring 框架的最核心基礎(chǔ)的功能是IoC(控制反轉(zhuǎn))容器,...
    simoscode閱讀 6,851評論 2 22
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,569評論 19 139
  • 最近在看一本書,陽光姐姐小說總動員:《單翼天使》我和這本書的主人公很像,因為我在小時候爸爸就生病去世了,留下了我和...
    口述筆錄閱讀 591評論 0 1
  • 古巴印象
    風(fēng)輕揚_408b閱讀 191評論 0 0

友情鏈接更多精彩內(nèi)容