InitializingBean详解与用法_凌兮~的博客-程序员宅基地

技术标签: springcloud  spring boot  bean  Initializing  

ApplicationContextAware用法

当一个类实现了这个接口之后,这个类就可以方便的获得ApplicationContext对象(spring上下文),Spring发现某个Bean实现了ApplicationContextAware接口,Spring容器会在创建该Bean之后,自动调用该Bean的setApplicationContext(参数)方法,调用该方法时,会将容器本身ApplicationContext对象作为参数传递给该方法。

package com.lyj.demo.utils;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @author 凌兮
 * @date 2020/5/18 15:39
 * 全局上下文
 */
@Component
public class ApplicationContextUtil implements ApplicationContextAware {
    

    private static ApplicationContext context;
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    
        context = applicationContext;
    }
    public static ApplicationContext getApplicationContext(){
    
        return context;
    }
    /**
     * 通过name获取 Bean
     * @param name beanName
     * @return Object
     */
    public static Object getBean(String name){
    
        return getApplicationContext().getBean(name);
    }



    public static <T> T getBean(Class<T> requiredType) throws BeansException{
    
        return getApplicationContext().getBean(requiredType);
    }
}


InitialzingBean用法

当一个类实现这个接口之后,Spring启动后,初始化Bean时,若该Bean实现InitialzingBean接口,会自动调用afterPropertiesSet()方法,完成一些用户自定义的初始化操作。

package com.lyj.demo.studySpringBoot.init;
 
import com.lyj.studySpringBoot.entity.Student;
import org.springframework.beans.factory.InitializingBean;
 
public class SpringBeanInit implements InitializingBean {
    
 
    private Integer id;
 
    private String name;
 
    private Integer age;
 
    private boolean sex;
 
    private Student student;
 
 /** 这里进行优先调用初始化一些参数
 */
    @Override
    public void afterPropertiesSet() throws Exception {
    
        System.out.println("this is bean init set student data");
        Student student = new Student(id,name,age,sex);
        this.student = student;
    }
 
    public void testInit(){
    
        System.out.println("this is bean web.xml init-method invock");
    }
 
    public Student getStudent() {
    
        return student;
    }
 
    public void setStudent(Student student) {
    
        this.student = student;
    }
 
    public Integer getId() {
    
        return id;
    }
 
    public void setId(Integer id) {
    
        this.id = id;
    }
 
    public String getName() {
    
        return name;
    }
 
    public void setName(String name) {
    
        this.name = name;
    }
 
    public Integer getAge() {
    
        return age;
    }
 
    public void setAge(Integer age) {
    
        this.age = age;
    }
 
    public boolean isSex() {
    
        return sex;
    }
 
    public void setSex(boolean sex) {
    
        this.sex = sex;
    }
}

同样配置Bean的时候使用init-method也可以实现类似的操作

	<bean id = "springBeanInit02" class = "com.lyj.studySpringBoot.init.SpringBeanInit" init-method="testInit">
		<property name="id" value="#{1111111}" />
		<property name="name" value="${test.springEL}" />
		<property name="age" value="#{10+8}" /> // SpringEL表达式
		<property name="sex" value="false" />
	</bean>

在spring初始化bean的时候,如果该bean是实现了InitializingBean接口,并且同时配置文件中指定了init-method,系统则是先调用afterPropertiesSet方法,然后在调用init-method中指定的方法。

重要提醒

Spring是通过反射来调用init-method指定方法,而实现InitializingBean接口是直接调用afterPropertiesSet方法,所以后者效率高,但使用init-method方式减少了对Spring的依赖
如果调用afterPropertiesSet方法时报错,则不会再调用init-method指定的方法

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_40093255/article/details/117318760

智能推荐

JavaScript入门篇(红宝书第四版+犀牛书权威指南)一_javascript犀牛书电子版分享_showmymind的博客-程序员宅基地

