springboot自定义stater启动流程
springboot启动时自动加载application.properties或者application.yml,如何定义自己的配置让springboot自动识别:
首先我们新建一个maven工程打包方式选择jar,然后引入所需的包
4.0.0 org.springframework.boot spring-boot-starter-parent 2.1.6.RELEASE com.ruobbo.xxl ruobbo.xxl 0.0.1-SNAPSHOT jar 1.8 org.springframework.boot spring-boot-starter 2.1.6.RELEASE org.springframework.boot spring-boot-autoconfigure 2.1.6.RELEASE org.springframework.boot spring-boot-configuration-processor 2.1.6.RELEASE org.projectlombok lombok 1.18.8 provided com.xuxueli xxl-job-core 2.0.1 org.springframework.boot spring-boot-starter-test test org.junit.vintage junit-vintage-engine releases http://192.168.1.99:8081/nexus/content/repositories/releases snapshots http://192.168.1.99:8081/nexus/content/repositories/snapshots
然后是配置读取
@ConfigurationProperties(prefix="xxl.job.executor")
@Data
publicclassXxlProperties{
privateStringadminAddresses;
privateStringappName;
privateStringip;
privateintport;
privateStringaccessToken;
privateStringlogPath;
privateintlogRetentionDays;
privateStringbasePackages;
}
将读取到的配置注入到bean中,然后加载到springbean容器中
@Configuration
@EnableConfigurationProperties(XxlProperties.class)
publicclassXxlAutoConfiguration{
privateLoggerlogger=LoggerFactory.getLogger(XxlAutoConfiguration.class);
@Resource
privateXxlPropertiesxxlProperties;
@Bean(initMethod="start",destroyMethod="destroy")
publicXxlJobSpringExecutorxxlJobExecutor(){
logger.info(">>>>>>>>>>>xxl-jobconfiginit.");
XxlJobSpringExecutorxxlJobSpringExecutor=newXxlJobSpringExecutor();
xxlJobSpringExecutor.setAdminAddresses(xxlProperties.getAdminAddresses());
xxlJobSpringExecutor.setAppName(xxlProperties.getAppName());
xxlJobSpringExecutor.setIp(xxlProperties.getIp());
xxlJobSpringExecutor.setPort(xxlProperties.getPort());
xxlJobSpringExecutor.setAccessToken(xxlProperties.getAccessToken());
xxlJobSpringExecutor.setLogPath(xxlProperties.getLogPath());
xxlJobSpringExecutor.setLogRetentionDays(xxlProperties.getLogRetentionDays());
returnxxlJobSpringExecutor;
}
}
最后在resources目录下添加META-INF文件夹添加spring.factories文件,文件内容为指定要初始化springbean的类全路径
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\com.ruobbo.xxl.XxlAutoConfiguration
使用过程中引入包
com.ruobbo.xxl ruobbo.xxl 0.0.1-SNAPSHOT
然后添加配置到application.properties或者application.yml
总结
以上所述是小编给大家介绍的springboot自定义stater启动流程,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。