1. package com.smart;
  2.  
  3. import org.springframework.beans.BeansException;
  4. import org.springframework.beans.factory.*;
  5.  
  6. public class Car implements BeanFactoryAware, BeanNameAware, InitializingBean, DisposableBean{
  7. private String brand;
  8. private String color;
  9. private int maxSpeed;
  10.  
  11. private BeanFactory beanFactory;
  12. private String beanName;
  13.  
  14. //默认构造函数
  15. public Car(){System.out.println("调用Car()构造函数");}
  16.  
  17. //带参构造函数
  18. public Car(String brand, String color, int maxSpeed) {
  19. this.brand = brand;
  20. this.color = color;
  21. this.maxSpeed = maxSpeed;
  22. }
  23.  
  24. //未带参方法
  25. public void introduce() {
  26. System.out.println("brand:" + brand + ";color:" + color + ";maxSpeed:" + maxSpeed);
  27. }
  28.  
  29. public String getBrand() {
  30. return brand;
  31. }
  32.  
  33. public void setBrand(String brand) {
  34. System.out.println("调用setBrand()设置属性");
  35. this.brand = brand;
  36. }
  37.  
  38. public String getColor() {
  39. return color;
  40. }
  41.  
  42. public void setColor(String color) {
  43. this.color = color;
  44. }
  45.  
  46. public int getMaxSpeed() {
  47. return maxSpeed;
  48. }
  49.  
  50. public void setMaxSpeed(int maxSpeed) {
  51. this.maxSpeed = maxSpeed;
  52. }
  53.  
  54. // BeanFactoryAware接口方法
  55. public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
  56. System.out.println("调用BeanFactoryAware.setBeanFactory().");
  57. this.beanFactory = beanFactory;
  58. }
  59.  
  60. // BeanNameAware接口方法
  61. public void setBeanName(String s) {
  62. System.out.println("调用BeanNameAware.setBeanName().");
  63. this.beanName = s;
  64. }
  65.  
  66. // DisposableBean接口方法
  67. public void destroy() throws Exception {
  68. System.out.println("调用DisposableBean.destroy().");
  69. }
  70.  
  71. // InitializingBean接口方法
  72. public void afterPropertiesSet() throws Exception {
  73. System.out.println("调用InitialingBean.afterPropertiesSet().");
  74. }
  75.  
  76. // 通过<bean>的init-method属性指定的初始方法
  77. public void myInit() {
  78. System.out.println("调用init-method属性指定的myInit(), 将maxSpeed设置为240");
  79. this.maxSpeed = 240;
  80. }
  81.  
  82. //同过<bean>的destroy-method属性指定的销毁方法
  83. public void myDestroy() {
  84. System.out.println("调用destroy-method属性指定的myDestroy().");
  85. }
  86.  
  87. }

  1. package com.smart.beanfactory;
  2.  
  3. import org.springframework.beans.BeansException;
  4. import org.springframework.beans.PropertyValues;
  5. import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter;
  6.  
  7. import java.beans.PropertyDescriptor;
  8.  
  9. public class MyInstantiationAwareBeanPostProcessor extends InstantiationAwareBeanPostProcessorAdapter {
  10.  
  11. @Override
  12. // 1.接口方法,在实例化Bean前调用
  13. public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
  14. // 1-1. 仅对容器中的car Bean进行处理
  15. if ("car".equals(beanName)) {
  16. System.out.println("调用InstantiationAwareBeanPostProcessor.postProcessBeforeInstantiation().");
  17. }
  18. return null;
  19. }
  20.  
  21. @Override
  22. // 2.接口方法,在实例化Bean后调用
  23. public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
  24. if ("car".equals(beanName)) {
  25. System.out.println("调用InstantiationAwarePostProcessor.postProcessorAfterInstantiation().");
  26. }
  27. return true;
  28. }
  29.  
  30. @Override
  31. // 3.接口方法,在设置某个属性时调用
  32. public PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException {
  33. // 3-1. 仅对容器中的car Bean进行处理, 还可以同过pds入参进行过滤
  34. if ("car".equals(beanName)) {
  35. System.out.println("调用InstantiationAwarePostProcessor.postProcessPropertyValues().");
  36. }
  37. return pvs;
  38. }
  39. }

  1. package com.smart.beanfactory;
  2.  
  3. import com.smart.Car;
  4. import org.springframework.beans.BeansException;
  5. import org.springframework.beans.factory.config.BeanPostProcessor;
  6.  
  7. public class MyBeanPostProcessor implements BeanPostProcessor {
  8. public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
  9. if ("car".equals(beanName)) {
  10. Car car = (Car) bean;
  11. if (car.getColor() == null) {
  12. System.out.println("调用BeanPostProcessor.postProcessBeforeInitialization()," +
  13. "若color为空,设置为默认黑色");
  14. car.setColor("黑色");
  15. }
  16. }
  17. return bean;
  18. }
  19.  
  20. public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
  21. if ("car".equals(beanName)) {
  22. Car car = (Car) bean;
  23. if (car.getMaxSpeed() >= 200) {
  24. System.out.println("调用BeanPostProcessor.postProcessAfterInitialization()," +
  25. "将maxSpeed设置为200");
  26. car.setMaxSpeed(200);
  27. }
  28. }
  29. return bean;
  30. }
  31. }

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5.  
  6. <bean id="car" class="com.smart.Car"
  7. init-method="myInit"
  8. destroy-method="myDestroy"
  9. p:brand="红旗CA72"
  10. p:maxSpeed="200"/>
  11.  
  12. </beans>

  1. package com.smart.beanfactory;
  2.  
  3. import com.smart.Car;
  4. import org.springframework.beans.factory.BeanFactory;
  5. import org.springframework.beans.factory.config.ConfigurableBeanFactory;
  6. import org.springframework.beans.factory.support.DefaultListableBeanFactory;
  7. import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
  8. import org.springframework.context.ApplicationContext;
  9. import org.springframework.context.support.ClassPathXmlApplicationContext;
  10. import org.springframework.core.io.ClassPathResource;
  11. import org.springframework.core.io.Resource;
  12.  
  13. public class BeanLifeCycle {
  14. public static void lifeCycleInBeanFactory() {
  15. // 1. 装载配置文件并启动容器
  16. Resource res = new ClassPathResource("beans.xml");
  17. BeanFactory bf = new DefaultListableBeanFactory();
  18. XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader((DefaultListableBeanFactory) bf);
  19. reader.loadBeanDefinitions(res);
  20.  
  21. //后处理器的实际调用顺序与注册顺序无关,在具有多个后处理器的情况下,必须通过实现
  22. //org.springframework.core.Ordered接口来确定调用顺序
  23. // 2. 向容器中注册MyBeanPostProcessor后处理器
  24. ((ConfigurableBeanFactory) bf).addBeanPostProcessor(new MyBeanPostProcessor());
  25. // 3. 向容器中注册MyInstantiationAwareBeanPostProcessor后处理器
  26. ((ConfigurableBeanFactory) bf).addBeanPostProcessor(new MyInstantiationAwareBeanPostProcessor());
  27.  
  28. // 4. 第一次从容器中获得car, 将触发容器实例化该Bean,这将引发Bean生命周期方法的调用
  29. Car car1 = (Car) bf.getBean("car");
  30. car1.introduce();
  31. car1.setColor("红色");
  32.  
  33. // 5. 第二次从容器中获取car, 直接从缓存池中获取
  34. Car car2 = (Car) bf.getBean("car");
  35.  
  36. // 6. 查看car1和car2是否指向同一引用
  37. System.out.println("car1==car2: " + (car1 == car2));
  38.  
  39. // 7. 关闭容器
  40. ((DefaultListableBeanFactory) bf).destroySingleton("car");
  41.  
  42. }
  43.  
  44. public static void main(String[] args) {
  45. lifeCycleInBeanFactory();
  46. }
  47. }

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

  1. 《精通Spring4.x企业应用开发实战》第三章

    这一章节主要介绍SpringBoot的使用,也是学习的重点内容,之后就打算用SpringBoot来写后台,所以提前看一下还是很有必要的. 3.SpringBoot概况 3.1.1SpringBoot发 ...

  2. 《精通Spring4.X企业应用开发实战》读后感第四章(资源访问)

    package com.smart.resource; import org.springframework.core.io.ClassPathResource; import org.springf ...

  3. 《精通Spring4.X企业应用开发实战》读后感第七章(创建增强类)

  4. 《精通Spring4.X企业应用开发实战》读后感第七章(AOP基础知识、jdk动态代理,CGLib动态代理)

  5. 《精通Spring4.X企业应用开发实战》读后感第七章(AOP概念)

  6. 《精通Spring4.X企业应用开发实战》读后感第六章(容器事件)

  7. 《精通Spring4.X企业应用开发实战》读后感第六章(国际化)

  8. 《精通Spring4.X企业应用开发实战》读后感第六章(引用Bean的属性值)

  9. 《精通Spring4.X企业应用开发实战》读后感第六章(使用外部属性文件)

  10. 《精通Spring4.X企业应用开发实战》读后感第六章(属性编辑器)

