SpringBoot啟動(dòng)執(zhí)行初始化代碼
標(biāo)簽:
SpringBoot
利用CommandLineRunner、EnvironmentAware在Spring boot启动时执行初始化代码
import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.CommandLineRunner;import org.springframework.context.EnvironmentAware;import org.springframework.core.annotation.Order;import org.springframework.core.env.Environment;import org.springframework.stereotype.Component;import java.util.List;@Component//如果有多个这样的类时,可以通过Order指定执行顺序,数值越小执行优先级越高@Order(value = 0)public class InitSystemConfig implements CommandLineRunner, EnvironmentAware { /* * 在服务启动后执行,会在@Bean实例化之后执行,故如果@Bean需要依赖这里的话会出问题 */ @Override public void run(String... args) { //这里可以根据数据库返回结果创建一些对象、启动一些线程等 } /* * 在SystemConfigDao实例化之后、@Bean实例化之前执行 * 常用于读取数据库配置以供其它bean使用 * environment对象可以获取配置文件的配置,也可以把配置设置到该对象中 */ @Override public void setEnvironment(Environment environment) { } }
github主页:https://github.com/xvjialing
作者:褪色的记忆1994
链接:https://www.jianshu.com/p/7826ce1d95ee
點(diǎn)擊查看更多內(nèi)容
為 TA 點(diǎn)贊
評(píng)論
評(píng)論
共同學(xué)習(xí),寫下你的評(píng)論
評(píng)論加載中...
作者其他優(yōu)質(zhì)文章
正在加載中
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說(shuō)多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開(kāi)微信掃一掃,即可進(jìn)行掃碼打賞哦