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

最新文章

  1. [UE4]AnimDynamics简介
  2. 《Spring 3.0就这么简单》 读书笔记
  3. 你知道QQ有多少人同时在线么
  4. C#----对时间结构DateTime的使用(时间日期的使用)
  5. WPF中的画图
  6. Nginx 笔记与总结(14)expires 缓存设置
  7. js控制手机号码中间用星号代替
  8. spark读hdfs文件实现wordcount并将结果存回hdfs
  9. 浅谈inline-block
  10. BZOJ 1063 道路设计NOI2008
  11. Value &#39;0000-00-00&#39; can not be represented as java.sql.Date
  12. Golang分布式爬虫:抓取煎蛋文章|Redis/Mysql|56,961 篇文章
  13. vue-cli 创建项目失败
  14. 2019 面试准备 - JS 防抖与节流 (超级 重要!!!!!)
  15. SSH 免密码登陆到多台机器
  16. HDU4341-Gold miner-分组DP
  17. huffman编解码英文文本[Python]
  18. asp.net的get和post请求
  19. POJ-2336 Ferry Loading II(简单DP)
  20. DirectShow 制作在Unity3D中可以设置进度的视频播放插件

热门文章

  1. Centos6.7安装mysql 5.6简单教程
  2. 将js进行到底:node学习笔记2
  3. sql子查询
  4. Thrift之TProtocol系列TBinaryProtocol解析
  5. HotSpot 虚拟机的算法实现
  6. java里程碑之泛型--类型通配符
  7. Linux Shell 文件描述符 及 stdin stdout stderr 重定向
  8. php之插入排序
  9. maven将本地jar包导入本地仓库
  10. ferror,clearerr和EOF含义