Bean的生命周期:

Bean的定义——Bean的初始化——Bean的使用——Bean的销毁
Bean的定义
Bean 是 spring 装配的组件模型,一切实体类都可以配置成一个 Bean ,进而就可以在任何其他的 Bean 中使用,一个 Bean 也可以不是指定的实体类,这就是抽象 Bean 。
Bean的初始化
Spring中bean的初始化回调有两种方法
一种是在配置文件中声明init-method="init",然后在一个实体类中用init()方法来初始化
另一种是实现InitializingBean接口,覆盖afterPropertiesSet()方法。
第一种:
配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="init-one" class="org.spring.test.BeanInitDemo1" init-method="init">
<property name="message" value="这里是配置文件中为message赋值"></property>
</bean>
</beans>
BeanInitDemo1类:
package org.spring.test;
public class BeanInitDemo1 {
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public void init(){
this.setMessage("这里是init()方法初始化设值");
}
}
测试类:
package org.spring.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
BeanInitDemo1 bid = (BeanInitDemo1) context.getBean("init-one");
System.out.println(bid.getMessage());
}
}
运行结果:
这里是init()方法初始化设值
原因:init()初始化方法的调用是在配置文件的Bean初始化之后执行的, 所以改变了配置文件中对message的赋值。
第二种:
配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="init-two" class="org.spring.test.BeanInitDemo2">
<property name="message" value="这里是配置文件中为message赋值"></property>
</bean>
</beans>
编写BeanInitDemo2类,使其实现InitializingBean接口
package org.spring.test;
import org.springframework.beans.factory.InitializingBean;
public class BeanInitDemo2 implements InitializingBean{
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public void afterPropertiesSet() throws Exception {
// TODO Auto-generated method stub
this.setMessage("这里覆盖了InitializingBean接口的afterPropertiesSet()方法设值");
}
}
测试:
package org.spring.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
BeanInitDemo2 bid = (BeanInitDemo2) context.getBean("init-two");
System.out.println(bid.getMessage());
}
}
运行结果: 这里覆盖了InitializingBean接口的afterPropertiesSet()方法设值
原因相同,afterPropertiesSet()方法在配置文件的Bean初始化后执行,所以改变了配置文件中对message的赋值
Bean的使用
Spring中有两种使用bean的方法:
1, BeanFactory:
BeanFactory factory= new XmlBeanFactory(new ClassPathResource("bean.xml"));
factory.getBean("student");
BeanFactory是延迟加载,如果Bean的某一个属性没有注入,BeanFacotry加载后,直至第一次使用getBean方法才会抛出异常,也就是说当使用BeanFactory实例化对象时,配置的bean不会马上被实例化。当你使用该bean时才会被实例化(getBean)。
2, ApplicationContext:
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
如果使用ApplicationContext,则配置的bean如果是singleton不管你用还是不用,都被实例化。ApplicationContext在初始化自身时检验,这样有利于检查所依赖属性是否注入。ApplicationContext是BeanFactory的子类,除了具有BeanFactory的所有功能外还提供了更完整的框架功能,例如国际化,资源访问等。所以通常情况下我们选择使用ApplicationContext。
Bean的销毁
Bean的销毁和初始化一样,都是提供了两个方法
一是在配置文件中声明destroy-method="cleanup",然后在类中写一个cleanup()方法销毁
二是实现DisposableBean接口,覆盖destory()方法
第一种:
配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="destory-one" class="org.spring.test.BeanDestoryDemo1" destroy-method="cleanup">
<property name="message" value="这里是配置文件中为message赋值"></property>
</bean>
</beans>
BeanDestoryDemo1类:
package org.spring.test;
public class BeanDestoryDemo1 {
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public void cleanup(){
System.out.println("销毁之前可以调用一些方法");
}
}
测试:
package org.spring.test;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class DestortTest {
public static void main(String[] args) {
AbstractApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
BeanDestoryDemo1 bdd = (BeanDestoryDemo1) context.getBean("destory-one");
System.out.println(bdd.getMessage());
context.registerShutdownHook();
}
}
运行结果:
context.registerShutdownHook()是为spring注册关闭吊钩,程序退出之前关闭spring容器,如果没有
context.registerShutdownHook();将不会执行cleanup()方法。
第二种:
配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="destory-two" class="org.spring.test.BeanDestoryDemo2">
<property name="message" value="这里是配置文件中为message赋值"></property>
</bean>
</beans>
BeanDestoryDemo2类:
package org.spring.test;
import org.springframework.beans.factory.DisposableBean;
public class BeanDestoryDemo2 implements DisposableBean{
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public void destroy() throws Exception {
// TODO Auto-generated method stub
System.out.println("同样,销毁之前调用的方法");
}
}
测试:
package org.spring.test;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class DestortTest {
public static void main(String[] args) {
AbstractApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
BeanDestoryDemo2 bdd = (BeanDestoryDemo2) context.getBean("destory-two");
System.out.println(bdd.getMessage());
context.registerShutdownHook();
}
}
运行结果:
Spring可以管理singleton作用域的Bean的生命周期,所以在Bean初始化及销毁之前可以做一些工作,更灵活的管理Bean。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
# spring
# bean生命周期
# springbean的生命周期
# bean
# 周期
# Spring Bean生命周期源码原理图解
# Spring与bean有关的生命周期示例详解
# Spring bean生命周期配置过程解析
# 深入了解Spring中Bean的作用域和生命周期
# spring中bean的生命周期详解
# 谈谈我对Spring Bean 生命周期的理解
# 浅谈Spring中Bean的作用域、生命周期
# 浅谈Spring bean 生命周期验证
# 详解Spring中bean生命周期回调方法
# 深入理解Spring中bean的生命周期介绍
# 详解Spring中Bean的生命周期和作用域及实现方式
# 详解Spring 中 Bean 的生命周期
# 配置文件
# 是在
# 中为
# 中对
# 第二种
# 第一种
# 都是
# 加载
# 改变了
# 子类
# 这就是
# 才会
# 两种
# 当你
# 中有
# 如果没有
# 使其
# 实体类
# 有两种
# 你用
相关文章:
如何在IIS7上新建站点并设置安全权限?
建站主机服务器选购指南:轻量应用与VPS配置解析
建站主机选哪家性价比最高?
天津个人网站制作公司,天津网约车驾驶员从业资格证官网?
如何快速查询域名建站关键信息?
建站之星五站合一营销型网站搭建攻略,流量入口全覆盖优化指南
建站主机空间推荐 高性价比配置与快速部署方案解析
在线ppt制作网站有哪些,请推荐几个好的课件下载的网站?
大同网页,大同瑞慈医院官网?
免费制作小说封面的网站有哪些,怎么接网站批量的封面单?
如何选择香港主机高效搭建外贸独立站?
购物网站制作费用多少,开办网上购物网站,需要办理哪些手续?
建站主机功能解析:服务器选择与快速搭建指南
湖南网站制作公司,湖南上善若水科技有限公司做什么的?
枣阳网站制作,阳新火车站打的到仙岛湖多少钱?
制作无缝贴图网站有哪些,3dmax无缝贴图怎么调?
表情包在线制作网站免费,表情包怎么弄?
如何在IIS中新建站点并解决端口绑定冲突?
网站网页制作电话怎么打,怎样安装和使用钉钉软件免费打电话?
如何通过VPS建站实现广告与增值服务盈利?
外贸公司网站制作,外贸网站建设一般有哪些步骤?
如何用PHP快速搭建高效网站?分步指南
建站之星微信建站一键生成小程序+多端营销系统
测试制作网站有哪些,测试性取向的权威测试或者网站?
如何通过VPS搭建网站快速盈利?
海南网站制作公司有哪些,海口网是哪家的?
如何在阿里云部署织梦网站?
商务网站制作工程师,从哪几个方面把握电子商务网站主页和页面的特色设计?
如何实现建站之星域名转发设置?
c++怎么使用类型萃取type_traits_c++ 模板元编程类型判断【方法】
,石家庄四十八中学官网?
如何快速搭建响应式可视化网站?
如何制作算命网站,怎么注册算命网站?
整人网站在线制作软件,整蛊网站退不出去必须要打我是白痴才能出去?
如何在万网主机上快速搭建网站?
小自动建站系统:AI智能生成+拖拽模板,多端适配一键搭建
Python路径拼接规范_跨平台处理说明【指导】
建站主机如何选?性能与价格怎样平衡?
建站之星云端配置指南:模板选择与SEO优化一键生成
如何确保FTP站点访问权限与数据传输安全?
制作网站的过程怎么写,用凡科建站如何制作自己的网站?
如何用PHP快速搭建CMS系统?
如何选择最佳自助建站系统?快速指南解析优劣
免费网站制作模板下载,除了易企秀之外还有什么H5平台可以制作H5长页面,最好是免费的?
西安大型网站制作公司,西安招聘网站最好的是哪个?
简历在线制作网站免费版,如何创建个人简历?
如何挑选最适合建站的高性能VPS主机?
公司网站建设制作费用,想建设一个属于自己的企业网站,该如何去做?
洛阳网站制作公司有哪些,洛阳的招聘网站都有哪些?
招贴海报怎么做,什么是海报招贴?
*请认真填写需求信息,我们会在24小时内与您取得联系。