美文网首页
JAVA 核心笔记 || [xxx] Spring 之 Bean

JAVA 核心笔记 || [xxx] Spring 之 Bean

作者: 魔介 | 来源:发表于2018-12-04 13:35 被阅读0次

Bean 生命周期

  • init-method 配置初始化调用方法
  • destroy-method 配置销毁调用方法

用法

App.java

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();

    }
}

BeanLife.java

package com.mj.bean;

public class BeanLife {
    private  String beanName = "BeanName=BeanLife";

    public void setBeanName(String beanName) {
        this.beanName = beanName;
    }

    public  void showName(){
        System.out.println("=Bean Name="+beanName);
    }

    public void init(){
        System.out.println("[BeanLife init]");
    }

    public void destroy(){
        System.out.println("[BeanLife 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 = "(_-_)"/>
    </bean>

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

</beans>

运行

[BeanLife init]
=Bean Name=BeanLife

相关文章

网友评论

      本文标题:JAVA 核心笔记 || [xxx] Spring 之 Bean

      本文链接:https://www.haomeiwen.com/subject/ovumcqtx.html