




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
SpringBoot中初始化资源的方式,你知道几种?假设有这么一个需求,要求在项目启动过程中,完成线程池的初始化,加密证书加载等功能,你会怎么做?如果没想好答案,请接着往下看。今天介绍几种在SpringBoot中进行资源初始化的方式,帮助大家解决和回答这个问题。CommandLineRunner定义初始化类
MyCommandLineRunner实现
CommandLineRunner
接口,并实现它的
run()
方法,在该方法中编写初始化逻辑注册成Bean,添加
@Component注解即可示例代码如下:package
cn.zh.controller;
import
org.springframework.boot.CommandLineRunner;
import
org.springframework.core.annotation.Order;
import
org.springframework.stereotype.Component;
@Componentpublic
class
MyCommandLineRunner
implements
CommandLineRunner
{
@Override
public
void
run(String...
args)
throws
Exception
{
System.out.println("项目初始化---------------11");
}
}实现了CommandLineRunner接口的Component会在所有SpringBeans初始化完成之后,在SpringApplication.run()执行之前完成。下面通过加两行打印来验证我们的测试。package
cn.zh;
import
org.springframework.boot.SpringApplication;
import
org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public
class
ProcApplication
{
public
static
void
main(String[]
args)
{
System.out.println("...start
SpringApplication.run");
SpringApplication.run(ProcApplication.class,args);
System.out.println("....end
SpringApplication.run");
}
}ApplicationRunner定义初始化类
MyApplicationRunner实现
ApplicationRunner
接口,并实现它的
run()
方法,在该方法中编写初始化逻辑注册成Bean,添加
@Component注解即可示例代码如下:package
cn.zh.controller;
import
org.springframework.boot.ApplicationArguments;
import
org.springframework.boot.ApplicationRunner;
import
org.springframework.core.annotation.Order;
import
org.springframework.stereotype.Component;
import
javax.annotation.PostConstruct;
@Component
public
class
MyApplicationRunner
implements
ApplicationRunner
{
@Override
public
void
run(ApplicationArguments
args)
throws
Exception
{
System.out.println("项目初始化二---------");
}
}可以看到,通过实现ApplicationRunner接口,和通过实现CommandLineRunner接口都可以完成项目的初始化操作,实现相同的效果。两者之间唯一的区别是
run()
方法中自带的形参不相同,在CommandLineRunner中只是简单的String...args形参,而ApplicationRunner则是包含了ApplicationArguments对象,可以帮助获得更丰富的项目信息。@Order如果项目中既有实现了
ApplicationRunner
接口的初始化类,又有实现了
CommandLineRunner
接口的初始化类,那么会是哪一个先执行呢?测试告诉我们,答案是实现了
ApplicationRunner
接口的初始化类先执行,我想这点倒是不需要大家过分去关注为什么。但如果需要改变两个初始化类之间的默认执行顺序,那么使用
@Order
注解就可以帮助我们解决这个问题。搜索公众号后端架构师后台回复“架构整洁”,获取一份惊喜礼包。@Component
@Order(1)
public
class
MyCommandLineRunner
implements
CommandLineRunner
{
/**
*
Callback
used
to
run
the
bean.
*
*
@param
args
incoming
main
method
arguments
*
@throws
Exception
on
error
*/
@Override
public
void
run(String...
args)
throws
Exception
{
System.out.println("项目初始化---------------11");
}
}
@Component
@Order(2)
public
class
MyApplicationRunner
implements
ApplicationRunner
{
@Override
public
void
run(ApplicationArguments
args)
throws
Exception
{
System.out.println("项目初始化二---------");
}
@PostConstruct
public
void
init(){
System.out.println("@PostConstruct初始化");
}
}@PostConstruct使用
@PostConstruct
注解同样可以帮助我们完成资源的初始化操作,前提是这些初始化操作不需要依赖于其它Springbeans的初始化工作。可以看到
@PostConstruct
注解是用在方法上的,写一个方法测试一下吧。@PostConstruct
public
void
init(){
System.out.println("@PostConstruct初始化");
}注意:只有一个非静态方法能使用此注解被注解的方法不得有任何参数被注解的方法返回值必须为void被注解方法不得抛出已检查异常此方法只会被执行一次综上,使用
@PostConstruct
注解进行初始化操作的顺序是最快的,前提是这些操作不能依赖于其它Bean的初始化完成。通过添加
@Order
注解,我们可以改变同层级之间不同Bean的加载顺序。InitializingBeanInitializingBean是Spring提供的一个接口,只包含一个方法afterPropertiesSet()。凡是实现了该接口的类,当其对应的Bean交由Spring管理后,当其必要的属性全部设置完成后,Spring会调用该Bean的afterPropertiesSet()。@Component
public
class
MyListener1
implements
InitializingBean
{
@Autowired
private
ShopInfoMapper
shopInfoMapper;
@Override
public
void
afterPropertiesSet()
{
//使用spring容器中的bean
//System.out.println(shopInfoMapper.selectById("1").getShopName());
System.out.println("项目启动OK");
}
}ApplicationListenerApplicationListener就是spring的监听器,能够用来监听事件,典型的观察者模式。如果容器中有一个ApplicationListenerBean,每当ApplicationContext发布ApplicationEvent时,ApplicationListenerBean将自动被触发。这种事件机制都必须需要程序显示的触发。其中spring有一些内置的事件,当完成某种操作时会发出某些事件动作。比如监听ContextRefreshedEvent事件,当所有的bean都初始化完成并被成功装载后会触发该事件,实现ApplicationListener接口可以收到监听动作,然后可以写自己的逻辑。同样事件可以自定义、监听也可以自定义,完全根据自己的业务逻辑来处理。所以也能做到资源的初始化加载。@Component
public
class
MyListener1
implements
ApplicationListener
{
@Override
public
void
onApplicationEvent(ApplicationEvent
applicationE
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 外科入院流程管理办法
- 小区绿化养护管理办法
- 外包薪酬结算管理办法
- 城市生活垃圾管理办法
- 展会补助资金管理办法
- 寺院投资项目管理办法
- 小学劳动教学创新评估计划
- 冀教版小学英语听说训练教学计划
- 小学阅读课阅读评价教学计划
- 部编版四年级道德与法治上册德育融合计划
- GB/T 16451-1996天然脂肪醇
- (约克)机组热回收技术
- 《小学趣味语文》PPT课件(优秀)
- 疫苗及其制备技术课件
- 世界卫生组织-人瘤病毒疫苗:世卫组织立场文件2022年5月(英译中)
- (完整版)常见肿瘤AJCC分期手册第八版(中文版)
- 《企业转型升级研究》文献综述(3000字)
- 人教版PEP初中八年级下册英语全册课件
- 幼儿园大班数学:《认识单双数》课件
- 日本文化介绍
- 药厂MES系统解决方案
评论
0/150
提交评论