Spring BeanFactory实例化Bean的详细过程
/**
* 实体类
*/
public class Employee implements InitializingBean, DisposableBean, BeanNameAware {
private String id;// 员工编号
private String name;// 员工姓名
private String sex;// 员工性别
private String age;// 员工年龄
private String nativePlace;// 员工籍贯
private String department;// 员工部门
private String beanName;// bean的名称 public Employee() {
System.out.println("**********第一步:调用Bean的默认构造方法**********");
this.id = "bean1:G080405214";
System.out.println("bean1的 值:" + this);
System.out.println("**********第二步:检查Bean配置文件中是否注入了Bean的属性值**********");
} public void afterPropertiesSet() throws Exception {
System.out.println("bean2的值:" + this);
System.out.println("**********第三步:检查Bean是否实现了InitializingBean接口**********");
this.name = "bean3:李晓红";
this.sex = "bean3:女";
this.age = "bean3:25";
System.out.println("bean3的值:" + this);
} public void init() {
System.out
.println("**********第四步:检查Bean配置文件中是否指定了init-method此属性**********");
this.nativePlace = "bean3:北京";
System.out.println("bean4的值:" + this);
} public void destroy() throws Exception {
System.out.println("**********服务停止**********");
} public void setBeanName(String arg0) {
System.out.println("**********设置bean的名称**********");
this.beanName = "myBeanName"; } public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
} public String getAge() {
return age;
} public void setAge(String age) {
this.age = age;
} public String getNativePlace() {
return nativePlace;
} public void setNativePlace(String nativePlace) {
this.nativePlace = nativePlace;
} public String getDepartment() {
return department;
} public void setDepartment(String department) {
this.department = department;
} public String getBeanName() {
return beanName;
} @Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + ", sex=" + sex
+ ", age=" + age + ", nativePlace=" + nativePlace
+ ", department=" + department + ", beanName=" + beanName + "]";
}
}
工具类:
/**
* Bean上下文工具类
*/
public class BeanContextHelper {
private static ApplicationContext _instance; static {
if (_instance == null)
_instance = buildApplicationContext();
} private BeanContextHelper() {
} /**
* 重新构建ApplicationContext对象
*/
public static ApplicationContext buildApplicationContext() {
return new ClassPathXmlApplicationContext("applicationContext-base.xml");
} /**
* 获取一个ApplicationContext对象
*/
public static ApplicationContext getApplicationContext() {
return _instance;
}
}
Spring的Bean配置:
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!--==============测试Spring BeanFactory实例化Bean的过程-->
<bean id="employee" class="bean_factory_test.Employee"
init-method="init" destroy-method="destroy">
<!--默认部门为研发部的-->
<property name="department">
<value>bean2:研发部</value>
</property>
</bean> </beans>
测试类:
/**
* BeanFactory实例化Bean工程测试类
*/
public class Test {
public static void main(String args[]) {
Test test = new Test();
test.test();
} public void test() {
ApplicationContext context = BeanContextHelper.getApplicationContext();
Employee employee = (Employee) context.getBean("employee");
System.out.println("**********从Spring BeanFactory获取到的最终bean实例**********");
System.out.println("最终bean的值:" + employee);
}
}
运行结果:
**********第一步:调用Bean的默认构造方法**********
bean1的 值:Employee [id=bean1:G080405214, name=null, sex=null, age=null, nativePlace=null, department=null, beanName=null]
**********第二步:检查Bean配置文件中是否注入了Bean的属性值**********
**********设置bean的名称**********
bean2的值:Employee [id=bean1:G080405214, name=null, sex=null, age=null, nativePlace=null, department=bean2:研发部, beanName=myBeanName]
**********第三步:检查Bean是否实现了InitializingBean接口**********
bean3的值:Employee [id=bean1:G080405214, name=bean3:李晓红, sex=bean3:女, age=bean3:25, nativePlace=null, department=bean2:研发部, beanName=myBeanName]
**********第四步:检查Bean配置文件中是否指定了init-method此属性**********
bean4的值:Employee [id=bean1:G080405214, name=bean3:李晓红, sex=bean3:女, age=bean3:25, nativePlace=bean3:北京, department=bean2:研发部, beanName=myBeanName]
**********从Spring BeanFactory获取到的最终bean实例**********
最终bean的值:Employee [id=bean1:G080405214, name=bean3:李晓红, sex=bean3:女, age=bean3:25, nativePlace=bean3:北京, department=bean2:研发部, beanName=myBeanName]
从运行结果看,我们应该很清楚Bean实例化的具体过程了。
Spring BeanFactory实例化Bean的详细过程的更多相关文章
- mybatis学习笔记(五) -- maven+spring+mybatis从零开始搭建整合详细过程(附demo和搭建过程遇到的问题解决方法)
文章介绍结构一览 一.使用maven创建web项目 1.新建maven项目 2.修改jre版本 3.修改Project Facts,生成WebContent文件夾 4.将WebContent下的两个文 ...
- mybatis学习笔记(六) -- maven+spring+mybatis从零开始搭建整合详细过程(下)
继续 mybatis学习笔记(五) -- maven+spring+mybatis从零开始搭建整合详细过程(上) 五.使用监听器启动Spring容器 1.修改pom.xml文件,添加Spring-we ...
- Spring、实例化Bean的三种方法
1.使用类构造器进行实例化 <bean id="personIService" class="cn.server.impl.PersonServiceImpl&qu ...
- 02.基于IDEA+Spring+Maven搭建测试项目--详细过程
一.背景介绍 1.1公司相关技术 Git:是一款免费的开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目,方便多人集成开发 Maven:是基于项目对象模型(POM),可以通过一小段描述信息 ...
- spring加载Bean的解析过程(二)
1.例如: BeanFactory bf = new XmlBeanFactory(new ClassPathResource("spring.xml")); User user ...
- SSH 之 Spring的源码(一)——Bean加载过程
看看Spring的源码,看看巨人的底层实现,拓展思路,为了更好的理解原理,看看源码,深入浅出吧.本文基于Spring 4.0.8版本. 首先Web项目使用Spring是通过在web.xml里面配置 o ...
- 看看Spring的源码(一)——Bean加载过程
首先Web项目使用Spring是通过在web.xml里面配置org.springframework.web.context.ContextLoaderListener初始化IOC容器的. <li ...
- spring BeanFactory及ApplicationContext中Bean的生命周期
spring bean 的生命周期 spring BeanFactory及ApplicationContext在读取配置文件后.实例化bean前后.设置bean的属性前后这些点都可以通过实现接口添加我 ...
- Spring 源码(14)Spring Bean 的创建过程(6)对象的提前暴露
知识回顾 解析完Bean信息的合并,可以知道Spring在实例化Bean之后,属性填充前,对Bean进行了Bean的合并操作,这里的操作主要做了对Bean对象标记了@Autowired.@Value. ...
随机推荐
- Laravel5 model create使用
1.在laravel的Eloquent ORM中,默认表会有created_at.updated_at两个字段,因此在使用create函数时若表无这两个字段会出错,可以设置 public $times ...
- highcharts柱状图和饼图的数据填充
1.其实数据填充很简单,它们就是json的格式,然后后台按照这种格式去套数据发给前端:前端再做一下连接处理等就行了. $('#program_statistics_bar').highcharts({ ...
- 【 bzoj4537】HNOI2016 最小公倍数
首先将边按a的值分组,每$\sqrt{m}$一组. 对于每一组,将符合一组a的询问选出来,将这些询问和这一块之前的边(a一定小于这些询问)按b排序,然后交替插入,询问,对于一个询问,在当前块也有可能有 ...
- S3C2440硬件IIC详解
S3C2440A RISC微处理器可以支持一个多主控IIC 总线串行接口.一条专用串行数据线(SDA)和一条专用串行时钟线(SCL)传递连接到IIC总线的总线主控和外设之间的信息.SDA和SCL线都为 ...
- iOS开源库--最全的整理 分类: ios相关 2015-04-08 09:20 486人阅读 评论(0) 收藏
youtube下载神器:https://github.com/rg3/youtube-dl 我擦咧 vim插件:https://github.com/Valloric/YouCompleteMe vi ...
- 微信小程序之----底部菜单action-sheet
action-sheet action-sheet是从底部弹出的选择菜单,子菜单通过action-sheet-item和action-sheet-cancel指定,action-sheet-item是 ...
- MVC分页控件的使用
1. 引用 using Webdiyer.WebControls.Mvc; 2. using Webdiyer.WebControls.Mvc; ) { )); } 3.数据来源 public cla ...
- 写一篇 Bootstrap弹窗确认的文章。本周完成
思路; 点击按钮,显示模态 点击确定,异步提交 根据结果,删除指定的记录
- javascript-array函数实例
<script type="text/javascript"> window.onload = function () { // body... var aNew = ...
- 按住ctrl键可以在新窗口打开图片
用firebug查看网页时,img标签(或background属性里面的url地址源)里面的图片源按住ctrl键可以弹出新窗口显示,并可右键另存为到本地目录