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() ...
随机推荐
- Mysql 别名
一.列别名 1.要给列添加别名,可以使用AS关键词后跟别名,例:SELECT [column_1 | expression] AS descriptive_name FROM table_name; ...
- Java基础笔记(十八)——多态
多态表示同一个操作作用在不同对象时,会有不同的结果. 多态可分为编译时多态和运行时多态. 编译时多态:方法重载,编译时就可以确定到底调用哪个方法,可以被看做一个类中的方法多态性. 运行时多态:只有在运 ...
- C. The Fair Nut and String 递推分段形dp
C. The Fair Nut and String 递推分段形dp 题意 给出一个字符串选择一个序列\({p_1,p_2...p_k}\)使得 对于任意一个\(p_i\) , \(s[p_i]==a ...
- springboot整合mybatis,redis,代码(三)
一 说明 接着上篇讲述redis缓存配置的用法: 二 正文 首先要使用缓存就必须要开开启缓存,第二步是需要开redis-server 下载redis包之后,点击图中两个都可以开启redis 怎么看是否 ...
- php小试牛刀
[构造函数] function __construct() [析构函数] 当某个对象的所有引用被删除,或者对象被显式的销毁时会执行析构函数 function __destruct() [静态方法] p ...
- Jenkins的Pipeline脚本在美团餐饮SaaS中的实践(转)
一.背景 在日常开发中,我们经常会有发布需求,而且还会遇到各种环境,比如:线上环境(Online),模拟环境(Staging),开发环境(Dev)等.最简单的就是手动构建.上传服务器,但这种方式太过于 ...
- hdu 1556 涂气球 线段树(区间更新~对区间[x,y]更新,求任意节点被更新的次数)
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- linu samba服务
关闭防火墙并且重启网络yum install samba samba-client samba-commmon -ysystemctl start smb smbclient -L //172.25 ...
- PHP漏洞全解—————9、文件上传漏洞
本文主要介绍针对PHP网站文件上传漏洞.由于文件上传功能实现代码没有严格限制用户上传的文件后缀以及文件类型,导致允许攻击者向某个可通过 Web 访问的目录上传任意PHP文件,并能够将这些文件传递给 P ...
- TCP通讯模型简单示例
1. TCP通讯模型 2. 服务器端 ① 创建socket,用函数socket() ② 绑定IP地址.端口号等信息到socket上,用函数bind() ③ 设置允许的最大连接数,用函数listen() ...