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。
Spring课程 Spring入门篇 3-2 Spring bean装配(上)之bean的生命周期的更多相关文章
- Spring Boot -01- 快速入门篇(图文教程)
Spring Boot -01- 快速入门篇(图文教程) 今天开始不断整理 Spring Boot 2.0 版本学习笔记,大家可以在博客看到我的笔记,然后大家想看视频课程也可以到[慕课网]手机 app ...
- Spring实践系列-入门篇(一)
本文主要介绍了在本地搭建并运行一个Spring应用,演示了Spring依赖注入的特性 1 环境搭建 1.1 Maven依赖 目前只用到依赖注入的功能,故以下三个包已满足使用. <properti ...
- Spring Cloud Alibaba入门篇
学习条件 了解web三层架构 熟练应用SSM架构 了解Maven管理工具的使用 熟练使用SpringBoot,以及了解SpringBoot基本原理. 了解部分术语:应用.工具.耦合.负载等 温馨提示: ...
- Spring Data JPA 入门篇
Spring Data JPA是什么 它是Spring基于ORM框架(如hibernate,Mybatis等).JPA规范(Java Persistence API)封装的一套 JPA应用框架,可使开 ...
- Spring——bean的五种作用域和生命周期
一.Bean的作用域 1.当一个bean的作用域为Singleton,那么Spring IoC容器中只会存在一个共享的bean实例,并且所有对bean的请求,只要id与该bean定义相匹配,则只会返回 ...
- <JVM中篇:字节码与类的加载篇>03-类的加载过程(类的生命周期)详解
笔记来源:尚硅谷JVM全套教程,百万播放,全网巅峰(宋红康详解java虚拟机) 同步更新:https://gitee.com/vectorx/NOTE_JVM https://codechina.cs ...
- .net core番外第一篇:Autofac的几种常见注入方式、生命周期和AOP
使用Autofac进行服务注册实践: 新建三个项目,分别是webapi项目 Wesky.Core.Autofac以及两个类库项目 Wesky.Core.Interface和Wesky.Core.Ser ...
- spring boot 学习入门篇【spring boot项目的搭建以及如何加载jsp界面】
[ 前言] Spring Boot 简介:Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置, ...
- [Swoole入门到进阶] [精选公开课] Swoole服务器-Server的四层生命周期
PHP 完整生命周期 执行PHP文件 PHP扩展模块初始化(MINIT) PHP扩展请求初始化(RINIT) 执行 PHP 逻辑 PHP扩展请求结束(RSHUTDOWN) PHP脚本清理 PHP扩展模 ...
- Java-多线程第三篇3种创建的线程方式、线程的生命周期、线程控制、线程同步、线程通信
1.Java使用Thread类代表线程. 所有的线程对象必须是Thread类或其子类的实例. 当线程继承Thread类时,直接使用this即可获取当前线程,Thread对象的getName() ...
随机推荐
- 大佬写的js生成玫瑰(来源网络)
<!DOCTYPE html> <html> <head> <title>js html5渲染的3D玫瑰花(程序员的情人节礼物)</title&g ...
- Java基础笔记(十九)——抽象类abstract
抽象类作为父类,不能实例化自己类型的对象,但可以通过向上转型实例化子类对象. public abstract class Animal{ } 比如eat(); ,每个动物子类都应有自己的方法,那An ...
- 【2014年百度之星资格赛1001】Energy Conversion
Problem Description 魔法师百小度也有遇到难题的时候—— 现在,百小度正在一个古老的石门面前,石门上有一段古老的魔法文字,读懂这种魔法文字需要耗费大量的能量和大量的脑力. 过了许久, ...
- HDU5952 Counting Cliques计算完全图的个数 巧妙构图+dfs
题目传送门 题目大意:给出n个点,m条无向边,让你计算这幅母图中有几个大小为s的完全图. 完全图的意思是任意一个点都和其他点直接相连,完全图的大小指的就是完全图点的个数. 思路:比较巧妙的构图方式.我 ...
- UVALive 3645 时序模型
按航班拆点 注意返边的条件 #include<bits/stdc++.h> using namespace std; const int maxn = 1e6+11; const int ...
- day_09 函数及参数
1.定义:把功能封装起来,方便下次直接调用 2.语法:def 关键词开头,空格之后接函数名称和圆括号(). def 函数名(形参) 函数体 3.参数:圆括号用来接收参数.若传入多个参数,参数之间用逗号 ...
- JS电话、手机号码验证
function isTelephone(inpurStr) { var partten = /^0(([1,2]\d)|([3-9]\d{2}))-\d{7,8}$/; ...
- 5.SpringMVC
1.SpringMVC概述 概述: SpringMVC是基于请求驱动,围绕一个核心Servlet 转发请求到对应的Controller而设计的优点:是一个典型的教科书式的MVC构架,易学易用提供了清晰 ...
- Linux忘记roo密码的解决办法
Linux忘记root密码有三种解决办法: 下面详细介绍第一种: 重启系统后出现GRUB界面在引导装载程序菜单上,用上下方向键选择你忘记密码的那个系统键入“e” 来进入编辑模式. 接下来你可以看到 ...
- python练习六十三:文件处理,读取文件内容,按内容生成文件
python练习六十三:文件处理 假设要读取code.txt文件中内容,code.txt文件内容如下 01 CN Chinese 02 US United States of America 03 J ...