zoukankan      html  css  js  c++  java
  • 深入理解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).生命周期图:

      

     

  • 相关阅读:
    hdu 4002 Find the maximum
    hdu 2837 坑题。
    hdu 3123
    zoj Treasure Hunt IV
    hdu 2053 Switch Game 水题一枚,鉴定完毕
    poj 1430 Binary Stirling Numbers
    hdu 3037 Saving Beans
    hdu 3944 dp?
    南阳oj 求N!的二进制表示最低位的1的位置(从右向左数)。
    fzu 2171 防守阵地 II
  • 原文地址:https://www.cnblogs.com/grl214/p/6623575.html
Copyright © 2011-2022 走看看