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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值