Spring IOC容器对bean的生命周期进行管理的过程
1.通过构造器或者工厂方法创建bean的实例
2.为bean的属性设置值和对其他bean的引用
3.将bean的实例传递给bean的后置处理器BeanPostProcessor的postProcessBeforeInitialization方法
4.调用bean的初始化方法
5.调用bean的后置处理器BeanPostProcessor的postProcessAfterInitialization方法
6.使用bean容器
7.调用bean的销毁方法
创建Car类 定义以下方法
public class Car {
public Car() {
System.out.println("create car");
}
private String brand;
public void setBrand(String brand) {
System.out.println("set brand");
this.brand = brand;
}
public void init() {
System.out.println("init ...");
}
public void destroy() {
System.out.println("destroy...");
}
@Override
public String toString() {
return "Car [brand=" + brand + "]";
}
}
创建一个类实现BeanPostProcessor接口 重写两个方法
public class MyBeanPostProcessor implements BeanPostProcessor{
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
// TODO Auto-generated method stub
System.out.println("postProcessBeforeInitialization"+bean+","+beanName);
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
// TODO Auto-generated method stub
System.out.println("postProcessAfterInitialization"+bean+","+beanName);
return bean;
}
}
在applicationContext.xml中配置bean
<bean id="car" class="beancycle.Car"
init-method="init" destroy-method="destroy">
<property name="brand" value="aodi"></property>
</bean>
<bean class="beancycle.MyBeanPostProcessor"></bean>
编写main方法测试
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Car car = (Car)context.getBean("car");
car.destroy();
}
输出结果如下
create car
set brand
postProcessBeforeInitializationCar [brand=aodi],car
init ...
postProcessAfterInitializationCar [brand=aodi],car
destroy...
Spring IOC容器对bean的生命周期进行管理的过程的更多相关文章
- spring IOC 容器中 Bean 的生命周期
IOC 容器中 Bean 的生命周期: 1.通过构造器或工厂方法创建 Bean 实例 2.为 Bean 的属性设置值和对其他 Bean 的引用 3.调用 Bean 后置处理器接口(BeanPostPr ...
- Spring IOC容器中Bean的生命周期
1.IOC容器中Bean的生命周期 构造器函数 设置属性 初始化函数(在Bean配置中 init-method) 使用Bean 结束时关闭容器(在Bean中配置destroy-method) 2.Be ...
- Spring(十二):IOC容器中Bean的生命周期方法
IOC容器中Bean的生命周期方法 1)Spring IOC容器可以管理Bean的声明周期,Spring允许在Bean生命周期的特定点执行定制的任务. 2)Spring IOC容器对Bean的生命周期 ...
- Spring学习-- IOC 容器中 bean 的生命周期
Spring IOC 容器可以管理 bean 的生命周期 , Spring 允许在 bean 声明周期的特定点执行定制的任务. Spring IOC 容器对 bean 的生命周期进行管理的过程: 通过 ...
- IOC容器中bean的生命周期
一.Bean的生命周期 Spring IOC容器可以管理Bean的生命周期,允许在Bean生命周期的特定点执行定制的任务. Spring IOC容器对Bean的生命周期进行管理的过程如下: (1).通 ...
- Spring重点—— IOC 容器中 Bean 的生命周期
一.理解 Bean 的生命周期,对学习 Spring 的整个运行流程有极大的帮助. 二.在 IOC 容器中,Bean 的生命周期由 Spring IOC 容器进行管理. 三.在没有添加后置处理器的情况 ...
- Spring容器中bean的生命周期以及关注spring bean对象的后置处理器:BeanPostProcessor(一个接口)
Spring IOC 容器对 Bean 的生命周期进行管理的过程: 1.通过构造器或工厂方法创建 Bean 实例 2.为 Bean 的属性设置值和对其他 Bean 的引用 3.将 Bean 实例传递给 ...
- Spring4学习回顾之路06- IOC容器中Bean的生命周期方法
SpringIOC容器可以管理Bean的生命周期,Spring允许在Bean生命周期的特定点执行特定的任务! Spring IOC容器对Bean的生命周期进行管理的过程: -通过构造器或者工厂方法创建 ...
- [原创]java WEB学习笔记101:Spring学习---Spring Bean配置:IOC容器中bean的声明周期,Bean 后置处理器
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
随机推荐
- jpa 分页
public Page<Stability> testPager(){ Pageable pageable = new PageRequest(1, 10, Sort.Direction. ...
- Linux Swap交换分区探讨
Swap交换分区概念 Linux divides its physical RAM (random access memory) into chucks of memory called pages. ...
- 正则表达式regex(golang版)
代码: //File: main.go package main import ( "fmt" "regexp" ) func main() { r := re ...
- 【题解】P1171 售货员的难题
Tags 搜索,状压. 裸的旅行商问题 #include <stdio.h> #include <string.h> #define re register #define ...
- go笔记-pprof使用
go tool pprof http://localhost:6060/debug/pprof/profile go tool pprof http://localhost:6060/debug/pp ...
- nginx中的epoll模型
要了解epoll模型,就要一个一个知识点由浅至深地去探索. 1.IO复用技术 IO流请求操作系统内核,有串行处理和并行处理两种概念. 串行处理是前面一个操作处理地时候,后面的所有操作都需要等待.因此, ...
- 其它综合-VMware虚拟机安装Ubuntu 19.04 版本
Ubuntu 19.04 版本安装过程 1. 环境: 使用的虚拟机软件是VMware,版本为 12 .(网上一搜一大推,在此不再演示.) 使用的 ISO镜像为Ubuntu 19.04.(自己也可以在网 ...
- Python抓取天气信息并存储原来这么简单
我们计划抓取的数据:杭州的天气信息 实现数据抓取的逻辑:使用python 请求 URL,会返回对应的 HTML 信息,我们解析 html,获得自己需要的数据.(很简单的逻辑) 第一步:创建 Pytho ...
- Thymeleaf的超链接与AJAX的跳转问题
//th:href :超链接<a th:href="@{/list}"></a>//可以在其他页面跳转yt <form id="msform ...
- 通过SQL脚本来查询SQLServer 中主外键关系
在SQLServer中主外键是什么,以及主外键如何创建,在这里就不说了,不懂的可以点击这里,这篇文章也是博客园的博友写的,我觉得总结的很好: 此篇文章主要介绍通过SQL脚本来查看Sqlserver中主 ...