Spring BeanFactory实例化Bean的过程
private String name;
private int age;
public Person(){
System.out.println("--------|| 构造方法在调用...");
this.name = "默认用户名";
this.age= 250;
System.out.println(this);
}
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 String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
public void init(){
System.out.println("--------|| init()正在调用...");
this.name="init";
this.age=998;
System.out.println(this);
}
public void afterPropertiesSet() throws Exception {
System.out.println("--------|| afterPropertiesSet()正在调用...");
this.age=999;
System.out.println(this);
}
public void destroy() throws Exception {
System.out.println("--------|| destroy()正在调用...");
}
}
private static ApplicationContext _instance;
static {
if (_instance == null) _instance = buildApplicationContext();
}
private BeanContextHelper() {
}
/**
* 重新构建ApplicationContext对象
*
* @return ApplicationContext
*/
public static ApplicationContext buildApplicationContext() {
return new ClassPathXmlApplicationContext("applicationContext.xml");
}
/**
* 获取一个ApplicationContext对象
*
* @return ApplicationContext
*/
public static ApplicationContext getApplicationContext() {
return _instance;
}
}
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="person" class="com.lavasoft.springnote.ch01.Person" init-method="init" destroy-method="destroy">
<property name="name">
<value>lavasoft</value>
</property>
<property name="age">
<value>22</value>
</property>
</bean>
</beans>
* Created by IntelliJ IDEA.<br>
* <b>User</b>: leizhimin<br>
* <b>Date</b>: 2008-4-17 16:58:07<br>
* <b>Note</b>: 客户端测试
*/
public class Test {
public static void main(String args[]) {
Test test = new Test();
test.test();
}
public void test(){
ApplicationContext context = BeanContextHelper.getApplicationContext();
Person person = (Person) context.getBean("person");
System.out.println("--------|| 从Spring BeanFactory获取person...");
System.out.println(person);
}
}
log4j:WARN No appenders could be found for logger (org.springframework.core.CollectionFactory).
log4j:WARN Please initialize the log4j system properly.
Person{name='默认用户名', age=250}
--------|| afterPropertiesSet()正在调用...
Person{name='lavasoft', age=999}
--------|| init()正在调用...
Person{name='init', age=998}
--------|| 从Spring BeanFactory获取person...
Person{name='init', age=998}
Process finished with exit code 0
只有一个方法void setBeanName(String name),用来设置bean的名字.
DisposableBean
为Bean的销毁提供回调功能,在bean实例销毁前调用接口的destroy()方法.
InitializingBean
Spirng的InitializingBean为bean提供了定义初始化方法的方式。InitializingBean是一个接口,它仅仅包含一个方法:afterPropertiesSet()。
Spring BeanFactory实例化Bean的过程相关推荐
- Spring生命周期Bean初始化过程详解
Spring生命周期Bean初始化过程详解 Spring 容器初始化 Spring Bean初始化 BeanFactory和FactoryBean 源码分析 Bean的实例化 preInstantia ...
- 第三节 Spring加载bean的过程
前言 通过前面两节的学习我们已经知道了Spring是如何解析XML与装载BeanDefinition的:在本章节中我们将继续学习bean的装载过程这将会面临更大的挑战,bean加载的功能实现远比bea ...
- 关于Spring容器管理Bean的过程以及加载模式
1.需要将bean的定义信息声明在Spring的配置文件中: 2.需要通过Spring抽象出的各种Resource来指定对应的配置文件: 3.需要显示声明一个Spring工厂,该工厂用来掌控我们在配置 ...
- spring容器实例化bean的3种方式
1. 使用公共的无参构造器 2. 使用静态工厂 (与设计模式的工厂无关) 3. 使用实例工厂 (与设计模式的工厂无关) 注解:第一种常用 转载于:https://www.cnblogs.com/wan ...
- 【Spring】BeanFactory解析bean详解
为什么80%的码农都做不了架构师?>>> 本文是Spring源码分析中的一篇,来讲讲Spring框架中BeanFactory解析bean的过程,先来看一个在Spring中一个基 ...
- Spring装配Bean的过程
首先说一个概念:"懒加载" 懒加载:就是我们在spring容器启动的是先不把所有的bean都加载到spring的容器中去,而是在当需要用的时候,才把这个对象实例化到容器中. spr ...
- Spring源码系列(十二)Spring创建Bean的过程(二)
1.写在前面 上篇博客主要Spring在创建Bean的时候,第一次调用的Bean的后置处理器的过程,同时笔者也打算将整个Spring创建的Bean的过程,通过这个系列,将Bean的创建过程给讲清楚,废 ...
- 通过Debug带你详细了解Spring创建Bean的过程,一清二楚!
Spring流程Debug 1.1 Spring测试环境搭建 Spring模块概览,绿色是模块,Spring中八大模块,黑色表示该模块包含的jar包(组件).例如我们想要用IOC容器,也就是绿色的Co ...
- Spring 初始化与 Bean 初始化
目录 1.Spring 容器初始化 2.Spring Bean 初始化 2.1 BeanFactory 和 FactoryBean 2.2 Bean 的实例化 Spring生命周期Bean初始化过程详 ...
最新文章
- dos命令关闭所有dos窗口
- 远程桌面无法复制文本时解决办法
- Java GC系列(4):垃圾回收监视和分析
- drools dmn_DMN 1.1 XML:从建模到使用Drools 7.0的自动化
- ubuntu如何安装samba
- python CV2裁剪图片并保存
- Xshell连接阿里云服务器ECS
- 凯利公式自动计算表_EXCEL——可自动计算的收支表,全函数计算,拿上就用!
- linux mac docky,Ubuntu 7.10中安装酷酷的MAC风格 dock(图)
- zend studio设置自动保存
- TDtree冲刺第四天
- java怎么控制页面打印次数_java – 控制打印页面上面板的大小
- Python——词频统计
- CNKI E-Study与Endnote 的参考文献题录互导
- 深度学习之Bias/Variance偏差、方差
- word里画的流程图怎么全选_流程图怎么做?用Word制作流程图超方便!
- java 转16进制_java中进制的转换,Byte与16进制的转换方法
- linux 进程 ldt,LInux 描述符GDT, IDT LDT结构定义
- 抢答器c语言程序设计,基于单片机的智能抢答器设计与实现
- 自定义View之仿QQ运动步数进度效果