zoukankan      html  css  js  c++  java
  • spring 中bean生命周期

    从头开始学习spring(一)

    传统编程中,依赖关系比较多的情况下,导致维护成本直线上升,spring 采用Ioc对bean进行管理,减少了开发人员的工作量

    正确理解spring bean 的生命周期非常重要

    package com.study.spring.beans;
    
    import org.springframework.beans.BeansException;
    import org.springframework.beans.factory.BeanFactory;
    import org.springframework.beans.factory.BeanFactoryAware;
    import org.springframework.beans.factory.BeanNameAware;
    import org.springframework.beans.factory.DisposableBean;
    import org.springframework.beans.factory.InitializingBean;
    import org.springframework.beans.factory.config.BeanPostProcessor;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.ApplicationContextAware;
    
    
    public class MyBeanLifeCycle implements BeanNameAware, BeanFactoryAware,
        ApplicationContextAware, BeanPostProcessor, InitializingBean, DisposableBean {
    
        @Override
        public void setBeanName(String s) {
            System.out.println("bean id : " + s);
        }
    
        @Override
        public void destroy() throws Exception {
            System.out.println("destroy");
        }
    
        @Override
        public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
            System.out.println("beanFactory : " + beanFactory.toString());
        }
    
        @Override
        public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
            System.out.println("applicationContext : " + applicationContext.getApplicationName());
        }
    
        @Override
        public Object postProcessBeforeInitialization(Object o, String s) throws BeansException {
            System.out.println("postProcessBeforeInitialization : " + s);
            return o;
        }
    
        @Override
        public Object postProcessAfterInitialization(Object o, String s) throws BeansException {
            System.out.println("postProcessAfterInitialization : " + s);
            return o;
        }
    
        @Override
        public void afterPropertiesSet() throws Exception {
            System.out.println("afterPropertiesSet");
        }
    }

    applicationContext.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns="http://www.springframework.org/schema/beans"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans.xsd">
    
    
      <bean class="com.study.spring.service.impl.HelloServiceImpl" id="helloService"/>
    
      <bean id="myBean" class="com.study.spring.beans.MyBeanLifeCycle"/>
    
    </beans>
    

      

  • 相关阅读:
    XAML学习笔记之Layout(五)——ViewBox
    XAML学习笔记——Layout(三)
    XAML学习笔记——Layout(二)
    XAML学习笔记——Layout(一)
    从0开始搭建SQL Server 2012 AlwaysOn 第三篇(安装数据,配置AlwaysOn)
    从0开始搭建SQL Server 2012 AlwaysOn 第二篇(配置故障转移集群)
    从0开始搭建SQL Server 2012 AlwaysOn 第一篇(AD域与DNS)
    Sql Server 2012 事务复制遇到的问题及解决方式
    Sql Server 2008R2升级 Sql Server 2012 问题
    第一次ACM
  • 原文地址:https://www.cnblogs.com/liuzyw/p/7594651.html
Copyright © 2011-2022 走看看