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的生命周期详解的更多相关文章

  1. Spring Bean的生命周期详解(转)

    Spring作为当前Java最流行.最强大的轻量级框架,受到了程序员的热烈欢迎.准确的了解Spring Bean的生命周期是非常必要的.我们通常使用ApplicationContext作为Spring ...

  2. Spring之Bean的生命周期详解

      通过前面多个接口的介绍了解了Bean对象生命周期相关的方法,本文就将这些接口的方法串起来,来了解Bean的完整的生命周期.而介绍Bean的生命周期也是面试过程中经常会碰到的一个问题,如果不注意就跳 ...

  3. Spring Bean的生命周期相关博客

    最近得面试题一直 问 Spring 得生命周期,鉴于自己还未阅读过源码 所以只能是自己 背一波了.属实不懂硬背得作用,但是无奈被各位面试官打败了.等以后有时间了 一定要阅读几遍spring的 源码 有 ...

  4. Spring Bean的生命周期(非常详细)

    Spring作为当前Java最流行.最强大的轻量级框架,受到了程序员的热烈欢迎.准确的了解Spring Bean的生命周期是非常必要的.我们通常使用ApplicationContext作为Spring ...

  5. spring bean的生命周期

    掌握好spring bean的生命周期,对spring的扩展大有帮助.  spring bean的生命周期(推荐看)  spring bean的生命周期

  6. ASP.NT运行原理和页面生命周期详解及其应用

    ASP.NT运行原理和页面生命周期详解及其应用 1. 下面是我画的一张关于asp.net运行原理和页面生命周期的一张详解图.如果你对具体不太了解,请参照博客园其他帖子.在这里我主要讲解它的实际应用.  ...

  7. ASP.NET生命周期详解

    最近一直在学习ASP.NET MVC的生命周期,发现ASP.NET MVC是建立在ASP.NET Framework基础之上的,所以原来对于ASP.NET WebForm中的很多处理流程,如管道事件等 ...

  8. ASP.NET生命周期详解 [转]

    最近一直在学习ASP.NET MVC的生命周期,发现ASP.NET MVC是建立在ASP.NET Framework基础之上的,所以原来对于ASP.NET WebForm中的很多处理流程,如管道事件等 ...

  9. Spring Bean的生命周期,《Spring 实战》书中的官方说法

    连着两天的面试 ,都问到了 Spring 的Bean的生命周期,其中还包括 昨晚一波阿里的电话面试.这里找到了Spring 实战中的官方说法.希望各位要面试的小伙伴记住,以后有可能,或者是有时间 去看 ...

随机推荐

  1. java 数据类型间的转换

    byte a = (byte)129; 129已经超过了byte数据类型的存储上限,所以需要在值的前面加括号需要转换的数据类型名. 但是从高往低转的时候数值精度会有丢失; 所以最后结果为 a = -1 ...

  2. 如何更改Linux的ssh端口

    1. 修改/etc/ssh/sshd_config里的Port字段 Port 22改为Port 1000(你自定义的端口) 2. 重启sshd服务 #service sshd restart

  3. 使用Flink的SavePoint功能

    Flink通过SavePoint功能可以做到程序升级后,继续从升级前的那个点开始执行计算,保证数据不中断. Flink中CheckPoint用于保存状态,是自动执行的,SavePoint是指向Chec ...

  4. SQLite学习手册(实例代码<一>)

    一.获取表的Schema信息:       1). 动态创建表.     2). 根据sqlite3提供的API,获取表字段的信息,如字段数量以及每个字段的类型.     3). 删除该表.     ...

  5. cobol

    过程部的语句一般从B区开始书写. ACCEPT A,B (x)       DISPLAY T1,T2.(O)显示在一行上 DISPLAY  T1 DISPLAY  T2 (O)显示在两行上 read ...

  6. Xcode 单元测试

    项目创建后自动创建的单元测试文件夹, 文件夹名为项目名+tests, xcode6后貌似没有选择是否添加测试target的选择了. 默认生成的如下图 提供的断言如下 typedefNS_ENUM(NS ...

  7. java二维码生成技术

    自从微信扫描出世,二维码扫描逐渐已经成为一种主流的信息传递和交换方式.下面就介绍一下我学习到的这种二维码生成方式.预计再过不久身份证户口本等都会使用二维码识别了,今天就做一个实验案例: 二维码主要实现 ...

  8. PUTTY无法远程连接服务器故障解决[转]

    对于一个刚刚了解putty工具的新手来说,在putty工具使用中有时出现了问题而无法解决.今天就来介绍怎么解决putty无法远程连接服务器的故障. 用putty远程连接服务器时,提示错误 server ...

  9. 搭建Hadoop集群(centos6.7+hadoop-2.7.3)

    hadoop集群有三种运行模式:单机模式.伪分布模式.完全分布模式.我们这里搭建第三种完全分布模式,即使用分布式系统,在多个节点上运行. 1 环境准备 1.1 配置DNS 进入配置文件,添加主节点和从 ...

  10. JS题目

    1.请你谈谈Cookie的弊端 cookie虽然在持久保存客户端数据提供了方便,分担了服务器存储的负担,但还是有很多局限性的. 第一:每个特定的域名下最多生成20个cookie 1.IE6或更低版本最 ...