《精通Spring4.X企业应用开发实战》读后感第四章(BeanFactory生命周期)










package com.smart; import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*; public class Car implements BeanFactoryAware, BeanNameAware, InitializingBean, DisposableBean{
private String brand;
private String color;
private int maxSpeed; private BeanFactory beanFactory;
private String beanName; //默认构造函数
public Car(){System.out.println("调用Car()构造函数");} //带参构造函数
public Car(String brand, String color, int maxSpeed) {
this.brand = brand;
this.color = color;
this.maxSpeed = maxSpeed;
} //未带参方法
public void introduce() {
System.out.println("brand:" + brand + ";color:" + color + ";maxSpeed:" + maxSpeed);
} public String getBrand() {
return brand;
} public void setBrand(String brand) {
System.out.println("调用setBrand()设置属性");
this.brand = brand;
} public String getColor() {
return color;
} public void setColor(String color) {
this.color = color;
} public int getMaxSpeed() {
return maxSpeed;
} public void setMaxSpeed(int maxSpeed) {
this.maxSpeed = maxSpeed;
} // BeanFactoryAware接口方法
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
System.out.println("调用BeanFactoryAware.setBeanFactory().");
this.beanFactory = beanFactory;
} // BeanNameAware接口方法
public void setBeanName(String s) {
System.out.println("调用BeanNameAware.setBeanName().");
this.beanName = s;
} // DisposableBean接口方法
public void destroy() throws Exception {
System.out.println("调用DisposableBean.destroy().");
} // InitializingBean接口方法
public void afterPropertiesSet() throws Exception {
System.out.println("调用InitialingBean.afterPropertiesSet().");
} // 通过<bean>的init-method属性指定的初始方法
public void myInit() {
System.out.println("调用init-method属性指定的myInit(), 将maxSpeed设置为240");
this.maxSpeed = 240;
} //同过<bean>的destroy-method属性指定的销毁方法
public void myDestroy() {
System.out.println("调用destroy-method属性指定的myDestroy().");
} }

package com.smart.beanfactory; import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter; import java.beans.PropertyDescriptor; public class MyInstantiationAwareBeanPostProcessor extends InstantiationAwareBeanPostProcessorAdapter { @Override
// 1.接口方法,在实例化Bean前调用
public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
// 1-1. 仅对容器中的car Bean进行处理
if ("car".equals(beanName)) {
System.out.println("调用InstantiationAwareBeanPostProcessor.postProcessBeforeInstantiation().");
}
return null;
} @Override
// 2.接口方法,在实例化Bean后调用
public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
if ("car".equals(beanName)) {
System.out.println("调用InstantiationAwarePostProcessor.postProcessorAfterInstantiation().");
}
return true;
} @Override
// 3.接口方法,在设置某个属性时调用
public PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException {
// 3-1. 仅对容器中的car Bean进行处理, 还可以同过pds入参进行过滤
if ("car".equals(beanName)) {
System.out.println("调用InstantiationAwarePostProcessor.postProcessPropertyValues().");
}
return pvs;
}
}

package com.smart.beanfactory; import com.smart.Car;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor; public class MyBeanPostProcessor implements BeanPostProcessor {
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if ("car".equals(beanName)) {
Car car = (Car) bean;
if (car.getColor() == null) {
System.out.println("调用BeanPostProcessor.postProcessBeforeInitialization()," +
"若color为空,设置为默认黑色");
car.setColor("黑色");
}
}
return bean;
} public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if ("car".equals(beanName)) {
Car car = (Car) bean;
if (car.getMaxSpeed() >= 200) {
System.out.println("调用BeanPostProcessor.postProcessAfterInitialization()," +
"将maxSpeed设置为200");
car.setMaxSpeed(200);
}
}
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" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="car" class="com.smart.Car"
init-method="myInit"
destroy-method="myDestroy"
p:brand="红旗CA72"
p:maxSpeed="200"/> </beans>


