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. ...
随机推荐
- (简单) POJ 3667 Hotel,线段树+区间合并。
Description The cows are journeying north to Thunder Bay in Canada to gain cultural enrichment and e ...
- for/foreach/linq效率测试
; Random r = new Random(); List<byte> list = new List<byte>(); Console.WriteLine("开 ...
- 安卓能用的modebus CRC16计算,附上对应的C语言的CRC16(转)
源:安卓能用的modebus CRC16计算,附上对应的C语言的CRC16 “源”即是原文地址,想了解作都更多文章及思想请移步到“源”.转过只是为了本人感兴趣的文章查找方便. 正文: 最近写安卓串口通 ...
- 企业证书APP发布流程 分类: ios相关 app相关 2015-06-10 11:01 212人阅读 评论(0) 收藏
企业发布app的 过程比app store 发布的简单多了,没那么多的要求,哈 但是整个工程的要求还是一样,比如各种像素的icon啊 命名规范啊等等. 下面是具体的流程 1.修改你的 bundle i ...
- iOS开发~CocoaPods使用详细说明 分类: ios相关 2015-04-01 16:45 68人阅读 评论(0) 收藏
iOS开发-CocoaPods使用详细说明 一.概要 iOS开发时,项目中会引用许多第三方库,CocoaPods(https://github.com/CocoaPods/CocoaPods)可以用来 ...
- spring.net的基本搭建
这几天在学C#,感觉还是需要一个控制反转的框架,正好Spirng也有.net版的,看着API搭建一个 大致目录是这样的,我们在APP.CONFIG里面配好xml文件的地址,这个APP.CONFIG就相 ...
- 设置git账号并生成新的ssh(切换电脑用户之后)
1.设置账号 2.设置邮箱 3.检查确认 4. 5.check-----成功~
- Struts2验证
一.声明式验证 1.字段验证 fielderror的两种显示方式 fielderror的提示信息可以国际化 2.非字段验证:actionErrors / <s:actionerror> 例 ...
- ios framework 开发实战 之 参考
WWDC2014之iOS使用动态库 iOS开发——创建你自己的Framework 使用CocoaPods开发并打包静态库 iOS Framework 和CocoaPods TDD的iOS开发初步以及K ...
- iOS 开发 之 编程知识点
iOS 创建和设置pch iOS 之 时间格式与字符串转换 iOS 之 二维码生成与扫描(LBXScan) iOS 之 定时器 iOS 之 通知 iOS 之 NSString 去除前后空格和回车键 i ...