对于前端开发者来说,《JavaScript高级程序设计》(俗称红宝书)和《JavaScript权威指南》肯定是两本封神之作,也是每一个前端工程师的必学经典。我们今天就一起跟着这两本书一起进入JavaScript(以后简称JS)的世界。 什么是JS?很多人只知道它是一门脚本语言,但是具体来说,JS由三部分组成。ECMAScript核心 文档对线模型DOM 浏览器对象模型BOMECMAScript是一种由Ecma国际(前身为欧洲计算机制造商协会,Europe..._javascript犀牛书电子版分享

成长中必须知道的20个道理-程序员宅基地

1、断箭    不相信自己的意志,永远也做不成将军。    春秋战国时代,一位父亲和他的儿子出征打战。父亲已做了将军,儿子还只是马前卒。又一阵号角吹响,战鼓雷鸣了,父亲庄严地托起一个箭囊,其中插着一只箭。父亲郑重对儿子说:“这是家袭宝箭,配带身边,力量无穷,但千万不可抽出来。”    那是一个极其精美的箭囊,厚牛皮打制,镶着幽幽泛光的铜边儿,再看露出的箭尾。一眼便能认定用上等的孔雀羽毛制作。儿子喜

微信小程序中image无法显示-程序员宅基地

在小程序中image不能放在text里面,改成view就好

linux 中read命令后面-p是什么意思呢_linux中-p是什么意思-程序员宅基地

p是prompt的缩写,即由这个开关项给出提示信息,例如read -p 'please input you choice:\n' choice回车后屏幕会显示:please input you choice:那么之后你输入的东西就会被保存到choice变量中了另外,read是bash的内建命令,这些选项的作用都可以通过man命令获得的..._linux中-p是什么意思

截至2005年11月25日10点,VB区总得分榜(包含非技术区)-程序员宅基地

数据来源: 汇总自所有VB版的得分回复lihonggen0122651rainstormmaster115200zyl910107580online98906TechnoFantasy81025daisy867552304cuizm51893tztz52050964of12343653viena43486lxcc42791holydiablo33252Leftie32741jennyvenus31

随便推点

python 100days github_Python-100-Days: Python - 100天从新手到大师 作者github地址:https://github.com/jackfrued/P...-程序员宅基地

Python - 100天从新手到大师作者:骆昊最近有很多想学习Python的小伙伴陆陆续续加入我们的交流群,目前我们的交流群人数已经超过一万人。我们的目标是打造一个优质的Python交流社区,一方面为想学习Python的初学者扫平入门过程中的重重障碍;另一方为新入行的开发者提供问道的途径,帮助他们迅速成长为优秀的职业人;此外,有经验的开发者可以利用这个平台把自己的工作经验无偿分享或有偿提供出来,[email protected]:jackfrued/python-100-days.git

用户空间和内核空间通讯之【Netlink 】-程序员宅基地

用户空间和内核空间通讯之【Netlink 上】 Alan Cox在内核1.3版本的开发阶段最先引入了Netlink,刚开始时Netlink是以字符驱动接口的方式提供内核与用户空间的双向数据通信;随后,在2.1内核开发过程中,Alexey Kuznetsov将Netlink改写成一个更加灵活、且易于扩展的基于消息通信接口,并将其应用到高级路由子系统的基础框架里。自那时起,Netlink

ARtoolkit源码阅读(一) artoolkit框架simpletest.c_artoolkit框架下载-程序员宅基地

ARToolKit 框架ARToolKit 是基于opengl和DSVL的增强现实软件包。它遵循opengl的运行模式,基于帧循环实现3D渲染。 主要流程: 1. 初始化视频捕获arVideoCapStart()、读取文件的标记模式、相机参数 arVideoOpen(vconf) 2. 获取一帧输入视频 3. 在输入视频图像中 检测标识和注册的模板 4. 计算相机相对于检测到模板的位..._artoolkit框架下载

matlab中值滤波实现_中值滤波,matlab-程序员宅基地

中值滤波是一种典型的非线性滤波,是基于排序统计理论的一种能够有效抑制噪声的非线性信号处理技术,基本思想是用像素点邻域灰度值的中值来代替该像素点的灰度值,让周围的像素值接近真实的值从而消除孤立的噪声点。该方法在去除脉冲噪声、椒盐噪声的同时能保留图像的边缘细节中值滤波示意如下图所示matlab代码实现clc;clear all;close all;RGB_data = imread('G:\picture_deal\matlab_code\mangguo.bmp');%图像读入..._中值滤波,matlab

当连接一个 IP 不存在的主机时,TCP握手过程是怎样的?_kevin_tech的博客-程序员宅基地

最近前同事去字节跳动面试,被问到了下面这个问题:连接一个IP 不存在的主机时,握手过程是怎样的?连接一个IP 地址存在但端口号不存在的主机时,握手过程又是怎样的呢?这让我回想起曾经也被..._tcp在不知道ip的情况下如何连接

Zookeeper--概述_zookeeper是一个开源的分布式的,为分布式应用提供协调服务的apache项目。它是一种-程序员宅基地

概述Zookeeper 是一个开源的分布式的,为分布式应用提供协调服务的 Apache 项目。特点Zookeeper:一个领导者(Leader),多个跟随者(Follower)组成的集群。集群中只要有半数以上节点存活,Zookeeper集群就能正常服务。全局数据一致:每个Server保存一份相同的数据副本,Client无论连接到哪个Server,数据都是一致的。更新请求顺序进行,..._zookeeper是一个开源的分布式的,为分布式应用提供协调服务的apache项目。它是一种

推荐文章

热门文章

相关标签