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

中国动漫影视培训网站源码找程序员代写程序

中国动漫影视培训网站源码,找程序员代写程序,佛山大良网站建设招聘,征信中心个人信用查询在Spring Boot应用程序中#xff0c;CommandLineRunner和ApplicationRunner是两个重要的接口#xff0c;它们允许我们在应用程序启动后执行一些初始化任务。本文将介绍CommandLineRunner和ApplicationRunner的区别#xff0c;并提供代码示例和使用场景#xff0c;让我们更好…在Spring Boot应用程序中CommandLineRunner和ApplicationRunner是两个重要的接口它们允许我们在应用程序启动后执行一些初始化任务。本文将介绍CommandLineRunner和ApplicationRunner的区别并提供代码示例和使用场景让我们更好地理解和使用这两个接口。 CommandLineRunner和ApplicationRunner的用法 CommandLineRunner接口: 方法签名: void run(String... args)参数类型: 字符串数组表示应用程序启动时传递的命令行参数执行时机: 在Spring上下文准备好之后但在调用ApplicationRunner之前执行。 ApplicationRunner接口: 方法签名: void run(ApplicationArguments args)参数类型: ApplicationArguments对象提供对应用程序启动参数的更高级别访问执行时机: 在CommandLineRunner之后执行。 这两个接口的目的是允许开发人员在应用程序启动完成后执行一些自定义的任务例如加载初始化数据、执行数据迁移、启动后台任务等它们都实现了org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean注解这意味着只有在没有其他类型的Bean定义的情况下才会自动配置它们。 我们可以通过以下两种方式使用CommandLineRunner和ApplicationRunner 通过实现接口并将其作为Spring Bean注册 创建一个类并实现CommandLineRunner或ApplicationRunner接口实现接口的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 {// 在这里编写您的自定义逻辑} }通过使用SpringApplication的run方法参数进行注册 在SpringApplication.run方法中将实现了CommandLineRunner或ApplicationRunner接口的实例作为参数传递给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方法将被调用并且我们可以在其中编写我们的自定义逻辑。可以根据我们的需求选择使用CommandLineRunner或ApplicationRunner接口。 CommandLineRunner和ApplicationRunner的区别 参数不同 从上面的代码示例中我们可以看到CommandLineRunner和ApplicationRunner的一个主要的不同就是它们的run方法的参数类型不同。CommandLineRunner的run方法接收一个String数组它是直接从命令行传入的参数比如java -jar myapp.jar arg1 arg2那么arg1和arg2就会被传入到run方法中。而ApplicationRunner的run方法接收一个ApplicationArguments对象它不仅包含了命令行传入的参数还包含了其他的应用程序参数比如--spring.profiles.activedev这些参数可以通过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并且分别指定了它们的优先级。那么它们的执行顺序是 FirstCommandLineRunnerSecondCommandLineRunnerFirstApplicationRunnerSecondApplicationRunner 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.zqtcl.cn/news/578715/

相关文章:

  • 网站解析后怎么解决方法淘宝网站建设方案模板
  • 淘宝客可以自己做网站推广吗营销网络建设怎么写
  • 上海高端网站制作广告设计培训课程
  • 互联网站平台有哪些建筑工程教育网官网
  • 广告传媒公司哪家好职场seo是什么意思
  • 番禺龙美村做网站博山区住房和城乡建设局网站
  • 山东网站建设xywlcnwordpress如何创建导航
  • 直接用ip访问网站网站开发常用字体
  • 江西省城乡建设培训网 官方网站杭州十大软件公司
  • 建设网站需要什么设备南昌购物网站制作
  • 做家具的网站工作单位怎么填
  • 福州建设银行官网招聘网站山西建设公司网站
  • 集团网站建设方案中卫网站推广制作
  • 射阳网站建设电商运营团队结构图
  • 有没有女的做任务的网站计算机网站开发专业
  • 怎么样开始做网站网站建设 营业执照 经营范围
  • 威海做网站网站建设方案书 模版
  • 泗阳做网站南昌建设
  • 做企业网站用什么软件深圳制作企业网站
  • 大连微信网站开发兰州网站建设模板
  • 建设项目安监备案网站外贸 网站 seo
  • 企慕网站建设网络推广合肥市网站制作
  • 做空比特币网站大气简约企业网站模板免费下载
  • 坪山网站建设行业现状做网站能月入10万
  • 个人网站有什么内容广西网站建设推广
  • 安徽教育云网站建设网站seo诊断的主要内容
  • 网站建设例子开发工具宏怎么使用
  • 新乡做网站公司哪个地区网站建设好
  • 网站模板怎么编辑网站定制化
  • 利于优化的网站网络科技公司怎么赚钱