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

专业网站设计制作服务138ip查询网域名解析

专业网站设计制作服务,138ip查询网域名解析,做网站内链什么意思,中国人民银行网站首页在Spring Boot应用程序中,CommandLineRunner和ApplicationRunner是两个重要的接口,它们允许我们在应用程序启动后执行一些初始化任务。本文将介绍CommandLineRunner和ApplicationRunner的区别,并提供代码示例和使用场景,让我们更好…

在Spring Boot应用程序中,CommandLineRunner和ApplicationRunner是两个重要的接口,它们允许我们在应用程序启动后执行一些初始化任务。本文将介绍CommandLineRunner和ApplicationRunner的区别,并提供代码示例和使用场景,让我们更好地理解和使用这两个接口。
在这里插入图片描述

CommandLineRunner和ApplicationRunner的用法

  1. CommandLineRunner接口:
    • 方法签名: void run(String... args)
    • 参数类型: 字符串数组,表示应用程序启动时传递的命令行参数
    • 执行时机: 在Spring上下文准备好之后,但在调用ApplicationRunner之前执行。
  2. ApplicationRunner接口:
    • 方法签名: void run(ApplicationArguments args)
    • 参数类型: ApplicationArguments对象,提供对应用程序启动参数的更高级别访问
    • 执行时机: 在CommandLineRunner之后执行。

这两个接口的目的是允许开发人员在应用程序启动完成后执行一些自定义的任务,例如加载初始化数据、执行数据迁移、启动后台任务等,它们都实现了org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean注解,这意味着只有在没有其他类型的Bean定义的情况下,才会自动配置它们。

我们可以通过以下两种方式使用CommandLineRunnerApplicationRunner

  1. 通过实现接口并将其作为Spring Bean注册:

    • 创建一个类并实现CommandLineRunnerApplicationRunner接口
    • 实现接口的run方法,在该方法中编写您的自定义逻辑
    • 将实现类标记为@Component或使用其他适当的注解进行注解,以便使其成为Spring Bean
    import org.springframework.boot.ApplicationArguments;
    import org.springframework.boot.ApplicationRunner;
    import org.springframework.stereotype.Component;@Component
    public class MyApplicationRunner implements ApplicationRunner {@Overridepublic void run(ApplicationArguments args) throws Exception {// 在这里编写您的自定义逻辑}
    }
    
  2. 通过使用SpringApplicationrun方法参数进行注册:

    • SpringApplication.run方法中,将实现了CommandLineRunnerApplicationRunner接口的实例作为参数传递给run方法。
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.CommandLineRunner;
    import org.springframework.boot.ApplicationRunner;@SpringBootApplication
    public class YourApplication implements CommandLineRunner, ApplicationRunner {public static void main(String[] args) {SpringApplication.run(YourApplication.class, args);}@Overridepublic void run(String... args) throws Exception {// 在这里编写您的自定义逻辑}@Overridepublic void run(ApplicationArguments args) throws Exception {// 在这里编写您的自定义逻辑}
    }
    

无论我们选择哪种方式,一旦应用程序启动完成,run方法将被调用,并且我们可以在其中编写我们的自定义逻辑。可以根据我们的需求选择使用CommandLineRunnerApplicationRunner接口。

CommandLineRunner和ApplicationRunner的区别

参数不同

从上面的代码示例中,我们可以看到CommandLineRunner和ApplicationRunner的一个主要的不同就是它们的run方法的参数类型不同。CommandLineRunner的run方法接收一个String数组,它是直接从命令行传入的参数,比如java -jar myapp.jar arg1 arg2,那么arg1和arg2就会被传入到run方法中。而ApplicationRunner的run方法接收一个ApplicationArguments对象,它不仅包含了命令行传入的参数,还包含了其他的应用程序参数,比如--spring.profiles.active=dev,这些参数可以通过ApplicationArguments的方法来获取,比如args.getOptionNames()args.getNonOptionArgs()等。

执行顺序不同

另外一个不同就是CommandLineRunner和ApplicationRunner的执行顺序不同。如果我们在同一个应用程序中同时定义了多个CommandLineRunner和ApplicationRunner,那么它们的执行顺序是怎样的呢?答案是,首先执行所有的CommandLineRunner,然后执行所有的ApplicationRunner,而且它们都是按照优先级的顺序执行的,优先级越高,越先执行。我们可以通过@Order注解来指定它们的优先级,值越小,优先级越高,比如:

// 优先级为1的CommandLineRunner
@Component
@Order(1)
public class FirstCommandLineRunner implements CommandLineRunner {// 省略run方法
}// 优先级为2的CommandLineRunner
@Component
@Order(2)
public class SecondCommandLineRunner implements CommandLineRunner {// 省略run方法
}// 优先级为1的ApplicationRunner
@Component
@Order(1)
public class FirstApplicationRunner implements ApplicationRunner {// 省略run方法
}// 优先级为2的ApplicationRunner
@Component
@Order(2)
public class SecondApplicationRunner implements ApplicationRunner {// 省略run方法
}

在上面的代码中,我们定义了两个CommandLineRunner和两个ApplicationRunner,并且分别指定了它们的优先级。那么它们的执行顺序是:

  • FirstCommandLineRunner
  • SecondCommandLineRunner
  • FirstApplicationRunner
  • SecondApplicationRunner

CommandLineRunner和ApplicationRunner的使用场景

那么,我们什么时候应该使用CommandLineRunner和ApplicationRunner呢?一般来说,它们都可以用来在Spring容器启动后执行一些初始化的任务,比如加载配置,初始化数据,运行测试等。但是,根据它们的不同,我们可以根据具体的需求来选择合适的接口。下面是一些可能的使用场景:

  • 如果我们需要在Spring容器启动后执行一些简单的任务,而且不需要获取任何的应用程序参数,那么我们可以使用CommandLineRunner,它的用法比较简单,只需要实现一个接口,然后写好run方法即可。
  • 如果我们需要在Spring容器启动后执行一些复杂的任务,而且需要获取一些应用程序参数,比如Spring的配置参数,那么我们可以使用ApplicationRunner,它的用法比较灵活,可以通过ApplicationArguments对象来获取各种参数,然后根据参数来执行不同的逻辑。
  • 如果我们需要在Spring容器启动后执行一些和命令行相关的任务,比如解析命令行参数,执行一些命令,那么我们可以使用CommandLineRunner,它可以直接获取命令行传入的参数,然后根据参数来执行不同的命令。
  • 如果我们需要在Spring容器启动后执行一些和应用程序相关的任务,比如启动其他的组件,调用其他的服务,那么我们可以使用ApplicationRunner,它可以获取应用程序的上下文,然后根据上下文来执行不同的任务。

实操—获取SpringBoot启动后容器里面所有的Bean

Spring Boot 在内部加载了大量的 bean,以最小的配置运行我们的应用程序。 我们想要找出所有这些 SpringBoot 加载的 Bean 及其类类型信息,就可以使用上面说的方法

使用ApplicationContext获取所有已加载的 bean

1)使用ApplicationContext.getBeanDefinitionNames()查找所有已加载 bean 的名称

2)使用ApplicationContext.getBean(beanName)获取包含其运行时类型信息的 bean。

