欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 财经 > 产业 > SpringBoot启动初始化配置之ApplicationRunner

SpringBoot启动初始化配置之ApplicationRunner

2025/4/19 14:02:14 来源:https://blog.csdn.net/fxtxz2/article/details/141156649  浏览:    关键词:SpringBoot启动初始化配置之ApplicationRunner

问题

最近需要在Spring Boot启动的时候,需要初始化第三方SDK配置,才能使用自己这个Spring Boot服务。

解决思路

使用Spring的ApplicationRunner进行初始化第三方SDK配置。Spring还有另外一个初始化接口CommandLineRunner,这里选择ApplicationRunner。

步骤

CustomApplicationRunner.java

package com.xxx.init;import com.xxx.XxxUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;import java.io.IOException;@Slf4j
@Component
public class CustomApplicationRunner implements ApplicationRunner {@Value("classpath:xxx_client_config.properties")private Resource resource;@Overridepublic void run(ApplicationArguments args) {try {if (XxxUtil.getInstance().init(resource.getInputStream())){log.info("xxx SDK 初始化成功");} else {log.error("xxxx SDK 初始化失败");}} catch (IOException e) {log.error("xxx SDK 初始化失败", e);}}
}

创建好这个ApplicationRunner实现类之后,然后,再启动Spring,Spring就会去初始化这个配置了。

参考

  • SpringBoot——项目启动时读取配置及初始化资源

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词