Spring bean的生命周期详解
bean的生命周期
1.实例化bean 即new
2.按照spring上下文对实例化的bean进行配置 即填充属性,也就是IOC/DI(控制反转,依赖注入)
3.如果这个bean实现了BeanNameAware接口,Spring会调用它实现的setBeanName()方法,参数是bean的ID,即Spring将bean的ID传递给setBeanName()方法。(让bean知道自己是谁,即自己的ID)
4.如果bean实现了BeanFactoryAware接口,Spring将调用setBeanFactory(BeanFactory factory)方法,将BeanFactory容器实例传入;(即知道bean自己属于哪个工厂)
5.如果Bean实现了ApplicationContextAware接口,Spring将调用setApplicationContext(ApplicationContext context)方法,传入Spring上下文。
6.如果Bean实现了BeanPostProcessor接口,将会调用postProcessBeforeInialization(Object obj,String s)方法。BeanPostProcessor经常被用作是Bean内容的更改。
7.如果这个Bean在Spring配置文件中配置了init-method属性会自动调用其配置的初始化方法。
8.如果这个Bean实现了BeanPostProcessor接口,将会调用postAfterInitialization(Object obj,String s)方法
备注:当以上工作完成后就可以使用这个Bean了,这个bean是single的,一般调用同一个ID的bean会是在内容地址相同的实例
9.这个Bean会一直留在应用上下文中(ApplicationContext),直到该应用上下文被销毁。
10.如果这个Bean实现了DisposableBean接口,会调用destroy()方法;如果Bean在Spring配置中配置了destroy-method属性,会自动调用其配置的销毁方法。
在Spring框架中,bean的定义,从编写到配置再到最终的getbean调用,框架都有相应的实现规则,具体如下所述。
bean的定义:
package com.spring.beans;
import javax.ejb.Init;
import org.springframework.beans.factory.InitializingBean;
public class HelloBean implements InitializingBean {
public HelloBean() {
System.out.println("构造方法");
}
private String name;
private String nullTest;
private int age;
public String getNullTest() {
return nullTest;
}
public void setNullTest(String nullTest) {
this.nullTest = nullTest;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void prints(String str) {
System.out.println(str + ":hahahah");
}
public void init() {
System.out.println("init");
}
@Override
public void afterPropertiesSet() throws Exception {
// TODO Auto-generated method stub
System.out.println("initializing");
}
}
BeanPostProcessor定义:
package com.spring.test; import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor; public class BeanPostProcessor_Imp implements BeanPostProcessor { @Override
public Object postProcessAfterInitialization(Object arg0, String arg1)
throws BeansException {
System.out.println("执行后");
return arg0;
} @Override
public Object postProcessBeforeInitialization(Object arg0, String arg1)
throws BeansException {
System.out.println("执行前");
return arg0;
} }
测试类的定义:
package com.spring.test; import java.util.List; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource; import com.spring.beans.BigBearBean;
import com.spring.beans.HelloBean;
import com.spring.beans.List_Map_Bean;
import com.spring.beans.smallBearBean;
import com.spring.interfaces.Animal; public class HelloTest {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"hellotest.xml");
HelloBean HB = (HelloBean) ctx.getBean("hello");
HB.prints("王涛");
System.out.println(HB.getName() + "\n------------");
} }
配置文件:
<?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:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <bean id="hello" class="com.spring.beans.HelloBean" init-method="init">
<property name="name">
<value><![CDATA[<wb<t>]]></value>
</property>
<property name="age" value="23" />
<property name="nullTest">
<value></value>
</property>
</bean>
<bean class="com.spring.test.BeanPostProcessor_Imp"></bean>
</beans>
运行结果:
15:38:12,269 INFO [context.support.ClassPathXmlApplicationContext] Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@61a48515: startup date [Fri Jun 23 15:38:12 CST 2017]; root of context hierarchy
15:38:12,343 INFO [factory.xml.XmlBeanDefinitionReader] Loading XML bean definitions from class path resource [hellotest.xml]
15:38:12,588 INFO [factory.support.DefaultListableBeanFactory] Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@25ff3700: defining beans [hello,com.spring.test.BeanPostProcessor_Imp#0]; root of factory hierarchy
构造方法
执行前
initializing
init
执行后
王涛:hahahah
Spring bean的生命周期详解的更多相关文章
- Spring Bean的生命周期详解(转)
Spring作为当前Java最流行.最强大的轻量级框架,受到了程序员的热烈欢迎.准确的了解Spring Bean的生命周期是非常必要的.我们通常使用ApplicationContext作为Spring ...
- Spring之Bean的生命周期详解
通过前面多个接口的介绍了解了Bean对象生命周期相关的方法,本文就将这些接口的方法串起来,来了解Bean的完整的生命周期.而介绍Bean的生命周期也是面试过程中经常会碰到的一个问题,如果不注意就跳 ...
- Spring Bean的生命周期相关博客
最近得面试题一直 问 Spring 得生命周期,鉴于自己还未阅读过源码 所以只能是自己 背一波了.属实不懂硬背得作用,但是无奈被各位面试官打败了.等以后有时间了 一定要阅读几遍spring的 源码 有 ...
- Spring Bean的生命周期(非常详细)
Spring作为当前Java最流行.最强大的轻量级框架,受到了程序员的热烈欢迎.准确的了解Spring Bean的生命周期是非常必要的.我们通常使用ApplicationContext作为Spring ...
- spring bean的生命周期
掌握好spring bean的生命周期,对spring的扩展大有帮助. spring bean的生命周期(推荐看) spring bean的生命周期
- ASP.NT运行原理和页面生命周期详解及其应用
ASP.NT运行原理和页面生命周期详解及其应用 1. 下面是我画的一张关于asp.net运行原理和页面生命周期的一张详解图.如果你对具体不太了解,请参照博客园其他帖子.在这里我主要讲解它的实际应用. ...
- ASP.NET生命周期详解
最近一直在学习ASP.NET MVC的生命周期,发现ASP.NET MVC是建立在ASP.NET Framework基础之上的,所以原来对于ASP.NET WebForm中的很多处理流程,如管道事件等 ...
- ASP.NET生命周期详解 [转]
最近一直在学习ASP.NET MVC的生命周期,发现ASP.NET MVC是建立在ASP.NET Framework基础之上的,所以原来对于ASP.NET WebForm中的很多处理流程,如管道事件等 ...
- Spring Bean的生命周期,《Spring 实战》书中的官方说法
连着两天的面试 ,都问到了 Spring 的Bean的生命周期,其中还包括 昨晚一波阿里的电话面试.这里找到了Spring 实战中的官方说法.希望各位要面试的小伙伴记住,以后有可能,或者是有时间 去看 ...
随机推荐
- Vue2反向代理
前一段时间写了一个vue2的小项目,用的是vue-cli脚手架搭建的项目,项目里需要跨域,但又不能使用jsonp,上网查了一下,发现有一个之前没接触过的词语--反向代理. 什么是"反向代 ...
- 【转载备忘】PowerDesigner16.5基本使用
这两天都在设计数据库,使用了powerdesigner进行设计的,然后摸索了好久,本来打算写一篇文章来记述一下的,写了一半,突然发现网上早就有比我写的好的文章了,所有删了之前写的,直接贴出来那个文章的 ...
- 怎么看vue版本
查看vue版本号是 vue -V 而不是npm vue -v ,npm vue -v 等同于npm -v vue -V: 后面那个V是大写的.
- 读书笔记——《C++ Concurrency IN ACTION》
=================================版权声明================================= 版权声明:原创文章 禁止转载 请通过右侧公告中的“联系邮 ...
- 一句话 Servlet
Servlet是用来完成B/S架构下,客户端请求的响应处理. web.xml其实就是servlet的一个配置文件,通过他来寻找对应的servlet
- @RequestMapping 相关 spring
* * @param request HttpServletRequest * @param delList 削除Idエスト * @return 削除結果 * @th ...
- SpringMVC的filter怎么使用Autowired依赖注入bean
有的时候根据我们业务的需要,我们需要在web项目中定义一个自己的filter,并想在这个filter中使用@Autowired注入bean供我们使用.如果直接使用的话是不行的,需要我们在xml文件 ...
- python_改变字符串中文本格式?
案例: 某软件的日志文件,其中日期格式为year-moth-day: 2016-04-21 10:50:30 python 2014-05-22 10:50:30 python 2017-06-23 ...
- JavaScript数组的22种方法
原文:http://www.cnblogs.com/xiaohuochai/p/5682621.html javascript中数组的22种方法 前面的话 数组总共有22种方法,本文将其分为对象继 ...
- junit源码解析总结
前面的博客我们也已经整理到了,我们使用junit38,在写测试类的时候我们的测试类必须继承TestCase.这个所有测试类的父类在junit.framework包下面. 前面我们的整理都是说直接在ID ...