spring bean生命周期流程图:
其中包含了很多常用的接口,可以参考spring 常用接口;
下面写个例子证明下:
1.实现InitializingBean以及各个Aware接口
public class UserB implements InitializingBean , BeanFactoryAware , BeanNameAware, BeanClassLoaderAware , ApplicationContextAware, EnvironmentAware, ResourceLoaderAware, MessageSourceAware { private String name; private int age; private UserA usera; @Autowired public void setUsera(UserA usera) { this.usera = usera; System.out.println("====UserB属性注入usera"+usera); } public UserB() { System.out.println("====构造userB"); } public String getName() { return "程序猿"; } public int getAge() { return 19; } public void function(){ System.out.println("====userB"+usera.getName()); } @Override public String toString() { return "UserB{" + "name='" + getName() + ''' + ", age=" + getAge() + ", usera=" + usera + '}'; } @Override public void afterPropertiesSet() throws Exception { System.out.println("UserB===InitializingBean-afterPropertiesSet"); } @Override public void setBeanFactory(BeanFactory beanFactory) throws BeansException { System.out.println("=====UserB:setBeanFactory"); } @Override public void setBeanClassLoader(ClassLoader classLoader) { System.out.println("=====UserB:setBeanClassLoader"); } @Override public void setBeanName(String name) { System.out.println("=====UserB:setBeanName"); } @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { System.out.println("=====UserB:setApplicationContext"); } @Override public void setEnvironment(Environment environment) { System.out.println("=====UserB:setEnvironment"); } @Override public void setMessageSource(MessageSource messageSource) { System.out.println("=====UserB:setMessageSource"); } @Override public void setResourceLoader(ResourceLoader resourceLoader) { System.out.println("=====UserB:setResourceLoader"); }}
2.实现BeanPostProcessor, BeanFactoryPostProcessor , InstantiationAwareBeanPostProcessor
@Componentpublic class UserPostProcessor implements BeanPostProcessor, BeanFactoryPostProcessor , BeanFactoryAware , InstantiationAwareBeanPostProcessor { BeanFactory beanFactory; @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { if(beanName.contains("UserB")) { Object bean1 = beanFactory.getBean(UserB.class.getName()); System.out.println("====BeanPostProcessor-postProcessBeforeInitialization:"+bean1); } return null; } @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { if(beanName.contains("UserB")) { Object bean1 = beanFactory.getBean(UserB.class.getName()); System.out.println("====BeanPostProcessor-postProcessAfterInitialization:"+bean1); } return null; } @Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { BeanDefinition beanDefinition = beanFactory.getBeanDefinition(UserB.class.getName()); System.out.println("=====BeanFactoryPostProcessor-postProcessBeanFactory:"+beanDefinition); } @Override public void setBeanFactory(BeanFactory beanFactory) throws BeansException { this.beanFactory = beanFactory; } @Override public Object postProcessBeforeInstantiation(Class> beanClass, String beanName) throws BeansException { if(beanName.contains("UserB")) { System.out.println("=====InstantiationAwareBeanPostProcessor-postProcessBeforeInstantiation:" + beanName); } return null; } @Override public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException { if(beanName.contains("UserB")) { Object bean1 = beanFactory.getBean(UserB.class.getName()); System.out.println("=====InstantiationAwareBeanPostProcessor-postProcessAfterInstantiation" + bean1); } return true; } @Override public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) throws BeansException { if(beanName.contains("UserB")) { Object bean1 = beanFactory.getBean(UserB.class.getName()); System.out.println("=====InstantiationAwareBeanPostProcessor-postProcessProperties:" + bean1); } return null; }}
3.启动类;
@SpringBootApplication@Import({UserB.class,UserA.class})public class DemoApplication {public static void main(String[] args) {ConfigurableApplicationContext run = SpringApplication.run(DemoApplication.class, args);}}
4.运行结果: