深入理解春天中bean的生命周期介绍

  

1。以ApplocationContext上下文单例模式装配bean为例,深入探讨bean的生命周期:
  

  

(1)。生命周期图:

  

深入理解春天中bean的生命周期介绍

  

(2)。具体事例:

  

人类实现BeanNameAware, BeanFactoryAware接口

        公共类人实现BeanNameAware BeanFactoryAware {      私人字符串名称;      公众人物(){   System.out.println(“调用构造器为属性值初始化”);   }      公共字符串getName () {   返回名称;   }      公共空间setName(字符串名称){   this.name=名称;   }      @Override   公共空间setBeanName(字符串arg0) {//TODO自动生成方法存根   system . out。println(“获取beanName id值" + " " + arg0);      }      @Override   公共空间setBeanFactory (BeanFactory arg0)抛出BeansException {//TODO自动生成方法存根   system . out。println(“获取BeanFactory " + " " + arg0);      }   }      之前            公共类MyBeanPostProcessor实现BeanPostProcessor {      @Override   公共对象postProcessAfterInitialization(对象arg0,字符串__arg1)抛出BeansException {//TODO自动生成方法存根   System.out.println(“调用postProcessAfterInitialization”);   返回arg0;   }      @Override   公共对象postProcessBeforeInitialization(对象arg0,字符串__arg1)抛出BeansException {//TODO自动生成方法存根   System.out.println(“调用postProcessBeforeInitialization”);   返回arg0;   }      }      之前      

ApplicationContext。xml配置文件:
  

        & lt; & # 63; xml version=" 1.0 " encoding=" utf - 8 " & # 63;比;   & lt;豆类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”比;   & lt; !——bean的配置文件——比;   & lt; bean id=叭恕崩?皁rg.jingdong.bean.life.Person”比;   & lt;属性名="名称" value=" https://www.yisu.com/zixun/grl "祝辞& lt;/property>   & lt;/bean>      & lt; bean id=癿yBeanPostProcessor”类=皁rg.jingdong.bean.life.MyBeanPostProcessor祝辞& lt;/bean>   & lt;/beans>      之前      

主要。java
  

        公开课主要{   公共静态void main (String [] args) {//创建国际奥委会容器   ApplicationContext ac=new ClassPathXmlApplicationContext ("org/jingdong/bean/life/applicationContext.xml”);//从容器中获取豆实例   人的人=(人)ac.getBean(“人”);//使用bean   System.out.println (person.getName ());   }   }   之前      

2。以弹簧工厂装配bean为例:

  

(1)。生命周期图:

  

深入理解春天中bean的生命周期介绍

  

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

深入理解春天中bean的生命周期介绍