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).生命周期图:

      

     

  • 相关阅读:
    wxpython(2)--按钮,位图按钮,滑动块,微调控制器
    获取网站title的脚本
    很酷的前端切图 | Sunny Day
    分析Java中的length和length()
    打破与被打破
    不错的东西: AutoMapper
    一个很不错的支持Ext JS 4的上传按钮
    [网络分析]WEBQQ3.0协议分析---good good study
    【翻译】提示18——如何决定ObjectContext的生命周期
    为什么要使用“var me=this”这样的写法
  • 原文地址:https://www.cnblogs.com/grl214/p/6623575.html
Copyright © 2011-2022 走看看