源码框架-Spring IOC-05-Bean的生命周期

一、什么是bean的生命周期

bean的创建----->初始化----->销毁方法

由容器管理Bean的生命周期,我们可以通过自己指定bean的初始化方法和bean的销毁方法。

单实例bean:容器启动的时候,bean的对象就创建了,而且容器销毁的时候,也会调用Bean的销毁方法

多实例bean:容器启动的时候,获取bean的时候才会被创建,而且bean的销毁不受 IOC容器的管理.


public class BertBean {
    private int id;
    public BertBean(int id) {
        this.id = id;
        System.out.println("创建构造器BertBean" + id);
    }

    // 初始方法
    public void init() {
        System.out.println("init..." + id);
    }

    // 销毁方法
    public void destroy() {
        System.out.println("destroy..." + id)
    }
}

@Configuration
public class LifeCycleConfig {
    // 指定了bean的生命周期的初始化方法和销毁方法.
    @Bean(initMethod = "init", destroyMethod = "destroy")
    public BertBean bertBean() {
        return new BertBean(1);
    }

    @Bean(initMethod = "init", destroyMethod = "destroy")
    @Scope("prototype")
    public BertBean bertBean1() {
        return new BertBean(2);
    }

    public static void main(String[] args) {
        AnnotationConfigApplicationContext ctx = new 
                AnnotationConfigApplicationContext(LifeCycleConfig.class);
        ctx.getBean("bertBean1");
        ctx.destroy();
    }
}

运行结果:

创建构造器BertBean1

init...1

创建构造器BertBean2

init...2

destroy...1

 

二、通过 InitializingBean和DisposableBean 的二个接口实现bean的初始化以及销毁方法

@Component
public class BertVo implements InitializingBean, DisposableBean {
    public BertVo() {
        System.out.println("BertVo的构造方法");
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("DisposableBean的destroy()方法 ");
    }

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


@ComponentScan(basePackages = {"com.bert.lifecycle.vo"})
public class LifeCycleConfig1 {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext ctx = new 
                AnnotationConfigApplicationContext(LifeCycleConfig1.class);
        ctx.destroy();
    }
}

运行结果:

BertVo的构造方法

InitializingBean的 afterPropertiesSet方法

DisposableBean的destroy()方法

 

三、通过JSR250规范 提供的注解@PostConstruct 和@ProDestory标注的方法

@Component
public class BertModel {
    public BertModel() {
        System.out.println("BertModel 的构造方法")
    }

    @PostConstruct
    public void init() {
        System.out.println("BertModel 的PostConstruct标志的方法");
    }

    @PreDestroy
    public void destory() {
        System.out.println("BertModel 的PreDestory标注的方法");
    }
}

@ComponentScan(basePackages = {"com.bert.lifecycle.model"})
public class LifeCycleConfig2 {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext ctx = new 
                AnnotationConfigApplicationContext(LifeCycleConfig2.class);
        ctx.destroy();
    }
}

运行结果:

BertModel 的构造方法

BertModel 的PostConstruct标志的方法

BertModel 的PreDestory标注的方法

 

四、通过Spring的BeanPostProcessor的 bean的后置处理器会拦截所有bean创建过程

postProcessBeforeInitialization 在init方法之前调用

postProcessAfterInitialization 在init方法之后调用

@Component
public class BertBeanPostProcessor implements BeanPostProcessor {

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("BertBeanPostProcessor.postProcessBeforeInitialization:" + beanName);
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("BertBeanPostProcessor.postProcessAfterInitialization:" + beanName);
        return bean;
    }
}

@ComponentScan(basePackages = {"com.bert.lifecycle.process"})
public class LifeCycleConfig3 {

    @Bean(initMethod = "init", destroyMethod = "destroy")
    public BertBean bertBean() {
        return new BertBean(1);
    }

    @Bean(initMethod = "init", destroyMethod = "destroy")
    @Scope("prototype")
    public BertBean bertBean1() {
        return new BertBean(2);
    }

    public static void main(String[] args) {
        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(LifeCycleConfig3.class);
        ctx.getBean("bertBean1");
        ctx.destroy();
    }
}

 

运行结果:

BertBeanPostProcessor.postProcessBeforeInitialization:lifeCycleConfig3
BertBeanPostProcessor.postProcessAfterInitialization:lifeCycleConfig3
创建构造器BertBean1
BertBeanPostProcessor.postProcessBeforeInitialization:bertBean
init...1
BertBeanPostProcessor.postProcessAfterInitialization:bertBean
创建构造器BertBean2
BertBeanPostProcessor.postProcessBeforeInitialization:bertBean1
init...2
BertBeanPostProcessor.postProcessAfterInitialization:bertBean1
destroy...1

 

 

五、通过@Value +@PropertySource来给组件赋值

public class BertValue {
    // 通过普通的方式
    @Value("测试")
    public String firstName;
    // spel方式来赋值, 23
    @Value("#{15+5+3}")
    public Integer age;
    // 通过读取外部配置文件的值
    @Value("${bert.name}")
    public String lastName;
}

@Configuration
@PropertySource(value = {"classpath:bert.properties"}) //指定外部文件的位置
public class BertValueConfig {
    @Bean
    public BertValue bertValue() {
        return new BertValue();
    }

    public static void main(String[] args) {
        AnnotationConfigApplicationContext ctx = new 
                AnnotationConfigApplicationContext(BertValueConfig.class);
        BertValue bertValue = (BertValue) ctx.getBean("bertValue");
        System.out.println(bertValue.firstName);
        System.out.println(bertValue.age);
        System.out.println(bertValue.lastName);
    }
}

bert.properties文件:

bert.name=test

运行结果:

测试

23

test

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值