Spring中自定义Bean

本文深入探讨Spring框架中Bean的生命周期管理,包括初始化和销毁过程,通过实现特定接口或使用JSR-250注解来定制Bean的行为。同时介绍了如何通过ApplicationContextAware获取Spring容器上下文。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言:此文章是本人在阅读Spring Framework官方文档Customizing the Nature of a Bean章节做的部分理解,可能并不适合广大的群众。若有理解不到位或者有错误的地方请指出。

官方文档:https://docs.spring.io/spring/docs/5.1.4.RELEASE/spring-framework-reference/core.html#beans-factory-scopes-other

自定义Bean:

Lifecycle Callbacks(生命周期回调)

实现InitializingBean接口,会在Bean实例化以后调用afterPropertiesSet(),个人理解可以类比在一个java类执行完构造方法后调用此方法。

实现DisposableBean接口,在销毁Bean的时候会调用 destroy()。

Spring2.5增加了支持JSR-250 @PostConstruct and @PreDestroy注解,分别对应对应实现InitializingBean和DisposableBean接口的方法

使用JSR-250

import org.springframework.stereotype.Component;

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

@Component
public class JSR250Test {

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

 

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

@Component
public class SpringBeanInterfaceImpl implements InitializingBean,DisposableBean {
    
        @Override
	public void destroy() throws Exception {
		System.out.println("SpringBeanInterfaceImpl preDestroy");
	}
	
	@Override
	public void afterPropertiesSet() throws Exception {
		System.out.println("SpringBeanInterfaceImpl postConstruct");
	}
}

关于一个Bean即实现了接口也有加了注解,那谁先执行呢,官方也给出了答复,在此我就贴Spring的原文。

Multiple lifecycle mechanisms configured for the same bean, 
with different initialization methods, are called as follows:

Methods annotated with @PostConstruct

afterPropertiesSet() as defined by the InitializingBean callback interface

A custom configured init() method

Destroy methods are called in the same order:

Methods annotated with @PreDestroy

destroy() as defined by the DisposableBean callback interface

A custom configured destroy() method
ApplicationContextAware,实现此接口的实例会提供ApplicationContext的引用,
在实际使用过程中实现了此接口的,通常会提供一个静态方法让外界访问获取Bean对象。
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @Author Adrain
 * @Date 2019/1/31 上午11:26
 * 此类要给Spring管理。获取spring容器里面的Bean,可以当将工具类开发出去
 **/
@Component
public class TestApplicationContextAware implements ApplicationContextAware {
	
	private static ApplicationContext applicationContext;
	
	
	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		if(this.applicationContext == null){
			this.applicationContext = applicationContext;
		}
	}
	
	public static <T> T getBean(Class<T> claz){
		return applicationContext.getBean(claz);
	}
	
}

BeanNameAware,实现这个接口是获取Bean在Ioc容器的id值。实际开发当中Spring官方是不建议使用此接口。我在实际开发当中也没有用过, 没有什么实际的应用场景。

import org.springframework.beans.factory.BeanNameAware;
import org.springframework.stereotype.Component;

@Component
public class TestBeanNameAware implements BeanNameAware {
	
	
	@Override
	public void setBeanName(String name) {
		System.out.println("TestBeanNameAware----------->name:"+name);
	}
}

在启动应用时可以看到控制台打印的:TestBeanNameAware----------->name:testBeanNameAware

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值