Spring课程 Spring入门篇 3-2 Spring bean装配(上)之bean的生命周期

本节主要讲了三大块内容

1    bean的生命周期概念

2    bean的初始化和销毁的三种方式对比(代码演练)

3    总结

1    bean的生命周期概念

1.1  bean的定义:xml中关于bean的配置,bean的id和bean的class等。

1.2  bean的初始化:ioc容器启动的时候加载xml文件中的bean生成实例.

1.3  bean的使用:bean容器中取出bean的实例并使用

1.4  bean销毁:指的是bean销毁时回收由这个bean创建的所有bean实例。

2    bean的初始化和销毁的三种方式对比(代码演练)

2.1  xml配置init-method方法

init-method方法实现类:

package com.imooc.lifecycle;

public class BeanLifeCycle {

    //单独bean方法初始化
    public void start(){
        System.out.println("单独bean,init方法执行");
    }

    public void stop(){
        System.out.println("单独bean,destroy方法执行");
    }

}

init-method方法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"
default-init-method="init" default-destroy-method="destroy">

<bean id="beanLifeCycle" class="com.imooc.lifecycle.BeanLifeCycle"  init-method="start" destroy-method="stop"></bean> 

</beans>

测试类:

package com.imooc.lifecycle;

import org.junit.Test;
import org.junit.internal.runners.JUnit4ClassRunner;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.springframework.cglib.core.Block;

import com.imooc.test.base.UnitTestBase;

@RunWith(BlockJUnit4ClassRunner.class)
public class TestLifeCycle extends UnitTestBase{

    public TestLifeCycle() {
        super("classpath*:spring-beanLifeCycle.xml");
        // TODO Auto-generated constructor stub
    }

    @Test
    public void testLifeCycle(){
         super.getbean("beanLifeCycle");
    }

}

2.2    实现接口 bean的初始化和销毁 方法

实现类:

package com.imooc.lifecycle;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

public class BeanLifeCycle implements InitializingBean,DisposableBean{

    /**
     * 实现接口,覆盖bean 的 初始化和销毁方法
     */
    @Override
    public void afterPropertiesSet() throws Exception {
        // TODO Auto-generated method stub
        System.out.println("实现接口,这里完成bean的初始化方法");
    }

    @Override
    public void destroy() throws Exception {
        // TODO Auto-generated method stub
        System.out.println("实现接口,这里完成bean的销毁方法");

    }

    /*//单独bean方法初始化
    public void start(){
        System.out.println("单独bean,init方法执行");
    }

    public void stop(){
        System.out.println("单独bean,destroy方法执行");
    }*/

}

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"
default-init-method="init" default-destroy-method="destroy">

<!-- init-method="start" destroy-method="stop" -->
<bean id="beanLifeCycle" class="com.imooc.lifecycle.BeanLifeCycle"></bean> 

</beans>

测试类:

package com.imooc.lifecycle;

import org.junit.Test;
import org.junit.internal.runners.JUnit4ClassRunner;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.springframework.cglib.core.Block;

import com.imooc.test.base.UnitTestBase;

@RunWith(BlockJUnit4ClassRunner.class)
public class TestLifeCycle extends UnitTestBase{

    public TestLifeCycle() {
        super("classpath*:spring-beanLifeCycle.xml");
        // TODO Auto-generated constructor stub
    }

    @Test
    public void testLifeCycle(){
         super.getbean("beanLifeCycle");
    }

}

2.3  全局初始化销毁方法

实现类:

package com.imooc.lifecycle;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

public class BeanLifeCycle {

    /**
     * 全局初始化销毁方法
     */
    //init方法名和xml中的配置相关
    private void init() {
        // TODO Auto-generated method stub
        System.out.println("全局初始化方法!");
    }

    //destroy方法名和xml中的配置相关
    private void destroy() {
        // TODO Auto-generated method stub
        System.out.println("全局销毁方法!");
    }

    /**
     * 实现接口,覆盖bean 的 初始化和销毁方法
     */
    /*@Override
    public void afterPropertiesSet() throws Exception {
        // TODO Auto-generated method stub
        System.out.println("实现接口,这里完成bean的初始化方法");
    }

    @Override
    public void destroy() throws Exception {
        // TODO Auto-generated method stub
        System.out.println("实现接口,这里完成bean的销毁方法");

    }*/

    /*//单独bean方法初始化
    public void start(){
        System.out.println("单独bean,init方法执行");
    }

    public void stop(){
        System.out.println("单独bean,destroy方法执行");
    }*/

}

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"
default-init-method="init" default-destroy-method="destroy">

