详解Spring中InitializingBean接口的功能

网友投稿 252 2022-07-31


Spring的InitializingBean接口有很好的用处,位于spring beans中,它只提供一个方法afterPropertiesSet(),当你实现了该方法后,spring就会对你提供框架级的支持:当你通过sring容器生产出实现了该接口的类的实例后,它就会调用afterPropertiesSet方法,通过这个方法,你可以检查你的bean是否正确地被初始化了.当然,你也可以用init-method方法.这两种方式可以同时使用,调用的顺序为init-method后调用.

下面介绍下Spring中InitializingBean接口的功能,内容如下所示:

如果你将类交给Spring容器管理,但是需要Spring帮你运行初始化方法

此时我们可以借助InitializingBean接口实现初始化方法的效果

InitializingBean接口的原理:

Spring实例化一个类后,会调用类中的afterPropertiesSet方法,达到初始化初始化方法的目的

下文笔者讲述Spring中InitializingBean接口的功能简介说明,如下所示

InitializingBean接口的功能

InitializingBean接口

为bean提供了初始化方法的方式

这个接口中只包括afterPropertiesSet方法

凡是继承该接口的类

在初始化bean的时,都会运行afterPropertiesSet方法

import org.springframework.beans.factory.InitializingBean;

import org.springframework.stereotype.Service;

public class InitBean implements InitializingBean{

public void afterPropertiesSet() throws Exception {

System.out.println("启动时自动执行 afterPropertiesSet...");

}

public void init(){

System.out.println("init method...");

}

} 

---配置文件-----

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"

xmlns:mvc="http://springframework.org/schema/mvc"

xmlns:aop="http://springframework.org/schema/aop"

xmlns:tx="http://springframework.org/schema/tx"

xsi:schemaLocation=

"http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-3.2.xsd

http://springfrMDRuBnOdpamework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc-3.2.xsd

http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-3.2.xsd

http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-3.2.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-3.2.xsd

http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"

default-lazy-init="true">

  

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"

xmlns:mvc="http://springframework.org/schema/mvc"

xmlns:aop="http://springframework.org/schema/aop"

xmlns:tx="http://springframework.org/schema/tx"

xsi:schemaLocation=

"http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-3.2.xsd

http://springfrMDRuBnOdpamework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc-3.2.xsd

http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-3.2.xsd

http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-3.2.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-3.2.xsd

http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"

default-lazy-init="true">

---main程序----

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.FileSystemXmlApplicationContext;

public class Main {

public static void main(String[] args) {

Applicatihttp://onContext context = new

FileSystemXmlApplicationContext("classpath:/applicationContext-core.xml");

context.getBean("initBean");

}

}  

-----运行以上代码,将输出以下信息---------

启动时自动执行 afterPropertiesSet...

init method...


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:kafka的消息存储机制和原理分析(kafka原理及应用)
下一篇:Kafka中消息队列的两种模式讲解
相关文章

 发表评论

暂时没有评论,来抢沙发吧~