Spring Bean 的作用范围及生命周期

scope 定义 Bean 的作用范围

  • singleton(默认值):单例模式的 bean 对象生命周期与容器相同
    对象出生:当创建容器时,对象就被创建了
    对象活着:只要容器在,对象一直活着
    对象死亡:当销毁容器时,对象就被销毁了
  • prototype:多例模式的 bean 对象,spring 框架只负责创建,不负责销毁
    对象出生:当使用对象时,创建新的对象实例
    对象活着:只要对象在使用中,就一直活着
    对象死亡:当对象⻓时间不用时,被 Java 的垃圾回收器回收了

Spring Bean 的生命周期

在这里插入图片描述

测试一下效果

@Component
public class MyBeanPostProcessor implements BeanPostProcessor {

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if ("lazyResult".equalsIgnoreCase(beanName)) {
            System.out.println("MyBeanPostProcessor before 方法拦截处理lazyResult");
        }
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if ("lazyResult".equalsIgnoreCase(beanName)) {
            System.out.println("MyBeanPostProcessor after 方法拦截处理lazyResult");
        }
        return bean;
    }
}
public class Result implements BeanNameAware, BeanFactoryAware, ApplicationContextAware, InitializingBean, DisposableBean {

    private String status;
    private String message;

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    @Override
    public String toString() {
        return "Result{" +
                "status='" + status + '\'' +
                ", message='" + message + '\'' +
                '}';
    }

    public void initMethod() {
        System.out.println("initMethod........");
    }

    @PostConstruct
    public void postConstruct() {
        System.out.println("postConstruct........");
    }

    @Override
    public void setBeanName(String name) {
        System.out.println("注册我成为bean时定义的id:" + name);
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        System.out.println("管理我的beanFactory为" + beanFactory);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        System.out.println("高级容器接口ApplicationContext:" + applicationContext);
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("afterPropertiesSet........");
    }

    @PreDestroy
    public void PreDestroy(){
        System.out.println("PreDestroy......");
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("destroy......");
    }
}
@Test
public void test() {
    ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("application-context.xml");
    Result result = (Result) applicationContext.getBean("lazyResult");
    System.out.println(result);

    applicationContext.close();
}

输出:

  • 注册我成为bean时定义的id:lazyResult
  • 管理我的beanFactory为org.springframework.beans.factory.support.DefaultListableBeanFactory@1d9b7cce: defining beans [jdbcAccountDaoImpl,transactionProxyFactory,myBeanPostProcessor,transferServiceImpl,connectionUtils,transactionManager,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.event.internalEventListenerProcessor,org.springframework.context.event.internalEventListenerFactory,org.springframework.context.support.PropertySourcesPlaceholderConfigurer#0,dataSource,lazyResult]; root of factory hierarchy
  • 高级容器接口ApplicationContext:org.springframework.context.support.ClassPathXmlApplicationContext@5b464ce8, started on Mon May 30 11:26:32 CST 2022
  • MyBeanPostProcessor before 方法拦截处理lazyResult
  • postConstruct…
  • afterPropertiesSet…
  • initMethod…
  • MyBeanPostProcessor after 方法拦截处理lazyResult
  • Result{status=‘s’, message=‘123’}
  • PreDestroy…
  • destroy…
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值