|
| 1 | +package com.debug.beanlifecycle; |
| 2 | + |
| 3 | +import org.springframework.beans.BeansException; |
| 4 | +import org.springframework.beans.factory.*; |
| 5 | +import org.springframework.context.*; |
| 6 | +import org.springframework.core.env.Environment; |
| 7 | +import org.springframework.core.io.Resource; |
| 8 | +import org.springframework.core.io.ResourceLoader; |
| 9 | + |
| 10 | +import java.util.Arrays; |
| 11 | + |
| 12 | +/** |
| 13 | + * @author: Shawn Chen |
| 14 | + * @date: 2018/6/6 |
| 15 | + * @description:测试主类 |
| 16 | + */ |
| 17 | +public class BeanLifeCycle implements InitializingBean, DisposableBean, ApplicationContextAware, ApplicationEventPublisherAware, BeanClassLoaderAware, BeanFactoryAware, BeanNameAware, EnvironmentAware, ResourceLoaderAware |
| 18 | +{ |
| 19 | + |
| 20 | + private String name; |
| 21 | + |
| 22 | + public String getName() |
| 23 | + { |
| 24 | + return name; |
| 25 | + } |
| 26 | + |
| 27 | + public void setName(String name) |
| 28 | + { |
| 29 | + System.out.println("6.setName(name),属性注入后调用,此时name=" + name); |
| 30 | + System.out.println(); |
| 31 | + this.name = name; |
| 32 | + } |
| 33 | + |
| 34 | + public BeanLifeCycle() |
| 35 | + { |
| 36 | + System.out.println("3.调用BeanLifeCycle无参构造函数,进行实例化"); |
| 37 | + System.out.println(); |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public void afterPropertiesSet() throws Exception |
| 42 | + { |
| 43 | + //processBeforeInitialization(BeanPostProcessor)后调用 |
| 44 | + System.out.println("11.调用InitializingBean的afterPropertiesSet(),processBeforeInitialization之后,配置的initMethod之前调用"); |
| 45 | + System.out.println(); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public void destroy() throws Exception |
| 50 | + { |
| 51 | + System.out.println("16.执行DisposableBean接口的destroy方法"); |
| 52 | + System.out.println(); |
| 53 | + } |
| 54 | + |
| 55 | + |
| 56 | + /** |
| 57 | + * 通过<bean>的destroy-method属性指定的销毁方法 |
| 58 | + * |
| 59 | + * @throws Exception |
| 60 | + */ |
| 61 | + public void destroyMethod() throws Exception |
| 62 | + { |
| 63 | + System.out.println("17.执行配置的destroy-method()方法"); |
| 64 | + System.out.println(); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * 通过<bean>的init-method属性指定的初始化方法 |
| 69 | + * |
| 70 | + * @throws Exception |
| 71 | + */ |
| 72 | + public void initMethod() throws Exception |
| 73 | + { |
| 74 | + System.out.println("12.BeanLifyCycle执行配置的init-method()"); |
| 75 | + System.out.println(); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public void setBeanClassLoader(ClassLoader classLoader) |
| 80 | + { |
| 81 | + System.out.println("执行setBeanClassLoader,ClassLoader Name = " + classLoader.getClass().getName()); |
| 82 | + System.out.println(); |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public void setBeanFactory(BeanFactory beanFactory) throws BeansException |
| 87 | + { |
| 88 | + //setBeanName 后调用 |
| 89 | + System.out.println("8.调用BeanFactoryAware的setBeanFactory()方法,setBeanName后调用,BeanLifeCycle bean singleton=" + beanFactory.isSingleton("beanlifecycle")); |
| 90 | + System.out.println(); |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public void setBeanName(String name) |
| 95 | + { |
| 96 | + System.out.println("7.调用BeanNameAware的setBeanName()方法,设置Bean名字:: Bean Name defined in context=" + name); |
| 97 | + System.out.println(); |
| 98 | + } |
| 99 | + |
| 100 | + |
| 101 | + @Override |
| 102 | + public void setEnvironment(Environment environment) |
| 103 | + { |
| 104 | + System.out.println("执行setEnvironment"); |
| 105 | + System.out.println(); |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public void setResourceLoader(ResourceLoader resourceLoader) |
| 110 | + { |
| 111 | + |
| 112 | + Resource resource = resourceLoader.getResource("classpath:beanlifecycle.xml"); |
| 113 | + System.out.println("执行setResourceLoader:: Resource File Name=" + resource.getFilename()); |
| 114 | + System.out.println(); |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) |
| 119 | + { |
| 120 | + System.out.println("执行setApplicationEventPublisher"); |
| 121 | + System.out.println(); |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException |
| 126 | + { |
| 127 | + System.out.println("执行setApplicationContext:: Bean Definition Names=" + Arrays.toString(applicationContext.getBeanDefinitionNames())); |
| 128 | + System.out.println(); |
| 129 | + } |
| 130 | + |
| 131 | + |
| 132 | +} |
0 commit comments