<!-- init-method="start" destroy-method="stop" -->
<bean id="beanLifeCycle" class="com.imooc.lifecycle.BeanLifeCycle"></bean> 

</beans>

测试类:

package com.imooc.lifecycle;

import org.junit.Test;
import org.junit.internal.runners.JUnit4ClassRunner;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.springframework.cglib.core.Block;

import com.imooc.test.base.UnitTestBase;

@RunWith(BlockJUnit4ClassRunner.class)
public class TestLifeCycle extends UnitTestBase{

    public TestLifeCycle() {
        super("classpath*:spring-beanLifeCycle.xml");
        // TODO Auto-generated constructor stub
    }

    @Test
    public void testLifeCycle(){
         super.getbean("beanLifeCycle");
    }

}

2.4  三种初始化和销毁方法同时执行(注意destroy方法):猜猜会输出什么?

实现类:

package com.imooc.lifecycle;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

public class BeanLifeCycle implements InitializingBean,DisposableBean{

    /**
     * 全局初始化销毁方法
     */
    //init方法名和xml中的配置相关
    private void init() {
        // TODO Auto-generated method stub
        System.out.println("全局初始化方法!");
    }

    //destroy方法名和xml中的配置相关
    private void destroy2() {
        // TODO Auto-generated method stub
        System.out.println("全局销毁方法!");
    }

    /**
     * 实现接口,覆盖bean 的 初始化和销毁方法
     */
    @Override
    public void afterPropertiesSet() throws Exception {
        // TODO Auto-generated method stub
        System.out.println("实现接口,这里完成bean的初始化方法");
    }

    @Override
    public void destroy() throws Exception {
        // TODO Auto-generated method stub
        System.out.println("实现接口,这里完成bean的销毁方法");

    }

    //单独bean方法初始化
    public void start(){
        System.out.println("单独bean,init方法执行");
    }

    public void stop(){
        System.out.println("单独bean,destroy方法执行");
    }

}

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"
default-init-method="init" default-destroy-method="destroy2">

<!-- init-method="start" destroy-method="stop" -->
<bean id="beanLifeCycle" class="com.imooc.lifecycle.BeanLifeCycle" init-method="start" destroy-method="stop"></bean> 

</beans>

测试类:

package com.imooc.lifecycle;

import org.junit.Test;
import org.junit.internal.runners.JUnit4ClassRunner;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.springframework.cglib.core.Block;

import com.imooc.test.base.UnitTestBase;

@RunWith(BlockJUnit4ClassRunner.class)
public class TestLifeCycle extends UnitTestBase{

    public TestLifeCycle() {
        super("classpath*:spring-beanLifeCycle.xml");
        // TODO Auto-generated constructor stub
    }

    @Test
    public void testLifeCycle(){
         super.getbean("beanLifeCycle");
    }

}

测试结果:

二月 24, 2019 8:42:21 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@2e9a6f43: startup date [Sun Feb 24 08:42:21 CST 2019]; root of context hierarchy
二月 24, 2019 8:42:21 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from URL [file:/F:/xiangmu3/Xin/FuQiang/Spring/ddwei-dao/target/classes/spring-beanLifeCycle.xml]
实现接口,这里完成bean的初始化方法
单独bean,init方法执行
二月 24, 2019 8:42:22 上午 org.springframework.context.support.ClassPathXmlApplicationContext doClose
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@2e9a6f43: startup date [Sun Feb 24 08:42:21 CST 2019]; root of context hierarchy
实现接口,这里完成bean的销毁方法
单独bean,destroy方法执行

3    总结

bean有默认的初始化销毁方法a,实现接口初始化销毁方法b,配置bean的初始化销毁方法c。

3.1  如果b和c没有声明实现,那么会默认执行a

3.2  如果有bc任何一种方法,不管是否有默认方法a,都不会执行方法a

3.3  如果至少有bc两种方法,那么会优先执行方法b。

原文地址:https://www.cnblogs.com/1446358788-qq/p/10425291.html

时间: 2024-08-26 08:53:50

Spring课程 Spring入门篇 3-2 Spring bean装配(上)之bean的生命周期的相关文章

Spring中bean用法(2):生命周期

一:基本流程 把一个bean纳入到Spring IoC容器之中,这个bean的生命周期就会交由容器进行管理 1.Bean的建立 由BeanFactory读取Bean定义文件,并生成各个实例. 2.Setter注入 执行Bean的属性依赖注入. 3.BeanNameAware的setBeanName() 如果Bean类实现了org.springframework.beans.factory.BeanNameAware接口,则执行其setBeanName()方法. 4.BeanFactoryAwar

SVG矢量图形课程基础入门篇

