spring Bean生命周期

本文关于spring Bean生命周期,做了一个简单的Demo(Annotation风格),spring版本:5.2.0.RELEASE。

运行结果

在这里插入图片描述

build.gradle添加spring-context依赖

	// https://mvnrepository.com/artifact/org.springframework/spring-context
    compile group: 'org.springframework', name: 'spring-context', version: '5.2.0.RELEASE'

测试类BeanLifeCycleTest

package com.tbryant.spring;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

@Component
public class BeanLifeCycleTest implements BeanNameAware, BeanFactoryAware, ApplicationContextAware, InitializingBean, BeanPostProcessor, DisposableBean {
    @Override
    public void setBeanName(String name) {
        System.out.println("=====1=====调用BeanNameAware.setBeanName(name:" + name + ")");
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        System.out.println("=====2=====调用BeanFactoryAware.setBeanFactory(beanFactory:" + beanFactory + ")");
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        System.out.println("=====3=====调用ApplicationContextAware.setApplicationContext(applicationContext:" + applicationContext + ")");
    }

    @PostConstruct
    public void beanLifeCycleTestInit() {
        System.out.println("=====4=====@PostConstruct注解的方法beanLifeCycleTestInit()");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("=====5=====调用InitializingBean.afterPropertiesSet()");
    }

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("=====6=====调用BeanPostProcessor.postProcessBeforeInitialization(bean:" + bean + "beanName:" + beanName + ")");
        return null;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("=====7=====调用BeanPostProcessor.postProcessAfterInitialization(bean:" + bean + "beanName:" + beanName + ")");
        return null;
    }

    @PreDestroy
    public void beanLifeCycleTestDestroy() {
        System.out.println("=====9=====@PreDestroy注解的方法beanLifeCycleTestDestroy()");
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("=====10=====调用DisposableBean.destroy()");
    }
}

配置类AppConfig

package com.tbryant.spring;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan("com.tbryant.spring")
public class AppConfig {
}

启动类Application

package com.tbryant.spring;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Application {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(AppConfig.class);
        BeanLifeCycleTest beanLifeCycleTest = (BeanLifeCycleTest) annotationConfigApplicationContext.getBean("beanLifeCycleTest");
        System.out.println("=====8===== Use Bean =====8=====" + beanLifeCycleTest);
        annotationConfigApplicationContext.close();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值