课程链接:

本节主要讲了三大块内容

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. 从零开始安装 Ambari (3) -- 安装 Ambari

    1. 安装 yum -y install ambari-server 2. ambari server 需要一个数据库存储元数据,默认使用的 Postgres 数据库.默认的用户名和密码是: amba ...

  2. Java开发环境配置(JDK+Tomcat+MyEclipsed)

    前言 这个项目一开始,我只能说我把自己整的很无语,所以我只能在调整心态的基础上,重新把思路缕了一遍,好了,接下来就说java运行环境以及发布运行方法还有SSH环境配置. 内容 本次配置用到的安装包: ...

  3. poj2947(高斯消元解同模方程组)

    题目链接:http://poj.org/problem?id=2947 题意:有n 种装饰物,m 个已知条件,每个已知条件的描述如下: p start enda1, a2......ap (1< ...

  4. 平衡树学习笔记(2)-------Treap

    Treap 上一篇:平衡树学习笔记(1)-------简介 Treap是一个玄学的平衡树 为什么说它玄学呢? 还记得上一节说过每个平衡树都有自己的平衡方式吗? 没错,它平衡的方式是......rand ...

  5. linux文件系统相关概念

    struct task_struct { ......................... struct mm_struct*mm;//内存描述符的指针 struct files_struct *f ...

  6. C3算法之我见

    C3算法说到底就是merge算法,看了一些帖子,总结说得莫名其妙,大家也是抄来抄去,我试着用自己的话来把这个东西怎么操作的说清楚.当然了我也要抄一些别人的,但是我会 尽量把我认为别人没有讲清楚的那一部 ...

  7. fsockopen函数被禁用的解决方法

    判断fsockopen 是否可用:function_exists('fsockopen');如果没有开启 一.开启fsockopen函数 修改php.ini,将 disable_functions = ...

  8. day23 模块

    1. 模块 1. 首先,我们先看个老生常谈的问题. 什么是模块. 模块就是一个包含了python定义和声 明的文件, 文件名就是模块的名字加上.py后缀. 换句话说我们目前写的所有的py文件都可以 看 ...

  9. git 合并某个提交commit到指定的分支上

    https://blog.csdn.net/anhenzhufeng/article/details/77962943 git checkout master git cherry-pick 62ec ...

  10. python3 reversed() 函数笔记

    需要逆向循环序列的话,先正向定位序列,然后调用 reversed() 函数. for i in reversed(range(1, 10, 2)):        print(i) 97531