Bean的生命周期
2015-11-30 16:10
691 查看
1.domain
package com.baobaotao.domain; import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactoryAware; import org.springframework.beans.factory.BeanNameAware; import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.InitializingBean; public class Car implements BeanFactoryAware,BeanNameAware,InitializingBean,DisposableBean { private String brand; private String color; private double price ; private int maxSpeed; private BeanFactory beanFactory; private String beanName; /** * @return the brand */ public String getBrand() { return brand; } /** * @param brand the brand to set */ public void setBrand(String brand) { System.out.println("调用setBrand()设置属性"); this.brand = brand; } /** * @return the color */ public String getColor() { return color; } /** * @param color the color to set */ public void setColor(String color) { this.color = color; } /** * @return the maxSpeed */ public int getMaxSpeed() { return maxSpeed; } /** * @param maxSpeed the maxSpeed to set */ public void setMaxSpeed(int maxSpeed) { this.maxSpeed = maxSpeed; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public Car() { System.out.println("调用Car()构造函数"); } public Car(String brand, String color, int maxSpeed) { super(); this.brand = brand; this.color = color; this.maxSpeed = maxSpeed; } public void myInit() { System.out.println("调用myInit(),将maxSpeed设置为240。"); this.maxSpeed = 240; } public void myDestory() { System.out.println("调用myDestroy()。"); } /* (non-Javadoc) * @see java.lang.Object#toString() */ @Override public String toString() { return "Car [brand=" + brand + ", color=" + color + ", maxSpeed=" + maxSpeed + "]"; } public void introduce(){ System.out.println(this.toString()); } @Override public void destroy() throws Exception { System.out.println("调用DisposableBean.destroy()"); } @Override public void afterPropertiesSet() throws Exception { System.out.println("调用InitializingBean.afterPropertiesSet()"); } @Override public void setBeanName(String name) { System.out.println("调用beanNameAware.setBeanName()"); this.beanName = beanName; } @Override public void setBeanFactory(BeanFactory beanFactory) throws BeansException { System.out.println("调用beanFactoryAware.setBeanFactory()"); this.beanFactory = beanFactory; } }
2.适配器InstantiationAwareBeanPostProcessorAdapter
package com.baobaotao.beanfactory; import java.beans.PropertyDescriptor; import org.springframework.beans.BeansException; import org.springframework.beans.PropertyValues; import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter; public class MyInstantiationAwareBeanPostProcessor extends InstantiationAwareBeanPostProcessorAdapter{ public Object postProcessBeforeInstantiation(Class beanClass, String beanName)throws BeansException{ if("car".equals(beanName)){ System.out.println("InstantiationAware BeanPostProcessor.postProcessBeforeInstantiation"); } return null; } public boolean postProcessAfterInstantiation(Object bean, String beanName)throws BeansException{ if("car".equals(beanName)){ System.out.println("InstantiationAware BeanPostProcessor.postProcessAfterInstantiation"); } return true; } public PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName)throws BeansException{ if("car".equals(beanName)){ System.out.println("Instantiation AwareBeanPostProcessor.postProcessPropertyValues"); } return pvs; } }
3.BeanPostProcessor实现
package com.baobaotao.beanfactory; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; import com.baobaotao.domain.Car; public class MyBeanPostProcessor implements BeanPostProcessor{ @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { if(beanName.equals("car")){ Car car = (Car)bean; if (car.getColor()==null){ System.out.println("调用BeanPostProcessor.postProcess BeforeInitialization(),color为空,设置为默认黑色"); car.setColor("黑色"); } } return bean; } @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { if(beanName.equals("car")){ Car car = (Car)bean; if (car.getMaxSpeed()>=200){ System.out.println("调用BeanPostProcessor.postProcess AfterInitialization(),将maxSpeed调整为200"); car.setMaxSpeed(200);; } } return bean; } }
4.beans.xml配置
<?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-3.0.xsd"> <bean id="car" class="com.baobaotao.domain.Car" init-method="myInit" destroy-method="myDestory" p:brand="红旗CA72" p:maxSpeed="200" scope="singleton" /> </beans>
5.容器装载配置文件,注册两个处理器
package com.baobaotao.beanfactory; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.config.ConfigurableBeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import com.baobaotao.domain.Car; public class BeanLifeCycle { private static void LifeCycleInBeanFactory(){ Resource res = new ClassPathResource("com/baobaotao/beanfactory/beans.xml"); BeanFactory bf = new XmlBeanFactory(res); ((ConfigurableBeanFactory)bf).addBeanPostProcessor(new MyBeanPostProcessor()); ((ConfigurableBeanFactory)bf).addBeanPostProcessor(new MyInstantiationAwareBeanPostProcessor()); Car car1 = (Car)bf.getBean("car"); car1.introduce(); car1.setColor("红色"); Car car2 = (Car)bf.getBean("car"); System.out.println("car1==car2:"+(car1==car2)); ((XmlBeanFactory)bf).destroySingletons(); } public static void main(String[] args) { LifeCycleInBeanFactory(); } }
相关文章推荐
- 一个jar包里的网站
- 一个jar包里的网站之文件上传
- 一个jar包里的网站之返回对媒体类型
- 模拟Spring的简单实现
- spring+html5实现安全传输随机数字密码键盘
- Spring中属性注入详解
- struts2 spring整合fieldError问题
- spring的jdbctemplate的crud的基类dao
- 读取spring配置文件的方法(spring读取资源文件)
- Spring Bean基本管理实例详解
- java实现简单美女拼图游戏
- java基本教程之线程休眠 java多线程教程
- JSP开发中在spring mvc项目中实现登录账号单浏览器登录
- spring boot实战之内嵌容器tomcat配置
- 基于Spring框架的Shiro配置方法
- Spring MVC中上传文件实例
- spring aop两种配置方式
- 实例讲解Java的Spring框架中的AOP实现
- Java的Spring框架的三种连接池的基本用法示例
- 解析Java中如何获取Spring中配置的bean