当前位置: 首页 > news >正文

网站建设的静态网页作业怎么建自己的网站?

网站建设的静态网页作业,怎么建自己的网站?,做电商需要学哪些基础,抖音seo软件工具SpringBoot系列之启动成功后执行业务逻辑。在Springboot项目中经常会遇到需要在项目启动成功后#xff0c;加一些业务逻辑的#xff0c;比如缓存的预处理#xff0c;配置参数的加载等等场景#xff0c;下面给出一些常有的方法 实验环境 JDK 1.8SpringBoot 2.2.1Maven 3.2…SpringBoot系列之启动成功后执行业务逻辑。在Springboot项目中经常会遇到需要在项目启动成功后加一些业务逻辑的比如缓存的预处理配置参数的加载等等场景下面给出一些常有的方法 实验环境 JDK 1.8SpringBoot 2.2.1Maven 3.2Mysql 8.0.26开发工具 IntelliJ IDEA smartGit 动手实践 ApplicationRunner和CommandLineRunner 比较常有的使用Springboot框架提供的ApplicationRunner和CommandLineRunner这两种Runner可以实现在Springboot项目启动后执行我们自定义的业务逻辑然后执行的顺序可以通过Order进行排序参数值越小越早执行 写个测试类实现ApplicationRunner接口注意加上Component才能被Spring容器扫描到 package com.example.jedis.runner;import lombok.extern.slf4j.Slf4j; import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationRunner; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component;Order(1) Component Slf4j public class TestApplicationRunner implements ApplicationRunner {Overridepublic void run(ApplicationArguments args) throws Exception {log.info(TestApplicationRunner);} } 实现CommandLineRunner接口 package com.example.jedis.runner;import lombok.extern.slf4j.Slf4j; import org.springframework.boot.CommandLineRunner; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component;Order(2) Component Slf4j public class TestCommandLineRunner implements CommandLineRunner {Overridepublic void run(String... args) throws Exception {log.info(TestCommandLineRunner);} } ApplicationListener加ApplicationStartedEvent SpringBoot基于Spring框架的事件监听机制提供ApplicationStartedEvent可以对SpringBoot启动成功后的监听基于事件监听机制我们可以在SpringBoot启动成功后做一些业务操作 package com.example.jedis.listener;import lombok.extern.slf4j.Slf4j; import org.springframework.boot.context.event.ApplicationStartedEvent; import org.springframework.context.ApplicationListener; import org.springframework.stereotype.Component;Component Slf4j public class TestApplicationListener implements ApplicationListenerApplicationStartedEvent {Overridepublic void onApplicationEvent(ApplicationStartedEvent applicationStartedEvent) {log.info(onApplicationEvent);} } SpringApplicationRunListener 如果要在启动的其它阶段做业务操作可以实现SpringApplicationRunListener接口例如要实现打印swagger的api接口文档url可以在对应方法进行拓展即可 package com.example.jedis.listener;import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplicationRunListener; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.core.env.ConfigurableEnvironment;import java.net.InetAddress;Slf4j public class TestSpringApplicationRunListener implements SpringApplicationRunListener {private final SpringApplication application;private final String[] args;public TestSpringApplicationRunListener(SpringApplication application, String[] args) {this.application application;this.args args;}Overridepublic void starting() {log.info(starting...);}Overridepublic void environmentPrepared(ConfigurableEnvironment environment) {log.info(environmentPrepared...);}Overridepublic void contextPrepared(ConfigurableApplicationContext context) {log.info(contextPrepared...);}Overridepublic void contextLoaded(ConfigurableApplicationContext context) {log.info(contextLoaded...);}Overridepublic void started(ConfigurableApplicationContext context) {log.info(started...);}SneakyThrowsOverridepublic void running(ConfigurableApplicationContext context) {log.info(running...);ConfigurableEnvironment environment context.getEnvironment();String port environment.getProperty(server.port);String contextPath environment.getProperty(server.servlet.context-path);String docPath port contextPath /doc.html;String externalAPI InetAddress.getLocalHost().getHostAddress();log.info(\n Swagger API: Local-API: \t\thttp://127.0.0.1:{}\n\t External-API: \thttp://{}:{}\n\t,docPath, externalAPI, docPath);}Overridepublic void failed(ConfigurableApplicationContext context, Throwable exception) {log.info(failed...);} } 在/META-INF/spring.factories配置文件配置 org.springframework.boot.SpringApplicationRunListener\com.example.jedis.listener.TestSpringApplicationRunListener源码分析 在Springboot的run方法里找到如下的源码大概看一下就可以知道里面是封装了对Runner和SpringApplicationRunListener的调用 public ConfigurableApplicationContext run(String... args) {StopWatch stopWatch new StopWatch();stopWatch.start();ConfigurableApplicationContext context null;CollectionSpringBootExceptionReporter exceptionReporters new ArrayList();this.configureHeadlessProperty();SpringApplicationRunListeners listeners this.getRunListeners(args);// SpringApplicationRunListener调用listeners.starting();Collection exceptionReporters;try {ApplicationArguments applicationArguments new DefaultApplicationArguments(args);ConfigurableEnvironment environment this.prepareEnvironment(listeners, applicationArguments);this.configureIgnoreBeanInfo(environment);Banner printedBanner this.printBanner(environment);context this.createApplicationContext();exceptionReporters this.getSpringFactoriesInstances(SpringBootExceptionReporter.class, new Class[]{ConfigurableApplicationContext.class}, context);this.prepareContext(context, environment, listeners, applicationArguments, printedBanner);this.refreshContext(context);this.afterRefresh(context, applicationArguments);stopWatch.stop();if (this.logStartupInfo) {(new StartupInfoLogger(this.mainApplicationClass)).logStarted(this.getApplicationLog(), stopWatch);}// SpringApplicationRunListener startlisteners.started(context);// 调用所有的Runnerthis.callRunners(context, applicationArguments);} catch (Throwable var10) {this.handleRunFailure(context, var10, exceptionReporters, listeners);throw new IllegalStateException(var10);}try {// SpringApplicationRunListener running执行listeners.running(context);return context;} catch (Throwable var9) {this.handleRunFailure(context, var9, exceptionReporters, (SpringApplicationRunListeners)null);throw new IllegalStateException(var9);}}
http://www.yutouwan.com/news/169112/

