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

摄影作品欣赏网站推荐电子邮件营销

摄影作品欣赏网站推荐,电子邮件营销,电子商务网站建设解决方案,电商购物CommandLineRunner 和 ApplicationRunner 都是 Spring Boot 中用于在应用启动后执行特定逻辑的接口。它们的主要区别在于传递的参数类型和执行顺序。下面我将为您详细解释它们的用途、使用案例以及执行顺序。 CommandLineRunner CommandLineRunner 是一个接口,它有…

CommandLineRunnerApplicationRunner 都是 Spring Boot 中用于在应用启动后执行特定逻辑的接口。它们的主要区别在于传递的参数类型和执行顺序。下面我将为您详细解释它们的用途、使用案例以及执行顺序。

CommandLineRunner

CommandLineRunner 是一个接口,它有一个方法 run,接受一个类型为 String[] 的参数,表示应用启动时传递的命令行参数。通常用于执行一些与命令行参数相关的初始化操作。

使用案例:

假设您的应用需要根据命令行参数初始化一些配置,您可以使用 CommandLineRunner 来实现:

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Component;@SpringBootApplication
public class MyApplication {public static void main(String[] args) {SpringApplication.run(MyApplication.class, args);}
}@Component
class MyCommandLineRunner implements CommandLineRunner {@Overridepublic void run(String... args) throws Exception {// 根据命令行参数执行初始化操作if (args.length > 0 && args[0].equals("init")) {System.out.println("执行初始化操作");}}
}

在这个例子中,如果您在启动应用时传递了命令行参数 “init”,MyCommandLineRunnerrun 方法将会执行相应的初始化操作。

ApplicationRunner

ApplicationRunner 是一个接口,也有一个方法 run,接受一个类型为 ApplicationArguments 的参数,主要目的是在应用程序上下文加载之后运行一些代码。

使用案例:

假设您的应用需要在应用程序上下文加载之后运行一些代码,您可以使用 ApplicationRunner

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Component;@SpringBootApplication
public class MyApplication {public static void main(String[] args) {SpringApplication.run(MyApplication.class, args);}
}@Component
class MyApplicationRunner implements ApplicationRunner {@Overridepublic void run(ApplicationArguments args) throws Exception {// 执行初始化操作}
}

执行顺序的定义:

在 Spring Boot 中,CommandLineRunnerApplicationRunner 的执行顺序是通过 @Order 注解或 Ordered 接口来定义的。通过这些方式,您可以明确指定执行的顺序,或者让 Spring Boot 自动根据数字值进行排序。

以下是一个示例,展示了如何使用 @Order 注解来定义执行顺序:

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;@SpringBootApplication
public class MyApplication {public static void main(String[] args) {SpringApplication.run(MyApplication.class, args);}
}@Component
@Order(2)
class MyCommandLineRunner1 implements CommandLineRunner {@Overridepublic void run(String... args) throws Exception {System.out.println("MyCommandLineRunner1 执行");}
}@Component
@Order(1)
class MyCommandLineRunner2 implements CommandLineRunner {@Overridepublic void run(String... args) throws Exception {System.out.println("MyCommandLineRunner2 执行");}
}

在这个示例中,MyCommandLineRunner2run 方法会在 MyCommandLineRunner1run 方法之前执行,因为它们分别使用了不同的 @Order 值。

总之,CommandLineRunnerApplicationRunner 接口都是用于在 Spring Boot 应用启动后执行特定逻辑的强大工具。您可以根据需要使用它们来执行初始化操作、业务逻辑或其他自定义操作,并通过 @OrderOrdered 接口来控制它们的执行顺序。

http://www.hkea.cn/news/416672/

相关文章:

  • 网站如何做内链seo高手是怎样炼成的
  • 设计师个人网站建设怎样注册一个自己的平台
  • 徐州营销网站建设产品线上推广渠道
  • 绍兴市网站建设公司企业官网搭建
  • 关于网页设计的网站免费发布信息网站大全
  • 郑州新闻头条seo基础教程
  • 做网站比较大的公司朔州seo
  • 如何制作私人网站福州专业的seo软件
  • 做网站主流技术南宁在哪里推广网站
  • 老板让我做网站负责人微博营销软件
  • 教我做网站百度打开
  • 网站开发时如何兼容电商运营是做什么的
  • 河北建设银行石家庄分行招聘网站怎么申请自己的网络平台
  • vs2008 做网站搜索引擎的工作原理是什么
  • 东莞常平做网站公司app营销策划方案
  • 爱用建站 小程序重庆网站制作公司
  • 网站建设小企业案例漯河网络推广哪家好
  • wordpress 清空回收站合肥网站优化软件
  • 电站建设招聘网站智推教育seo课程
  • 做静态网站选用什么服务器站长素材网站
  • 网站建设先做前台还是后台百度认证是什么
  • 广州专业做crm系统的供应商seo网站培训班
  • 景安建网站企业网站seo方案案例
  • 山东滕州疫情最新消息今天i长沙官网seo
  • 公司做网站买域名之后做什么百度一下你就知道手机版
  • 北京婚恋网站哪家最好企业推广宣传方式
  • 国发网站建设西安做网站公司
  • 网站推广服务合同简述网络营销的主要方法
  • 信息门户网站是什么成人计算机培训机构哪个最好
  • 网站建设公司 中企动力公司东莞商城网站建设