Spring入门第十一课
IOC容器中Bean的生命周期
Spring IOC容器可以管理Bean的生命周期,Spring允许在Bean生命周期的特定点执行定制的任务。
Spring IOC容器对Bean的生命周期进行管理的过程:
-通过构造器或工厂方法创建Bean实例
-为Bean的属性值和对其他Bean的引用
-调用Bean的初始化方法
-Bean可以使用了
-当容器关闭时,调用Bean的销毁方法
在Bean的声明里设置init-method和destroy-method属性,为Bean指定初始化和销毁方法。
下面看代码
package logan.spring.study.cycle;
public class Car {
public Car() {
// TODO Auto-generated constructor stub
System.out.println("Car's Constructor...");
}
private String brand;
public void setBrand(String brand) {
this.brand = brand;
}
public void init(){
System.out.println("init...");
}
public void destroy(){
System.out.println("destroy...");
}
}
配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="car" class="logan.spring.study.cycle.Car"
init-method="init"
destroy-method="destroy">
<property name="brand" value="Audi"></property>
</bean> </beans>
测试代码
package logan.spring.study.cycle;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans-cycle.xml");
Car car = (Car) ctx.getBean("car");
System.out.println(car);
ctx.close();
}
}
输出结果
五月 21, 2017 10:21:56 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@7aec35a: startup date [Sun May 21 10:21:56 CST 2017]; root of context hierarchy
五月 21, 2017 10:21:56 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [beans-cycle.xml]
Car's Constructor...
init...
logan.spring.study.cycle.Car@523884b2
五月 21, 2017 10:21:56 上午 org.springframework.context.support.ClassPathXmlApplicationContext doClose
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@7aec35a: startup date [Sun May 21 10:21:56 CST 2017]; root of context hierarchy
destroy...
创建Bean后置处理器
Bean后置处理器允许在调用初始化方法前后对Bean进行而外的处理。
Bean后置处理器对IOC容器里的所有Bean实例逐一处理,而非单一的实例,器典型应用是:检查Bean属性的正确性或者根据特定的标准更改Bean的属性。
对Bean后置处理器而言,需要实现Interface BeanPostProcessor接口,在初始化方法被调用前后,Spring将会把每个Bean实例分别传递给上述接口的一下两个方法:
postProcessAfterInitialization(Object bean,String beanName)
postProcessBeforeinitialization(Object bean, String beanName)
添加Bean后置处理器后Bean的生命周期
Spring IOC容器对Bean的生命周期进行管理的过程:
-通过构造器或者工厂方法创建Bean实例
-为Bean的属性设置值和对其他Bean的引用
-将Bean实例传递给Bean后置处理器的postProcessBeforeInitialization方法
-调用Bean的初始化方法
-将Bean实例传递给Bean后置处理器的postProcessAfterInitialization方法
-Bean可以使用了
-当关不容器时,调用Bean的销毁方法。
看下面代码:
package logan.spring.study.cycle; import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor; public class MyBeanPostProcess implements BeanPostProcessor { @Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
// TODO Auto-generated method stub
System.out.println("postProcessAfterInitialization" + ", " + bean + ", " + beanName);
return bean;
} @Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
// TODO Auto-generated method stub
System.out.println("postProcessBeforeInitialization" + ", " + bean + ", " + beanName);
return 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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="car" class="logan.spring.study.cycle.Car"
init-method="init"
destroy-method="destroy">
<property name="brand" value="Audi"></property>
</bean> <bean class="logan.spring.study.cycle.MyBeanPostProcess"></bean> </beans>
下面是输出结果:
五月 21, 2017 10:44:40 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@7aec35a: startup date [Sun May 21 10:44:40 CST 2017]; root of context hierarchy
五月 21, 2017 10:44:40 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [beans-cycle.xml]
Car's Constructor...
postProcessBeforeInitialization, logan.spring.study.cycle.Car@4b9e13df, car
init...
postProcessAfterInitialization, logan.spring.study.cycle.Car@4b9e13df, car
logan.spring.study.cycle.Car@4b9e13df
五月 21, 2017 10:44:41 上午 org.springframework.context.support.ClassPathXmlApplicationContext doClose
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@7aec35a: startup date [Sun May 21 10:44:40 CST 2017]; root of context hierarchy
destroy...
BeanProcessor很强大,可以偷梁换柱,如下代码:
package logan.spring.study.cycle;
public class Car {
public Car() {
// TODO Auto-generated constructor stub
System.out.println("Car's Constructor...");
}
private String brand;
public void setBrand(String 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 + "]";
}
}
package logan.spring.study.cycle; import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor; public class MyBeanPostProcess implements BeanPostProcessor { @Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
// TODO Auto-generated method stub
System.out.println("postProcessAfterInitialization" + ", " + bean + ", " + beanName);
Car car = new Car();
car.setBrand("Ford");
return car;
} @Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
// TODO Auto-generated method stub
System.out.println("postProcessBeforeInitialization" + ", " + bean + ", " + beanName);
return bean;
} }
下面是输出结果:
五月 21, 2017 10:49:29 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@7aec35a: startup date [Sun May 21 10:49:29 CST 2017]; root of context hierarchy
五月 21, 2017 10:49:29 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [beans-cycle.xml]
Car's Constructor...
postProcessBeforeInitialization, Car [brand=Audi], car
init...
postProcessAfterInitialization, Car [brand=Audi], car
Car's Constructor...
Car [brand=Ford]
五月 21, 2017 10:49:29 上午 org.springframework.context.support.ClassPathXmlApplicationContext doClose
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@7aec35a: startup date [Sun May 21 10:49:29 CST 2017]; root of context hierarchy
destroy...
Spring入门第十一课的更多相关文章
- Spring入门第六课
XML配置里的Bean自动装配 Spring IOC容器可以自动装配Bean.需要做的仅仅是在<bean>的autowire属性里指定自动装配的模式 ByType(根据类型自动装配):若I ...
- Spring入门第五课
集合属性 在Spring中可以通过一组内置的xml标签(如:<list>,<set>,<map>)来配置集合属性. 配置java.util.List类型的属性,需要 ...
- Spring入门第四课
注入参数详解:null值和级联属性 可以使用专用的<null/>元素标签为Bean的字符串或其他对象类型的属性注入null值. 和Struts,Hiberante等框架一样,Spring支 ...
- Spring入门第三课
属性注入 属性注入就是通过setter方法注入Bean的属性值或依赖的对象. 属性植入使用<property>元素,使用name属性指定Bean的属性名称,value属性或者<val ...
- Spring入门第十三课
通过FactoryBean来配置Bean package logan.spring.study.factoryBean; public class Car { private String brand ...
- Spring入门第十课
Spring表达式语言:SpEL Spring表达式语言(简称SpEL)是一个支持运行时查询和操作对象图的强大的表达式语言. 语法类似于EL:SpEL使用#{...}作为定界符,所有在大括号中的字符都 ...
- Spring入门第八课
看如下代码 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http:// ...
- Spring入门第七课
Bean之间的关系:继承和依赖. 继承Bean配置 Spring允许继承bean的配置,被继承的bean称为父bean,继承这个父bean的Bean称为子Bean. 子Bean从父Bean中继承配置, ...
- Spring入门第十七课
AOP编程 问题: 代码混乱: 越来越多的非业务需求(日志和验证等)加入后,原有的业务方法急剧膨胀,每个方法在处理核心逻辑的同事还必须兼顾其他多个关注点. 代码分散:以日志需求为例,只是为了满足这个单 ...
随机推荐
- HDU 6208 The Dominator of Strings 后缀自动机
The Dominator of Strings Time Limit: 3000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java ...
- idea创建普通java项目以及maven创建项目过程(转)
1. idea创建一个普通项目流程 http://blog.csdn.net/testcs_dn/article/details/52303941 2. idea创建maven项目流程 http:// ...
- 【题解】At2370 Piling Up
[题解]At2370 Piling Up \[ dp(i,j,0/1) \\ 正在进行i项操作并且此时黑球剩下j个,黑球[0/1]数量曾经到过0 \\ 为什么加第二位,判重.怎么想到的? \] 非常神 ...
- python 搜集参数的共有项和所有项
搜集共性项和所有项 ###搜集共有参数值 def intersect(*args): res=[] for x in args[0]: for other in args[1:]: if x not ...
- 使用maven3 创建自定义的archetype
创建自己的archetype一般有两种方式,比较简单的就是create from project 1.首先使用eclipse创建一个新的maven project,然后把配置好的一些公用的东西放到相应 ...
- import org.marker.weixin.DefaultSession; import org.marker.weixin.HandleMessageAdapter; import org.marker.weixin.MySecurity; import org.marker.weixin.msg.*;
需要以下微信包可以添加我的微信公众号 回复“微信api”即可得到jar链接,以及maven添加本地jar方法,以及更改后的源代码 import org.marker.weixin.DefaultSes ...
- terminal中 启动ios模拟器,并安装软件
启动运行模拟器: xcrun instruments -w 'iPhone 6 Plus' 在已经启动好的模拟器中安装应用: xcrun simctl install booted Calculato ...
- apache 网页301重定向、自定义400/403/404/500错误页面
首先简单介绍一下,.htaccess文件是Apache服务器中的一个配置文件(Nginx服务器没有),它负责相关目录下的网页配置.通过对.htaccess文件进行设置,可以帮我们实现:网页301重定向 ...
- ThreadLocalMap里Entry声明为WeakReference
Java里,每个线程都有自己的ThreadLocalMap,里边存着自己私有的对象.Map的Entry里,key为ThreadLocal对象,value即为私有对象T.在spring MVC中,常用T ...
- 【linux】记录一次系统被攻击的处理过程
今天登录zabbix监控网页的时候发现非常卡,登录到系统里面以后,通过top看,CPU已经100%了,有一个叫做httpds的进程占用,第一反映就是系统被入侵了,下面记录了处理过程,仅供各位参考 通过 ...