课程链接:

本节主要讲了三大块内容

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。

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

  1. Spring Boot -01- 快速入门篇(图文教程)

    Spring Boot -01- 快速入门篇(图文教程) 今天开始不断整理 Spring Boot 2.0 版本学习笔记,大家可以在博客看到我的笔记,然后大家想看视频课程也可以到[慕课网]手机 app ...

  2. Spring实践系列-入门篇(一)

    本文主要介绍了在本地搭建并运行一个Spring应用,演示了Spring依赖注入的特性 1 环境搭建 1.1 Maven依赖 目前只用到依赖注入的功能,故以下三个包已满足使用. <properti ...

  3. Spring Cloud Alibaba入门篇

    学习条件 了解web三层架构 熟练应用SSM架构 了解Maven管理工具的使用 熟练使用SpringBoot,以及了解SpringBoot基本原理. 了解部分术语:应用.工具.耦合.负载等 温馨提示: ...

  4. Spring Data JPA 入门篇

    Spring Data JPA是什么 它是Spring基于ORM框架(如hibernate,Mybatis等).JPA规范(Java Persistence API)封装的一套 JPA应用框架,可使开 ...

  5. Spring——bean的五种作用域和生命周期

    一.Bean的作用域 1.当一个bean的作用域为Singleton,那么Spring IoC容器中只会存在一个共享的bean实例,并且所有对bean的请求,只要id与该bean定义相匹配,则只会返回 ...

  6. <JVM中篇:字节码与类的加载篇>03-类的加载过程(类的生命周期)详解

    笔记来源:尚硅谷JVM全套教程,百万播放,全网巅峰(宋红康详解java虚拟机) 同步更新:https://gitee.com/vectorx/NOTE_JVM https://codechina.cs ...

  7. .net core番外第一篇:Autofac的几种常见注入方式、生命周期和AOP

    使用Autofac进行服务注册实践: 新建三个项目,分别是webapi项目 Wesky.Core.Autofac以及两个类库项目 Wesky.Core.Interface和Wesky.Core.Ser ...

  8. spring boot 学习入门篇【spring boot项目的搭建以及如何加载jsp界面】

    [ 前言]  Spring Boot 简介:Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置, ...

  9. [Swoole入门到进阶] [精选公开课] Swoole服务器-Server的四层生命周期

    PHP 完整生命周期 执行PHP文件 PHP扩展模块初始化(MINIT) PHP扩展请求初始化(RINIT) 执行 PHP 逻辑 PHP扩展请求结束(RSHUTDOWN) PHP脚本清理 PHP扩展模 ...

  10. Java-多线程第三篇3种创建的线程方式、线程的生命周期、线程控制、线程同步、线程通信

    1.Java使用Thread类代表线程.     所有的线程对象必须是Thread类或其子类的实例. 当线程继承Thread类时,直接使用this即可获取当前线程,Thread对象的getName() ...

随机推荐

  1. 在Python中正确使用Unicode

    正确处理文本,特别是正确处理Unicode.是个老生常谈的问题,有时甚至会难倒经验丰富的开发者.并不是因为这个问题很难,而是因为对软件中的文本,开发者没有正确理解一些关键概念及其表示方法.在Stack ...

  2. Linux文件属性用户、组、权限

    Linux系统中的用户是分角色的,用户的角色是由UID和GID来识别的(也就是说系统识别的是用户的UID.GID,而非用户用户名),有个UID是唯一的(系统中唯一如同身份证一样)用来标识系统的用户账号 ...

  3. 线段树【洛谷P2894】 [USACO08FEB]酒店Hotel

    P2894 [USACO08FEB]酒店Hotel 参考样例,第一行输入n,m ,n代表有n个房间,编号为1---n,开始都为空房,m表示以下有m行操作,以下 每行先输入一个数 i ,表示一种操作: ...

  4. stl仿函数和适配器

    所谓的适配器就是底层利用仿函数,然后修改仿函数的接口,达到自己的目的: 例如:template<class operation> class binder1st的适配器,其本质是一个类,它 ...

  5. shell脚本安装jdk

    #!/bin/bash BASE_SERVER=192.168.1.11 yum install -y wget wget $BASE_SERVER/soft-all/jdk-7u45-linux-x ...

  6. lambda函数/排序/filter/map

    1.lambda 匿名函数 zrf = lambda x:x**2 ret = zrf(10) #这里面实际上还是有函数名 print(ret) 2.sorted 排序(list也自带排序功能) 排序 ...

  7. JS执行顺序-函数声明提升、匿名函数、函数表达式

    大方向上: JS 是按照 代码块 进行 编译.执行 的. 学习至: 1.变量声明提升 2.新唐的博客 3.js中匿名函数的创建与调用方法分析 4.前端圣经 - <高程三> 5.深入理解变量 ...

  8. 【SSO】单点登录系统

    一.单点登录系统介绍 对于一个开发项目来说,每个项目都必不可少要有登录的这个功能.但是随着项目的变大,变大,再变大.系统可能会被拆分成多个小系统,咱们就拿支付宝和淘宝来说,咱们在淘宝上购物,然后就可以 ...

  9. I2C(smbus pmbus)和SPI分析

    2C和SPI作为两种非常常用的低速外部总线 I2C I2C是以前的飞利浦半导体制定的标准,也就是如今的NXP. I2C总线由一条数据线(SDA)和一条时钟线(SCL)组成.设备分主从,主设备提供时钟, ...

  10. 求js数组的最大值和最小值

    数组 ,,,,,,,,,]; 方法1 - 字符串拼接法 利用toString或join把数组转换为字符串,再和Math的max和min方法分别进行拼接,最后执行eval方法 var max = eva ...