随机推荐

  1. Circling Round Treasures CodeForces - 375C

    C. Circling Round Treasures time limit per test 1 second memory limit per test 256 megabytes input s ...

  2. 【BZOJ1604】[Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 Treap+并查集

    [BZOJ1604][Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 Description 了解奶牛们的人都知道,奶牛喜欢成群结队.观察约翰的N(1≤N≤100000) ...

  3. Python中属性

    属性定义的两种方式: 1.num1=property(GetNum,SetNum)   class Pro(): def __init__(self): self._num= def GetNum(s ...

  4. SecureCRT 会话设置项

    登陆动作------自动登陆仿真------两个颜色复选框都勾上模式------光标键模式(2个复选框)映射键------使用windows复制和粘贴热键外观------字符编码:UTF-8外观--- ...

  5. 【题解】CF559C C. Gerald and Giant Chess(容斥+格路问题)

    [题解]CF559C C. Gerald and Giant Chess(容斥+格路问题) 55336399 Practice: Winlere 559C - 22 GNU C++11 Accepte ...

  6. angularJs-HelloWorld

    AngularJS使用了不同的方法,它尝试去补足HTML本身在构建应用方面的缺陷.AngularJS通过使用我们称为标识符(directives)的结构,让浏览器能够识别新的语法. 1使用双大括号{{ ...

  7. PAT 甲级 1128. N Queens Puzzle (20) 【STL】

    题目链接 https://www.patest.cn/contests/pat-a-practise/1128 思路 可以 对每一个皇后 都判断一下 它的 行,列 ,左右对角线上 有没有皇后 深搜解决 ...

  8. maven GroupID和ArtifactID

    GroupID是项目组织唯一的标识符,实际对应JAVA的包的结构,是main目录里java的目录结构. ArtifactID就是项目的唯一的标识符,实际对应项目的名称,就是项目根目录的名称.一般Gro ...

  9. 【LeetCode】求众数

    给定一个大小为 n 的数组,找到其中的众数.众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素. 你可以假设数组是非空的,并且给定的数组总是存在众数. class Solution(object): ...

  10. matlab的数组

    1.定义:同一类型的元素的集合. 2.生成:用[]创建,元素之间用逗号或者空格隔开. 第一例: >>a=[1,2,3,4] a = 1 2 3 4 注意,取矩阵的某几列,是这样a(:,2: ...