课程链接:

本节主要讲了三大块内容

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. Shell-2-命令之乐

    1.cat (1)基本用法 [root@cai tmp]# cat 1.txt 2.txt this is a test1 this is a test 2 (2)cat -s file(删除额外空白 ...

  2. 2019.2.14 考试T3 交互题

    \(\color{#0066ff}{ 题目描述 }\) 由于机房被成功拯救了,花_Q很高兴,花_Q生成了一个 0 到 N - 1 的排列(排列的下标从 0 到 N - 1 ).保证排列中 0 在 N ...

  3. 模板【洛谷P3390】 【模板】矩阵快速幂

    P3390 [模板]矩阵快速幂 题目描述 给定n*n的矩阵A,求A^k 矩阵A的大小为n×m,B的大小为n×k,设C=A×B 则\(C_{i,j}=\sum\limits_{k=1}^{n}A_{i, ...

  4. 缩点+spfa最长路【bzoj】 1179: [Apio2009]Atm

    [bzoj] 1179: [Apio2009]Atm Description Siruseri 城中的道路都是单向的.不同的道路由路口连接.按照法律的规定, 在每个路口都设立了一个 Siruseri ...

  5. linux 虚拟环境问题

    1.python环境 python2和python3命令用来区分python版本 pip2和pip3命令用来区分pip,你的包到底安装在哪里pip3 install xxx sudo apt inst ...

  6. C#工具类之字符串扩展类

    /// <summary> /// 字典串帮忙类 /// </summary> public static class StringHelper { /// <summa ...

  7. 测试用例逐步演进-xmind2excel(Python版)测试用例逐步演进-xmind2excel(Python版)

    最近,我在做项目的时候,经常被问到一个问题:如何做测试评审会更有效呢? 只要做过测试用例评审,特别是比较复杂的测试用例评审的时候,很多测试同学都会苦恼于如何能更有效的向大家说出自己的测试设计思路. 当 ...

  8. Laravel5.1接收json数据

    <?php namespace App\Http\Controllers;use Illuminate\Routing\Controller as BaseController; use Ill ...

  9. Hive 报错信息及解决方法

    return code 2 为SQL报错. return code 1 一般为权限问题. 具体要看源码.

  10. mysql隔离级别与锁,接口并发响应速度的关系(1)

    默认隔离级别:可重复读 原始数据 | id | name | addr | | nick | NULL | 事务1 事务2 start transaction start transaction ; ...