beans-cycle.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="car" class="com.hy.spring.beans.cycle.Car"
init-method="init"
destroy-method="destroy">
<property name="brand" value="AuDi"></property>
</bean>
</beans>

Car.java

package com.hy.spring.beans.cycle;

public class Car {
public Car(){
System.out.println("Car's Constructor");
}
private String brand;
public void setBrand(String brand) {
System.out.println("setBrand......");
this.brand = brand;
}
public void init(){
System.out.println("init.....");
}
public void destroy(){
System.out.println("destroy.....");
}

}

Main.java

package com.hy.spring.beans.cycle;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

public static void main(String[] args) {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans-cycle.xml");
Car car = (Car) ctx.getBean("car");
System.out.println(car);

//关闭IOC容器
ctx.close();

}

}

结果

xml 配置:

<!--
实现 BeanPostProcessor 接口,并具体提供
Object postProcessBeforeInitialization (Object bean, String beanName): init-method之前被调用
Object postProcessAfterInitialization (Object bean, String beanName): init-method之后被调用
的实现。
bean: bean实例本身。
beanName :IOC 容器配置的bean的名字。
返回值: 是实际上返回给用户的那个bean,注意:可以在以上两个方法中修改返回的bean,甚至返回一个新的bean
-->
<!-- 配置bean的后置处理器:不需要配置id,IOC容器自动识别是一个 BeanPostProcessor-->
<bean id="myBeanPostProcessor" class="com.hy.spring.beans.cycle.MyBeanPostProcessor">
</bean>

MyBeanPostProcessor.java

package com.hy.spring.beans.cycle;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

public class MyBeanPostProcessor implements BeanPostProcessor{

public Object postProcessAfterInitialization(Object bean, String beanName)
throws BeansException {
System.out.println("postProcessAfterInitialization:" + bean + ", " + beanName);
Car car = new Car();
car.setBrand("Ford");
return car;
}

public Object postProcessBeforeInitialization(Object bean, String beanName)
throws BeansException {
if("car".equals(beanName)){
//....
}

System.out.println("postProcessBeforeInitialization:" + bean + ", " + beanName);
return bean;
}

}

 Car.java

package com.hy.spring.beans.cycle;

public class Car {
public Car(){
System.out.println("Car's Constructor");
}
private String brand;
public void setBrand(String brand) {
System.out.println("setBrand......");
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 + "]";
}
}

结果:

最新文章

  1. C#的委托和事件(delegate)
  2. 使用Jconsole监控weblogic的配置方法
  3. TCP客户机-服务器
  4. Python学习笔记总结(二)函数和模块
  5. javascript外部ファイル
  6. GNU/Linux超级本ZaReason Ultralap 440体验
  7. 再次精读《javascript高级程序设计第3版》学习笔记(1)
  8. Android WebView 打印 Console Log
  9. python获取当前文件路径以及父文件路径
  10. OpenEXR-2.2.0在Win7 x64系统下的安装方法
  11. Zabbix 创建监控项目
  12. vue内引入jsPlumb流程控制器(一)
  13. First scrum meeting report - 151017
  14. &quot;重力锁屏&quot;Beta版使用说明
  15. 每天学一点easyui②
  16. secret
  17. JavaScript正则表达式_常用的正则
  18. 【bzoj4182】Shopping 树的点分治+dfs序+背包dp
  19. iptables详解(6):iptables扩展匹配条件之’–tcp-flags’
  20. Windows Server 2012十大实用快捷键组合

热门文章

  1. Codeforces Round #210 (Div. 1).B
  2. 如何利用jQuery检查浏览器是否是IE6-8
  3. 修改docker时区
  4. showslow / YSlow for PhantomJS/slimerjs(gecko)/phantomas
  5. MySQL小记
  6. appium入门基础知识
  7. PHP使用Mongodb
  8. 004-java类保存优化
  9. node.js---sails项目开发(4)---配置MongoDB数据库连接
  10. RS232串口通信