Spring中Bean的七步生命周期详解-----Spring框架

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--    需要手动指定初始化方法和销毁方法-->
    <bean id="vip" class="com.powernode.spring6.bean.Vip"></bean>
    <bean id="user" class="com.powernode.spring6.bean.User" init-method="initBean" destroy-method="destroyBean">
        <property name="name" value="Jack"/>
    </bean>
<!--    第一步实例化,第二步赋值,第三步初始化,第四步使用,第五步销毁,七步就是在第三步初始化前后添加代码-->
<!--    Bean生命周期七步,第一步实例化,第二步赋值,第三步Bean后处理器的before方法,第四步初始化,第五步Bean后处理器的after方法,第六步使用,第七步销毁Bean-->
<!--    配置Bean后处理器,该处理器将作用于整个配置文件所有的Bean-->
    <bean class="com.powernode.spring6.bean.LogBeanPostProcessor"></bean>
</beans>

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 需要手动指定初始化方法和销毁方法-->
<bean id="vip" class="com.powernode.spring6.bean.Vip"></bean>
<bean id="user" class="com.powernode.spring6.bean.User" init-method="initBean" destroy-method="destroyBean">
<property name="name" value="Jack"/>
</bean>
<!-- 第一步实例化,第二步赋值,第三步初始化,第四步使用,第五步销毁,七步就是在第三步初始化前后添加代码-->
<!-- Bean生命周期七步,第一步实例化,第二步赋值,第三步Bean后处理器的before方法,第四步初始化,第五步Bean后处理器的after方法,第六步使用,第七步销毁Bean-->
<!-- 配置Bean后处理器,该处理器将作用于整个配置文件所有的Bean-->
<bean class="com.powernode.spring6.bean.LogBeanPostProcessor"></bean>
</beans>

 

package com.powernode.spring6.bean;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

public class LogBeanPostProcessor implements BeanPostProcessor
{
    private static final Logger logger = LoggerFactory.getLogger(LogBeanPostProcessor.class);
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        logger.info("执行了Bean后处理器的Before");
        return BeanPostProcessor.super.postProcessBeforeInitialization(bean, beanName);
    }
    //方法有两个参数,一个是刚刚创建的Bean对象,一个是Bean的名字
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        logger.info("执行了Bean后处理器的After");
        return BeanPostProcessor.super.postProcessAfterInitialization(bean, beanName);
    }
    //日志Bean后处理器,在初始化前和之后都记录日志
}

package com.powernode.spring6.bean;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

public class LogBeanPostProcessor implements BeanPostProcessor
{
private static final Logger logger = LoggerFactory.getLogger(LogBeanPostProcessor.class);
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
logger.info("执行了Bean后处理器的Before");
return BeanPostProcessor.super.postProcessBeforeInitialization(bean, beanName);
}
//方法有两个参数,一个是刚刚创建的Bean对象,一个是Bean的名字
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
logger.info("执行了Bean后处理器的After");
return BeanPostProcessor.super.postProcessAfterInitialization(bean, beanName);
}
//日志Bean后处理器,在初始化前和之后都记录日志
}

package com.powernode.spring6.bean;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class User
{
    private static final Logger logger = LoggerFactory.getLogger(User.class);
    private String name;
    public User()
    {
        logger.info("User的无参数构造方法,1");
    }
    //第一步实例化Bean,给Bean属性赋值(通过set方法)
    //初始化Bean(调用Bean的init方法,需要自己写自己配),使用Bean,销毁Bean(调用destroy方法,需要自己写自己配)
    public void setName(String name)
    {
        this.name = name;
        logger.info("给属性赋值,2");
    }
    public void initBean()
    {
        //需要自己写,自己配
        logger.info("初始化Bean,3");
    }
    public void destroyBean()
    {
        //需要自己写,自己配
        logger.info("销毁Bean,5");
    }
}

package com.powernode.spring6.bean;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class User
{
private static final Logger logger = LoggerFactory.getLogger(User.class);
private String name;
public User()
{
logger.info("User的无参数构造方法,1");
}
//第一步实例化Bean,给Bean属性赋值(通过set方法)
//初始化Bean(调用Bean的init方法,需要自己写自己配),使用Bean,销毁Bean(调用destroy方法,需要自己写自己配)
public void setName(String name)
{
this.name = name;
logger.info("给属性赋值,2");
}
public void initBean()
{
//需要自己写,自己配
logger.info("初始化Bean,3");
}
public void destroyBean()
{
//需要自己写,自己配
logger.info("销毁Bean,5");
}
}

package com.powernode.spring6.bean;

public class Vip {
}

package com.powernode.spring6.bean;

public class Vip {
}

package com.powernode.spring6.test;

import com.powernode.spring6.bean.User;
import com.powernode.spring6.bean.Vip;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test
{
    private static final Logger logger = LoggerFactory.getLogger(Test.class);
    @org.junit.Test
    public void TestLifeCycleFive()
    {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
        User user = applicationContext.getBean("user",User.class);
        logger.info("使用Bean,4");
        ClassPathXmlApplicationContext context = (ClassPathXmlApplicationContext) applicationContext;
        context.getBean("vip", Vip.class);
        context.close();
    }
}

package com.powernode.spring6.test;

import com.powernode.spring6.bean.User;
import com.powernode.spring6.bean.Vip;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test
{
private static final Logger logger = LoggerFactory.getLogger(Test.class);
@org.junit.Test
public void TestLifeCycleFive()
{
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
User user = applicationContext.getBean("user",User.class);
logger.info("使用Bean,4");
ClassPathXmlApplicationContext context = (ClassPathXmlApplicationContext) applicationContext;
context.getBean("vip", Vip.class);
context.close();
}
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Bean生命周期可以分为以下几个阶段: 1. 实例化(Instantiation):在该阶段,Spring容器根据配置信息或注解创建Bean的实例。这可以通过构造函数或工厂方法进行实例化。 2. 属性赋值(Population):在该阶段,Spring容器为Bean的属性赋值。这可以通过依赖注入(DI)或属性注入来完成。依赖注入是指通过构造函数、Setter方法或字段注入其他Bean的引用。 3. 初始化(Initialization):在该阶段,Spring容器对Bean进行初始化。这可以通过实现InitializingBean接口或在配置文件指定init-method方法来实现。在这个阶段,可以执行一些初始化操作,例如建立数据库连接、加载资源等。 4. 使用(In Use):在该阶段,Bean可以被正常使用。它可以被其他Bean引用,也可以执行一些业务逻辑。 5. 销毁(Destruction):在该阶段,Spring容器销毁Bean实例。这可以通过实现DisposableBean接口或在配置文件指定destroy-method方法来实现。在这个阶段,可以执行一些清理操作,例如关闭数据库连接、释放资源等。 需要注意的是,Spring容器并不管理所有Bean生命周期。通常情况下,Spring只管理由容器实例化的单例Bean生命周期。原型Bean生命周期由客户端负责管理。 另外,还可以通过Bean后置处理器(BeanPostProcessor)来对Bean生命周期进行自定义扩展。Bean后置处理器可以在Bean实例化、属性赋值、初始化等阶段进行一些额外的处理操作。例如,可以在Bean初始化之前进行一些自定义操作,或者在Bean销毁之前进行一些清理操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值