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. ...
随机推荐
- Android源码编译jar包BUILD_JAVA_LIBRARY 与BUILD_STATIC_JAVA_LIBRARY的区别(一)
一般情况下,在Android源码下编译一个jar包的典型makefile(Android.mk)如下: 在文件中加入以下内容: LOCAL_PATH:= $(call my-dir)#make jar ...
- 我推荐的一些iOS开发书单
文/叶孤城___(简书作者)原文链接:http://www.jianshu.com/p/2fa080673842著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”. 上次发了一下比较不错的i ...
- 微信小程序之----audio音频播放
audio audio为音频组件,我们可以轻松的在小程序中播放音频. audio组件属性如下: 属性名 类型 默认值 说明 id String video 组件的唯一标识符, src String ...
- js盒子模型
1.js盒子模型 指的是通过js中提供的一系列的属性和方法,获取页面中元素的样式信息值 例: #box有很多自己的私有属性: HTMLDivElement.prototype->HTMLElem ...
- listview 去掉header和footer中的分割线
在listView中加上android:headerDividersEnabled="false" android:footerDividersEnabled="fals ...
- eclipse安装maketplace插件
对于默认eclipse是没有marketplace插件的,但是marketplace确实是非常好用的,可以在上面下载各种插件. 1.Help-->Install new Software 2.输 ...
- PHP读取Excel文件(PHPExcel)
/* * 读取Excel文件 * */ require_once (dirname(dirname(dirname(__FILE__))).'/PHPExcel/PHPExcel ...
- centos 6.5下编译安装、配置高性能服务器Nginx
1.nginx是什么? Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,由俄罗斯的程序设计师Igor Sysoev所开发,其特点是占有内存少,并发能力 ...
- CSS文字大小单位PX、EM、PT
老是被人问到px.pt和em的区别,自己有时候也会纠结到底该用什么单位,今天特意查了一些文章,下面这篇虽然很久远了,但解释的比较全面,转载收藏之.点击查看原文 这里引用的是Jorux的"95 ...
- JMeter使用代理录制脚本
用JMeter做接口测试,有时候接口数量比较多,用录制方式可以节省很多时间,录制接口可以配合badboy使用,但是badboy访问的浏览器是基于ie6的,基本上现在的网站不会对ie6做兼容了,使用代理 ...