SVG矢量图形基础入门(一) SVG完整名称是Scalable Vector Graphics,基于XML的互联网图形标准,本课程将介绍SVG的基本相关使用 课程链接:http://www.gbtags.com/gb/gbliblist/66.htm SVG矢量图形基础入门(二) SVG完整名称是Scalable Vector Graphics,基于XML的互联网图形标准,本课程将介绍SVG的基本相关使用 课程链接:http://www.gbtags.com/gb/gbliblist/76.ht

构建微服务:Spring boot 入门篇

构建微服务:Spring boot 入门篇 什么是spring boot Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置.用我的话来理解,就是spring boot其实不是什么新的框架,它默认配置了很多框架的使用方式,就像maven整合了所有的jar包,spring boot整合了所有的框架(不知道这样比喻是否合适). 使用spring boot有什

Spring容器中Bean的生命周期

日出日落,春去秋来,花随流水,北雁南飞,世间万物皆有生死轮回.从调用XML中的Bean配置信息,到应用到具体实例中,再到销毁,Bean也有属于它的生命周期. 人类大脑对图像的认知能力永远高于文字,因此,闲言少叙,书归正传,上图先: 步骤很多,切莫惊慌,我们可以把上面的步骤归纳如下: 1-2:创建实例: 现在假设spring就是个容器,而配置文件中配置的bean属性才是我们真正需要的东西.创建实例就是说,我把配置文件中的bean信息取出来化作一个真正的bean并放到容器中. 3-4:注入依赖关系:

Spring核心技术之IOC容器(一):IOC容器与Bean简介

最近开始研究Spring框架,今天学习Spring的核心内容IOC 与 Bean 1. Spring IOC 与 Bean 简介  Inversion of Control (IoC)即控制反转,也叫dependency injection (DI)依赖注入,Spring实现了一个基于配置文件的复杂工厂模式来提供实现控制反转. org.springframework.beans 和org.springframework.context包是Spring中实现IOC的基础包,其中BeanFactor

[Spring实战系列](15)使用Spring基于Java的配置

并不是所有的开发人员都喜欢使用XML,所以Spring3.0 为这些人准备了一些特别的东西.可以几乎不使用XML而使用纯粹的Java代码来配置Spring应用.并且基于Java的配置拥有一些XML配置所不具有的技巧. 1. 创建基于Java的配置 即使Spring的Java配置可以让我们不使用XML就可以编写大多数的Spring配置,但是我们仍然需要极少量的XML来启用Java配置. <?xml version="1.0" encoding="UTF-8"?&

Spring之Bean的作用域与生命周期

在前面博客中提到容器启动获得BeanDefinition对象中有一个scope 属性.该属性控制着bean对象的作用域.本章节介绍Bean的作用域及生命周期,了解bean是怎么来的又怎么没的. 一.Bean的作用域在Bean容器启动会读取bean的xml配置文件,然后将xml中每个bean元素分别转换成BeanDefinition对象.在BeanDefinition对象中有scope 属性,就是它控制着bean的作用域.Spring框架支持5种作用域,有三种作用域是当开发者使用基于web的App

【SSRS】入门篇(五) -- 设置报表格式

原文:[SSRS]入门篇(五) -- 设置报表格式 在上一节 [SSRS]入门篇(四) -- 向报表添加数据 我们设置好了报表,并可以预览到数据,如下图: 当报表完成后,有个重要的工作就是美化报表格式,这一节将讲解: 设置日期格式 设置货币格式 更改文本样式和列宽 设置日期格式:默认情况下,Data字段显示日期和时间信息:这边我们格式设置为中文日期. 1.单击"设计"选项页: 2.右键单击字段[Date],在弹出的菜单单击"文本框属性": 3.单击"数字&

微信小程序入门五: wxml文件引用、模版、生命周期

实例内容 wxml文件引用(include.import) 模版 小程序生命周期 实例一: include方式引用header.wxml文件 文件引用对于代码的重用非常重要,例如在web开发中我们可以将公用的header部分和footer等部分进行提取,然后在需要的地方进行引用. 微信小程序里面,是包含引用功能的--include.import.这两个引用文件的标签,使用基本差不多,这里先说一下include. 微信中的视图文件引用,引用过来的都是没有渲染的,基本类似于直接将引用过来的文件复制到

项目构建之maven篇:6.生命周期与插件

项目生命周期 清理 初始化 编译 测试 打包 部署 三套生命周期 1.clean pre-clean 执行一些需要在clean之前完成的工作 clean 移除所有上一次构建生成的文件 post-clean 执行一些需要在clean之后立刻完成的工作 2.compile validate generate-sources process-sources generate-resources process-resources 复制并处理资源文件,至目标目录,准备打包. compile 编译项目的源