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. ...
随机推荐
- CSharp笔记>>>多语言,注册
C#多语言 方案1:http://blog.csdn.net/suncherrydream/article/details/43234059 http://blog.itpub.net/1263917 ...
- leetcode--009 Linked List Cycle I
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAZgAAACACAIAAAC5q+hAAAAJ+UlEQVR4nO2dwbXrKBJAOyelRShEQw
- php json_decode返回失败的排查
参考网址: https://segmentfault.com/a/1190000006154011 返回失败时,通过 json_last_error可以获取其json解析失败的原因.
- mysql5.7创建账户并授权
CREATE USER 'jeffrey'@'localhost' IDENTIFIED BY 'mypass'; GRANT ALL ON db1.* TO 'jeffrey'@'localhost ...
- UVa 10523 - Very Easy !!!
题目大意:给你一个公式,直接按照式子计算就可以了,要用到大数. import java.io.*; import java.util.*; import java.math.*; class Main ...
- spark 1.6 idea本地运行错误
将spark maven更新至1.6,运行原有程序,发现以下错误: java.lang.IllegalArgumentException: System memory must be at least ...
- PHP 中 static 和 self 的区别
使用 self:: 或者 __CLASS__ 对当前类的静态引用,取决于定义当前方法所在的类: 使用 static:: 不再被解析为定义当前方法所在的类,而是在实际运行时计算的.也可以称之为" ...
- ANT风格URL规则
转: 我们在看Java技术书籍的过程中,当加载文件时总会遇到是否支持ant风格路径加载,这里说的ant风格是什么意思呢,今天我查了一下,明白了什么意思,现在总结一下 ANT通配符有三种: 通配符 说明 ...
- Update关联查询不走索引,效率低下
优化一个sql,就是有A,B两个表,要利用b表的字段更新a表对应的字段.形如 Sql代码 update A set A.a=(select B.b from B where A.id=B.id); 原 ...
- HTML 表单元素、 输入类型、Input 属性
<input> 元素 最重要的表单元素是 <input> 元素. <input> 元素根据不同的 type 属性,可以变化为多种形态. 注释:下一章讲解所有 HTM ...