如何使用springboot自定义stater启动流程-创新互联

这篇文章将为大家详细讲解有关如何使用springboot自定义stater启动流程,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

成都创新互联是专业的兰考网站建设公司,兰考接单;提供做网站、网站设计,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行兰考网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!

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")@Datapublic class XxlProperties {  private String adminAddresses;  private String appName;  private String ip;  private int port;  private String accessToken;  private String logPath;  private int logRetentionDays;  private String basePackages;  }

将读取到的配置注入到bean中,然后加载到spring bean容器中

@Configuration@EnableConfigurationProperties(XxlProperties.class)public class XxlAutoConfiguration {    private Logger logger = LoggerFactory.getLogger(XxlAutoConfiguration.class);    @Resource  private XxlProperties xxlProperties;    @Bean(initMethod = "start", destroyMethod = "destroy")  public XxlJobSpringExecutor xxlJobExecutor() {    logger.info(">>>>>>>>>>> xxl-job config init.");    XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();    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());    return xxlJobSpringExecutor;  }  }

最后在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启动流程”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。


网站题目:如何使用springboot自定义stater启动流程-创新互联
当前URL:http://pwwzsj.com/article/hhssi.html