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


    [Spring中bean的生命周期]


    bean的生命周期

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

     (1).生命周期图:

      (2).具体事例:

      person类实现BeanNameAware,BeanFactoryAware接口

     1 public class Person implements BeanNameAware ,BeanFactoryAware{
     2     
     3     private String name;
     4     
     5     public Person(){
     6         System.out.println("调用构造器为属性值初始化");
     7     }
     8 
     9     public String getName() {
    10         return name;
    11     }
    12 
    13     public void setName(String name) {
    14         this.name = name;
    15     }
    16 
    17     @Override
    18     public void setBeanName(String arg0) {
    19         // TODO Auto-generated method stub
    20         System.out.println("获取beanName id值"+"  "+arg0);
    21         
    22     }
    23 
    24     @Override
    25     public void setBeanFactory(BeanFactory arg0) throws BeansException {
    26         // TODO Auto-generated method stub
    27         System.out.println("获取BeanFactory" +"  "+arg0);
    28         
    29     }
    30     
    31 
    32 }
     1 public class MyBeanPostProcessor  implements BeanPostProcessor{
     2 
     3     @Override
     4     public Object postProcessAfterInitialization(Object arg0, String arg1) throws BeansException {
     5         // TODO Auto-generated method stub
     6         System.out.println("调用postProcessAfterInitialization");
     7         return arg0;
     8     }
     9 
    10     @Override
    11     public Object postProcessBeforeInitialization(Object arg0, String arg1) throws BeansException {
    12         // TODO Auto-generated method stub
    13         System.out.println("调用postProcessBeforeInitialization");
    14         return arg0;
    15     }
    16 
    17 }

    ApplicationContext.xml配置文件:

     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"
     4     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
     5 <!-- bean的配置文件 -->
     6 <bean id="person" class="org.jingdong.bean.life.Person">
     7 <property name="name" value="grl"></property>
     8 </bean>
     9 
    10 <bean id="myBeanPostProcessor" class="org.jingdong.bean.life.MyBeanPostProcessor"></bean>
    11 </beans>

    Main.java

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

     

    2.以Spring Factory装配bean为例:

      (1).生命周期图:

      

     

  • 相关阅读:
    C#演练—Windows应用程序—在windows窗体上动态创建上下文菜单
    C#演练—Windows应用程序—可视化继承
    C#演练—Windows应用程序—创建主从windows窗体
    小胖IT大讲堂之三 Hook实战(二) SQL Monitor山寨版
    小胖的2011总结之回忆篇
    Oracle安装示例数据库
    《领域驱动设计》读书笔记(一) 分离领域
    小胖IT大讲堂之一 .NET Reflector工具介绍
    10年前我不是小胖,也是个“诗人”
    忘记
  • 原文地址:https://www.cnblogs.com/grl214/p/6623575.html
Copyright © 2020-2023  润新知