加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-05-25 09:44:58 所属栏目:Java 来源:互联网
导读:1.以ApplocationContext上下文单例模式装配bean为例,深入探讨bean的生命周期:(1).生命周期图:

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

(1).生命周期图:

(2).具体事例:

person类实现BeanNameAware,BeanFactoryAware接口

public class Person implements BeanNameAware,BeanFactoryAware{
  
  private String name;
  
  public Person(){
    System.out.println("调用构造器为属性值初始化");
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  @Override
  public void setBeanName(String arg0) {
    // TODO Auto-generated method stub
    System.out.println("获取beanName id值"+" "+arg0);
    
  }

  @Override
  public void setBeanFactory(BeanFactory arg0) throws BeansException {
    // TODO Auto-generated method stub
    System.out.println("获取BeanFactory" +" "+arg0);
    
  }
}

public class MyBeanPostProcessor implements BeanPostProcessor{

  @Override
  public Object postProcessAfterInitialization(Object arg0,String arg1) throws BeansException {
    // TODO Auto-generated method stub
    System.out.println("调用postProcessAfterInitialization");
    return arg0;
  }

  @Override
  public Object postProcessBeforeInitialization(Object arg0,String arg1) throws BeansException {
    // TODO Auto-generated method stub
    System.out.println("调用postProcessBeforeInitialization");
    return arg0;
  }

}

ApplicationContext.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的配置文件 -->
<bean id="person" class="org.jingdong.bean.life.Person">
<property name="name" value="grl"></property>
</bean>

<bean id="myBeanPostProcessor" class="org.jingdong.bean.life.MyBeanPostProcessor"></bean>
</beans>

Main.java

public class Main {
  public static void main(String[] args) {
    // 创建IOC容器
    ApplicationContext ac = new ClassPathXmlApplicationContext("org/jingdong/bean/life/applicationContext.xml");
    //从容器中获取bean实例
    Person person = (Person) ac.getBean("person");
    //使用bean
    System.out.println(person.getName());
  }
}

2.以Spring Factory装配bean为例:

(1).生命周期图:

  

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

您可能感兴趣的文章:

  • Spring学习笔记之bean生命周期
  • 浅谈Spring bean 生命周期验证
  • Spring配置使用之Bean生命周期详解
  • 详解Spring中bean生命周期回调方法
  • 详解Spring中Bean的生命周期和作用域及实现方式
  • Spring Bean的生命周期详细介绍
  • 浅谈Spring中Bean的作用域、生命周期

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读