package com.smart.beanfactory; import com.smart.Car;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource; public class BeanLifeCycle {
public static void lifeCycleInBeanFactory() {
// 1. 装载配置文件并启动容器
Resource res = new ClassPathResource("beans.xml");
BeanFactory bf = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader((DefaultListableBeanFactory) bf);
reader.loadBeanDefinitions(res); //后处理器的实际调用顺序与注册顺序无关,在具有多个后处理器的情况下,必须通过实现
//org.springframework.core.Ordered接口来确定调用顺序
// 2. 向容器中注册MyBeanPostProcessor后处理器
((ConfigurableBeanFactory) bf).addBeanPostProcessor(new MyBeanPostProcessor());
// 3. 向容器中注册MyInstantiationAwareBeanPostProcessor后处理器
((ConfigurableBeanFactory) bf).addBeanPostProcessor(new MyInstantiationAwareBeanPostProcessor()); // 4. 第一次从容器中获得car, 将触发容器实例化该Bean,这将引发Bean生命周期方法的调用
Car car1 = (Car) bf.getBean("car");
car1.introduce();
car1.setColor("红色"); // 5. 第二次从容器中获取car, 直接从缓存池中获取
Car car2 = (Car) bf.getBean("car"); // 6. 查看car1和car2是否指向同一引用
System.out.println("car1==car2: " + (car1 == car2)); // 7. 关闭容器
((DefaultListableBeanFactory) bf).destroySingleton("car"); } public static void main(String[] args) {
lifeCycleInBeanFactory();
}
}








《精通Spring4.X企业应用开发实战》读后感第四章(BeanFactory生命周期)的更多相关文章
- 《精通Spring4.x企业应用开发实战》第三章
这一章节主要介绍SpringBoot的使用,也是学习的重点内容,之后就打算用SpringBoot来写后台,所以提前看一下还是很有必要的. 3.SpringBoot概况 3.1.1SpringBoot发 ...
- 《精通Spring4.X企业应用开发实战》读后感第四章(资源访问)
package com.smart.resource; import org.springframework.core.io.ClassPathResource; import org.springf ...
- 《精通Spring4.X企业应用开发实战》读后感第七章(创建增强类)
- 《精通Spring4.X企业应用开发实战》读后感第七章(AOP基础知识、jdk动态代理,CGLib动态代理)
- 《精通Spring4.X企业应用开发实战》读后感第七章(AOP概念)
- 《精通Spring4.X企业应用开发实战》读后感第六章(容器事件)
- 《精通Spring4.X企业应用开发实战》读后感第六章(国际化)
- 《精通Spring4.X企业应用开发实战》读后感第六章(引用Bean的属性值)
- 《精通Spring4.X企业应用开发实战》读后感第六章(使用外部属性文件)
- 《精通Spring4.X企业应用开发实战》读后感第六章(属性编辑器)
随机推荐
- 九度OJ 1012:畅通工程 (最小生成树)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:7052 解决:3034 题目描述: 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府"畅通工 ...
- git服务的安装和使用
参考文章 http://www.centoscn.com/image-text/install/2014/0514/2972.html 1.搭建Git服务器yum安装Git服务器创建一个git用户,用 ...
- .htaccess技巧: URL重写(Rewrite)与重定向(Redirect) (转)
目录 Table of Contents 一.准备开始:mod_rewrite 二.利用.htaccess实现URL重写(rewrite)与URL重定向(redirect) 将.htm页面映射到.ph ...
- Java中的内存泄漏
[转]介绍Java中的内存泄漏 1. 什么是内存泄漏? 内存泄漏的定义:对象已经没有被应用程序使用,但是垃圾回收器没办法移除它们,因为还在被引用着. 要想理解这个定义,我们需要先了解一下对象在内存中的 ...
- SQL语句性能优化操作
1.对查询进行优化,应尽量避免全表扫描,首先应考虑在where及order by涉及的列上建立索引. 2.应尽量避免在where子句中对字段进行null值判断,创建表时NULL是默认值,但大多数时候应 ...
- 《CSS权威指南(第三版)》---第六章 文本属性
本章主要的内容是: 1.文本缩进: text-indent.行内元素无法缩进,一般用左内边距或外边距来创造这种效果. 2.文本对齐:text-align .只应用于块状元素. 3.行高:一般line- ...
- Uncaught TypeError: Illegal invocation解决
jquery中报了这个错,仔细一看,有个使用ajax的地方,其中有个参数是从页面某个文本框获取的,本应该 $('#id').value ,被我写成了 $('id') .所以报错,目前已解决.
- 9.1 NOIP普及组试题精解(2)
9-4 soldier.c #include <stdio.h> #define MAXN 21 }; int n, m, x, y; //n,m为B点的行列坐标位置,x,y为马的坐标位置 ...
- <tx:advice/> 有关的设置
将描述通过 <tx:advice/> 标签来指定不同的事务性设置.默认的 <tx:advice/> 设置如下: 事务传播设置是 REQUIRED 隔离级别是 DEFAULT 事 ...
- 使用.net实现ZooKeeper客户端
最近在项目中用到ZooKeeper, 通过Java连接比较容易,.net项目就没那么容易,尤其对于不熟悉Linux的开发人员,这里写点搭建ZooKeeper测试环境的经验,供参考. 背景知识: Zoo ...