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

    bean的生命周期如图所示:

    下面通过代码示例说明:

    bean:

    package com.beanlifecycle;
    
    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.context.ApplicationContext;
    import org.springframework.context.ApplicationContextAware;
    
    public class Person
            implements BeanNameAware, BeanFactoryAware, ApplicationContextAware, InitializingBean, DisposableBean {
    
        private String name;
    
        public Person() {
            System.out.println("*********");
            System.out.println("调用构造方法");
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        @Override
        public String toString() {
            return "Person [name=" + name + "]";
        }
    
        // BeanNameAware的接口方法
        @Override
        public void setBeanName(String name) {
            System.out.println("*********");
            System.out.println("BeanNameAware的接口方法setBeanName");
        }
    
        // BeanFactoryAware的接口方法
        @Override
        public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
            System.out.println("*********");
            System.out.println("BeanFactoryAware的接口方法setBeanFactory");
        }
    
        // ApplicationContextAware的接口方法
        @Override
        public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
            System.out.println("*********");
            System.out.println("ApplicationContextAware的接口方法setApplicationContext");
        }
    
        // InitializingBean的接口方法
        @Override
        public void afterPropertiesSet() throws Exception {
            System.out.println("*********");
            System.out.println("InitializingBean的接口方法afterPropertiesSet");
        }
    
        // 配置文件配置的init-method
        private void initMethod() {
            System.out.println("*********");
            System.out.println("配置文件配置的init-method");
        }
    
        // DisposableBean的接口方法
        @Override
        public void destroy() throws Exception {
            System.out.println("*********");
            System.out.println("DisposableBean的接口方法destroy");
        }
    
        // 配置文件配置的destroy-method
        private void destroyMethod() {
            System.out.println("*********");
            System.out.println("配置文件配置的destroy-method");
        }
    
    }

    CustomInstantiationAwareBeanPostProcessor:

    package com.beanlifecycle;
    
    import java.beans.PropertyDescriptor;
    
    import org.springframework.beans.BeansException;
    import org.springframework.beans.PropertyValues;
    import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter;
    
    public class CustomInstantiationAwareBeanPostProcessor extends InstantiationAwareBeanPostProcessorAdapter {
    
        // 实例化bean之前调用此方法
        @Override
        public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
            System.out.println("*********");
            System.out.println("实例化bean之前调用此方法:postProcessBeforeInstantiation");
            return super.postProcessBeforeInstantiation(beanClass, beanName);
        }
    
        // 实例化bean之后调用此方法
        @Override
        public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
            System.out.println("*********");
            System.out.println("实例化bean之后调用此方法:postProcessAfterInstantiation");
            return super.postProcessAfterInstantiation(bean, beanName);
        }
    
        // 设置某个属性值时调用
        @Override
        public PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean,
                String beanName) throws BeansException {
            // TODO Auto-generated method stub
            System.out.println("*********");
            System.out.println("设置某个属性值时调用:postProcessPropertyValues:" + pvs);
            return super.postProcessPropertyValues(pvs, pds, bean, beanName);
        }
    
    }

    CustomBeanPostProcessor:

    package com.beanlifecycle;
    
    import org.springframework.beans.BeansException;
    import org.springframework.beans.factory.config.BeanPostProcessor;
    
    public class CustomBeanPostProcessor implements BeanPostProcessor {
    
        // 初始化bean之前调用此方法
        @Override
        public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
            System.out.println("*********");
            System.out.println("初始化bean之前调用此方法:postProcessBeforeInitialization");
            return BeanPostProcessor.super.postProcessBeforeInitialization(bean, beanName);
        }
    
        // 初始化bean之后调用此方法
        @Override
        public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
            System.out.println("*********");
            System.out.println("初始化bean之后调用此方法:postProcessAfterInitialization");
            return BeanPostProcessor.super.postProcessAfterInitialization(bean, beanName);
        }
    
    }

    配置文件:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context-2.5.xsd  
         http://www.springframework.org/schema/beans
         https://www.springframework.org/schema/beans/spring-beans.xsd">
        <bean class="com.beanlifecycle.Person" scope="singleton"
            init-method="initMethod" destroy-method="destroyMethod">
            <property name="name" value="tim"></property>
        </bean>
        <bean class="com.beanlifecycle.CustomInstantiationAwareBeanPostProcessor"></bean>
        <bean class="com.beanlifecycle.CustomBeanPostProcessor"></bean>
    </beans>

    测试类:

    package com.beanlifecycle;
    
    import org.springframework.beans.BeansException;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.AbstractApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import com.test.database.JdbcPooledService;
    import com.test.database.JdbcService;
    import com.test.database.mybatis.TestDao;
    import com.test.properties.PropertiesDemo;
    
    public class Main {
    
        public static void main(String[] args) throws BeansException, Exception {
            AbstractApplicationContext context = new ClassPathXmlApplicationContext("spring-context-beanlifecycle.xml");
            context.getBean(Person.class);
            Thread.sleep(5000);
            context.close();
    
        }
    }

    测试结果:

    *********
    实例化bean之前调用此方法:postProcessBeforeInstantiation
    *********
    调用构造方法
    *********
    实例化bean之后调用此方法:postProcessAfterInstantiation
    *********
    设置某个属性值时调用:postProcessPropertyValues:PropertyValues: length=1; bean property 'name'
    *********
    BeanNameAware的接口方法setBeanName
    *********
    BeanFactoryAware的接口方法setBeanFactory
    *********
    ApplicationContextAware的接口方法setApplicationContext
    *********
    初始化bean之前调用此方法:postProcessBeforeInitialization
    *********
    InitializingBean的接口方法afterPropertiesSet
    *********
    配置文件配置的init-method
    *********
    初始化bean之后调用此方法:postProcessAfterInitialization
    *********
    DisposableBean的接口方法destroy
    *********
    配置文件配置的destroy-method
  • 相关阅读:
    预备知识
    开场白
    H.264 / MPEG-4 Part 10 White Paper-翻译
    H.264简介
    batchGetAnchorLevel(dubbo接口)
    【Python022--递归】
    【python021-函数lambda表达式】
    【Python020--内嵌函数和闭包】
    【Python019--函数与过程】
    【python018--函数参数】
  • 原文地址:https://www.cnblogs.com/silenceshining/p/12709523.html
Copyright © 2011-2022 走看看