public class BeanLifeCycle {
public static void main(String[] args) {
Resource resource = new ClassPathResource("application-context/spring/beans.xml");
BeanFactory beanFactory = new XmlBeanFactory(resource);
((ConfigurableBeanFactory)beanFactory).addBeanPostProcessor(new MyBeanPostProcessor());
((ConfigurableBeanFactory)beanFactory).addBeanPostProcessor(new MyInstantiationAwareBeanPostProcessor());
((ConfigurableBeanFactory)beanFactory).addBeanPostProcessor(new InitDestroyAnnotationBeanPostProcessor());
Car car1 = (Car) beanFactory.getBean("car");
System.out.println(car1.toString());
car1.setColor("red");
System.out.println("****************************************************************");
Car car2 = (Car) beanFactory.getBean("car");
System.out.println(car2.toString());
System.out.println("car1 == car2: " + (car1 == car2));
((XmlBeanFactory)beanFactory).destroySingletons();
}
}
public class MyInstantiationAwareBeanPostProcessor extends InstantiationAwareBeanPostProcessorAdapter {
@Override
public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
if ("car".equals(beanName)) {
System.out.println("MyInstantiationAwareBeanPostProcessor.postProcessBeforeInstantiation");
}
return super.postProcessBeforeInstantiation(beanClass, beanName);
}
@Override
public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
if ("car".equals(beanName)) {
System.out.println("MyInstantiationAwareBeanPostProcessor.postProcessAfterInstantiation");
}
return super.postProcessAfterInstantiation(bean, beanName);
}
@Override
public PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException {
if ("car".equals(beanName)) {
System.out.println("MyInstantiationAwareBeanPostProcessor.postProcessPropertyValues");
}
return super.postProcessPropertyValues(pvs, pds, bean, beanName);
}
}
public class MyBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object object, String beanName) throws BeansException {
if ("car".equals(beanName)) {
Car car = (Car) object;
if (car.getColor() == null) {
System.out.println("MyBeanPostProcessor.postProcessBeforeInitialization. set Color black");
car.setColor("black");
}
}
return object;
}
@Override
public Object postProcessAfterInitialization(Object object, String beanName) throws BeansException {
if ("car".equals(beanName)) {
Car car = (Car) object;
if (car.getMaxSpeed() > 200) {
System.out.println("MyBeanPostProcessor.postProcessAfterInitialization, set MaxSpeed to 200.");
car.setMaxSpeed(200);
}
}
return object;
}
}
public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
BeanDefinition db = configurableListableBeanFactory.getBeanDefinition("car");
db.getPropertyValues().addPropertyValue("brand", "qiruiQQ");
System.out.println("MyBeanFactoryPostProcessor.postProcessBeanFactory");
}
}
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("call constructor");
}
public Car(String brand, String color, int maxSpeed) {
this.brand = brand;
this.color = color;
this.maxSpeed = maxSpeed;
}
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
System.out.println("BeanFactoryAware setBeanFactory");
this.beanFactory = beanFactory;
}
@Override
public void setBeanName(String s) {
System.out.println("BeanNameAware setBeanName");
}
@Override
public void destroy() throws Exception {
System.out.println("DisposableBean destroy");
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("InitializingBean afterPropertiesSet");
}
public void setBrand(String brand) {
System.out.println("call setBrand");
this.brand = brand;
}
public void setColor(String color) {
System.out.println("call setColor");
this.color = color;
}
public void setMaxSpeed(int maxSpeed) {
System.out.println("call setMaxSpeed");
this.maxSpeed = maxSpeed;
}
public String getBrand() {
return brand;
}
public String getColor() {
return color;
}
public int getMaxSpeed() {
return maxSpeed;
}
public void myInit() {
System.out.println("init-method.");
this.maxSpeed = 240;
}
public void myDestroy() {
System.out.println("destroy-method.");
}
@PostConstruct
public void postConstruct() {
System.out.println("postConstruct");
}
@PreDestroy
public void preDestroy() {
System.out.println("preDestroy");
}
@Override
public String toString() {
return "Car{" +
"brand='" + brand + '\'' +
", color='" + color + '\'' +
", maxSpeed=" + maxSpeed +
'}';
}
}
<?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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:security="http://www.springframework.org/schema/security"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
http://www.springframework.org/schema/data/repository http://www.springframework.org/schema/data/repository/spring-repository-1.6.xsd ">
<bean id="car" class="org.anicloud.spring4.spring.lifecycle.Car"
p:brand="bwm"
p:maxSpeed="200"
init-method="myInit"
destroy-method="myDestroy"
scope="singleton"/>
</beans>
分享到:
相关推荐
Spring bean生命周期demo
spring bean 的生命周期,把运行结果的日志,用sublime打开对比查看,你会有比较清晰的认识
Springbean生命周期
Spring Bean 生命周期之“我从哪里来?”
Spring Bean 的生命周期在整个 Spring 中占有很重要的位置,掌握这些可以加深对 Spring 的理解。这篇文章主要介绍了Spring Bean 生命周期,需要的朋友可以参考下
虽然可以随意配置 <bean> 的属性,但是建议不要过多地使 Bean 实现接,因为这样会导致代码和 Spring 的聚合过于紧密第 1 步:实例化Bean第
SpringBean的生命周期.mdj
本篇文章主要介绍了浅谈Spring bean 生命周期验证,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
这个工程主要实现了: Spring中Bean的生命周期 applicationcontext的应用(实现国际化,事件的传递)
主要介绍了Spring bean生命周期配置过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
此资源是我的博客bean的生命周期的测试代码,只有源代码,没有相关库文件,环境是spring4.2 ,
主要介绍了Spring Bean的生命周期的相关资料,需要的朋友可以参考下
本文主要介绍 Spring IoC 容器如何管理 Bean 的生命周期。 在应用开发中,常常需要执行一些特定的初始化工作,这些工作都是相对比较固定的,比如建立数据库连接,打开网络连接等,同时,在结束服务时,也有一些相对...
主要给大家介绍了Spring中Bean的生命周期和作用域及实现方式的相关资料,文中介绍的非常详细,对大家具有一定的参考价值,需要的朋友们下面来一起看看吧。
介绍了Spring的Bean周期,容器周期,工厂周期,运行代码后可以看到运行结果
本篇文章主要介绍了spring之Bean的生命周期详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
本篇文章主要介绍了深入理解Spring中bean的生命周期介绍,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
Spring的生命周期是指实例化Bean时所经历的一系列阶段,即通过getBean()获取bean对象及设置对象属性时,Spring框架做了哪些事。Bean的生命周期从Spring容器实例化Bean到销毁Bean。 本文分别对 BeanFactory 和 ...
学习Spring过程中,使用Eclipse调试Spring源码的关键断点文件。