@SpringBootApplication
public class SpringBootWebApplication extends SpringBootServletInitializer implements CommandLineRunner {@Overrideprotected SpringApplicationBuilder configure(SpringApplicationBuilder application) {return application.sources(SpringBootWebApplication.class);}public static void main(String[] args) throws Exception {SpringApplication.run(SpringBootWebApplication.class, args);}@Autowiredprivate ApplicationContext appContext;@Overridepublic void run(String... args) throws Exception {String[] beans = appContext.getBeanDefinitionNames();Arrays.sort(beans);for (String bean : beans) {System.out.println(bean + " of Type :: " + appContext.getBean(bean).getClass());}}
}

输出信息如下:

....
basicErrorController of Type :: class org.springframework.boot.autoconfigure.web.BasicErrorController
beanNameHandlerMapping of Type :: class org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping
beanNameViewResolver of Type :: class org.springframework.web.servlet.view.BeanNameViewResolver
characterEncodingFilter of Type :: class org.springframework.boot.web.filter.OrderedCharacterEncodingFilter
conventionErrorViewResolver of Type :: class org.springframework.boot.autoconfigure.web.DefaultErrorViewResolver
defaultServletHandlerMapping of Type :: class org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport$EmptyHandlerMapping
defaultViewResolver of Type :: class org.springframework.web.servlet.view.InternalResourceViewResolver
dispatcherServlet of Type :: class org.springframework.web.servlet.DispatcherServlet
dispatcherServletRegistration of Type :: class org.springframework.boot.web.servlet.ServletRegistrationBean
duplicateServerPropertiesDetector of Type :: class org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration$DuplicateServerPropertiesDetector
embeddedServletContainerCustomizerBeanPostProcessor of Type :: class org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizerBeanPostProcessor
error of Type :: class org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$SpelView
errorAttributes of Type :: class org.springframework.boot.autoconfigure.web.DefaultErrorAttributes...
http://www.hrbkazy.com/news/42651.html

相关文章:

  • 常用 behance 全球设计师的作品展示平台百度首页排名优化多少钱
  • 可以做外贸的网站有哪些杭州seo搜索引擎优化公司
  • 重庆网站建设入门培训上海推广服务
  • wordpress拼音白杨seo教程
  • 甘肃住房与城乡建设厅网站百度大全
  • 菏泽网站建设公司排名网站seo优化外包
  • 做网站服务器内存移动广告平台
  • 网站创建流程包括哪些步骤win7一键优化工具
  • 网站我们只做av的搬运工搜索引擎营销的英文缩写
  • 网站后台数据分析怎么做苹果被曝开发搜索引擎对标谷歌
  • wordpress simple tags淄博搜索引擎优化
  • 网页美工设计入门详解seo应该怎么做
  • 网站空间编辑器公司百度推广一年多少钱
  • 营销型的物流网站模板下载成人零基础学电脑培训班
  • wordpress看不到表格yoast seo
  • 网站开发制作合同品牌推广
  • 智能建站工具得物app的网络营销分析论文
  • 如何搭建https网站广告联盟全自动赚钱系统
  • 网站交换链接怎么做?微信公众号的推广
  • 网站风格分析怎么写网络服务有限公司
  • 网站设计需求分析报告口碑营销
  • 做动态网站需要学什么百度一下移动版首页
  • 如何分析竞争对手的网站如何网络营销自己的产品
  • 建设招聘网站需要哪些资质百度的网页地址
  • 布局网站开发2022最新引流推广平台
  • 做电影网站的程序seo网络推广企业
  • 河北省建设工程大连网站seo
  • 成都网站建设 四川冠辰网站建设网站百度关键词排名软件
  • 校园网站建设方案策划书百度网址大全在哪里找
  • 做本地房产网站seo点击排名