spring 之 bean 创建到销毁

bean的生命周期,创建、初始化、销毁;

1)、自定义初始化和销毁方法:
	通过@Bean指定initMethod和destroyMethod方法
2)、通过实现InitializingBean,DisposableBean接口
3)、通过JSR250方式 @PostConstruct和@PreDestroy注解作用在方法上
4)、BeanPostProcessor  bean的后置处理器;
		在bean初始化的前后进行处理工作
		postProcessBeforeInitialization  :在初始化之前
		postProcessAfterInitialization :在初始化之后

第一种方式:自定义初始化和销毁方法
定义一个Cool类

package com.bean;

public class Cool {
	public Cool(){
		System.out.println("创建Cool对象。。。。");
	}
	public void init(){
		System.out.println("调用init方法开始初始化。。。");
	}
	
	public void detroy(){
		System.out.println("调用detroy方法销毁。。。");
	}
}

注解类

package com.config;

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

import com.bean.Cool;

@Configuration   //告诉spring这是一个配置类
public class MianConfig2 {
	@Bean(initMethod="init",destroyMethod="detroy")
	public Cool  getCool(){
		return new Cool();
	}
}

测试类

package com;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.config.MianConfig2;

public class DemoTest {
	@Test
	public void Test02() {
		System.out.println("准备加载ioc容器。。。");
		AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MianConfig2.class);
		System.out.println("初始化ioc容器完成。。。");
		//关闭容器
		applicationContext.close();
	}
	
}

在这里插入图片描述

总结:bean的生命周期,在单实例中创建容器的时候就会调用方法创建对象(多实例是获取的时候创建对象),
绑定自定义的初始化和销毁方法需要使用 **@Bean(initMethod="init",destroyMethod="detroy")**
最后关闭容器就调用了销毁方法**applicationContext.close();**

创建:单实例是容器启动就创建
	  多实例是获取的时候才创建
初始化:对象创建完成,赋值好,调用初始化方法
销毁:单实例是容器关闭时销毁
    多实例容器不会管理这个bean,也不会调用销毁方法

第二种方式:实现InitializingBean,DisposableBean接口

package com.bean;

import javax.naming.spi.DirStateFactory;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;
@Component
public class Red implements InitializingBean,DisposableBean{

	public void destroy() throws Exception {
		// TODO Auto-generated method stub
		System.out.println("销毁方法destroy()。。。。");
	}

	public void afterPropertiesSet() throws Exception {
		// TODO Auto-generated method stub
		System.out.println("初始化方法afterPropertiesSet()。。。。");	
	}
}

在注解类中加入扫描注解:@ComponentScan(“com.bean”)
在这里插入图片描述
总结:在容器启动时候,afterPropertiesSet()方法被调用;在容器关闭之前,destroy()方法被调用
第三种方式:@PostConstruct和@PreDestroy注解

// 在LifeCycleBean中添加以下即可
@PostConstruct
public void postConstruct() {
    System.out.println("annotation init ...");
}
 
@PreDestroy
public void preDestroy() {
    System.out.println("annotation destroy...");
}

在使用的时候记得把类加入到容器,@Component

https://blog.csdn.net/qq_26323323/article/details/89790006

第四种方式:bean的后置处理


package com.bean;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;
@Component
public class Cat implements BeanPostProcessor {
	//初始化之前
	public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
		// TODO Auto-generated method stub
		System.out.println("postProcessBeforeInitialization方法被调用"+beanName+"==>"+bean);
		return bean;
	}
	//初始化之后
	public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
		// TODO Auto-generated method stub
		System.out.println("postProcessAfterInitialization方法被调用"+beanName+"==>"+bean);
		return bean;
	}
	
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值