相关文章:

  • 律师网站深圳网站设计淘宝客的优惠卷网站怎么做的
  • 中原郑州网站建设帮忙注册公司
  • 多元网站建设部网站核对编号
  • 设计网站的功能有哪些内容教人做家务的网站
  • 网站设计公司排名知乎discuz整合wordpress
  • 政务中心建设网站北京注册公司麻烦吗
  • 广东建设基金管理有限公司网站Wordpress 图片左右滑动
  • 豆瓣读书网站模板南宁seo企业优化
  • 网站标题关键词用什么隔开权重查询工具
  • 秦皇岛城乡建设局网站西安网站建设推广优化
  • 免费微信网站源码中国建设银行企业
  • asp建设网站需要了解什么如何做国际网站首页
  • 秦皇岛高端网站设计网站建设拿什么框架
  • 9377 这种网站怎么做缔造自助建站
  • 网站怎样查是哪家做的长沙传媒公司招聘
  • 空壳网站查询wordpress模块咋编辑
  • 安庆网站建设推荐安徽秒搜科技seo培训学校排名
  • 2017做那些网站致富天天做网站
  • 买电脑的怎么下wordpress什么是优化营商环境
  • 优礼品网站模板如何建立一家公司网站
  • 北京网站建设课程培训班桂林漓江景区游玩攻略
  • 怎样用百度做网站优化国内网站开发公司
  • 浙江网站建设推广公司找哪家平邑县门户网站
  • 广州外贸网站推广免费建站网站一级大录像不卡在线看网页
  • 哪儿网站建设费用低建立网站目录结构时正确的建议是
  • 专业做轮胎的网站如何在本地发布自己做的网站
  • 山东网站备案图片网站怎么做排名
  • 淄博网站建设多好的设计师网站有哪些
  • 户县规划建设和住房保障局网站做牙齿的招聘网站
  • 关于建设网站的培训知识做网络调查的网站赚钱