×

commandlinerunner

commandlinerunner(SpringBoot的CommandLineRunner 怎么启动不了)

admin admin 发表于2023-11-17 22:37:59 浏览33 评论0

抢沙发发表评论

本文目录

SpringBoot的CommandLineRunner 怎么启动不了

实际应用中,我们会有在项目服务启动的时候就去加载一些数据或做一些事情这样的需求。 为了解决这样的问题,spring Boot 为我们提供了一个方法,通过实现接口 CommandLineRunner 来实现。很简单,只需要一个类就可以,无需其他配置。 创建实现接口 CommandLineRunner 的类?1234567891011121314151617181920 package org.springboot.sample.runner; import org.springframework.boot.CommandLineRunner; import org.springframework.stereotype.Component; /** * 服务启动执行 * * @author 单红宇(365384722) * @create 2016年1月9日 */@Componentpublic class MyStartupRunner1 implements CommandLineRunner { @Override public void run(String... args) throws Exception { System.out.println(“》》》》》》》》》》》》》》》服务启动执行,执行加载数据等操作《《《《《《《《《《《《《“); } } Spring Boot应用程序在启动后,会遍历CommandLineRunner接口的实例并运行它们的run方法。也可以利用@Order注解(或者实现Order接口)来规定所有CommandLineRunner实例的运行顺序。如下我们使用@Order 注解来定义执行顺序。?12345678910111213141516171819202122 package org.springboot.sample.runner; import org.springframework.boot.CommandLineRunner; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; /** * 服务启动执行 * * @author 单红宇(365384722) * @create 2016年1月9日 */@Component@Order(value=2) public class MyStartupRunner1 implements CommandLineRunner { @Override public void run(String... args) throws Exception { System.out.println(“》》》》》》》》》》》》》》》服务启动执行,执行加载数据等操作 11111111 《《《《《《《《《《《《《“); } } ?12345678910111213141516171819202122 package org.springboot.sample.runner; import org.springframework.boot.CommandLineRunner; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; /** * 服务启动执行 * * @author 单红宇(365384722) * @create 2016年1月9日 */@Component@Order(value=1) public class MyStartupRunner2 implements CommandLineRunner { @Override public void run(String... args) throws Exception { System.out.println(“》》》》》》》》》》》》》》》服务启动执行,执行加载数据等操作 22222222 《《《《《《《《《《《《《“); } } 启动程序后,控制台输出结果为:》》》》》》》》》》》》》》》服务启动执行,执行加载数据等操作 22222222 《《《《《《《《《《《《《》》》》》》》》》》》》》》》服务启动执行,执行加载数据等操作 11111111 《《《《《《《《《《《《《根据控制台结果可判断,@Order 注解的执行优先级是按value值从小到大顺序。以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

springboot 如何实现init

写一个类实现CommandLineRunner接口,在实现方法中写需要初始化的代码即可。如果多个CommandLineRunner时可以通过Order指定初始化的顺序

Commandline 22是什么

Commandline22是一款可以帮助用户创建PDF的软件。commandline22这款软件用户可以手动输入文本内容,也可以打开已经存在的PDF文件重新里面的内容,内置文本标注工具,绘图工具,图章工具,注解工具,让用户在添加内容,输入诠释方法方面得到最佳的辅助设计功能等等。

Commandline22用途

在使用Commandline22构建项目时,通常有一些预先数据的加载。Commandline22提供了一个简单的方式来实现CommandLineRunner。CommandLineRunner是一个接口,我们需要时,只需实现该接口就行。

存在多个加载的数据,可以用Order注解来排序,定义了一个数据加载类MyStartupRuner1,排序为2,以及另一个数据加载类MyStartupRunner2,排序为1。

如何启动MongoDB的WEB界面

让我们构建一个简单的EmployeeManager Web服务,我们将使用它来演示与MongoDB连接的HATEOAS。为了引导我们的应用程序,我们将使用Spring Initializr。我们将使用Spring HATEOAS和Spring Data MongoDB作为依赖项。你应该看到类似下图2所示的内容。  图2 :引导应用程序配置完成后,下载zip并将其作为Maven项目导入你喜欢的IDE中。 首先,让我们配置application.properties。要获得MongoDB连接,spring.data.mongodb.host= //Mongo server host

spring.data.mongodb.port= //Mongo server portspring.data.mongodb.username= //Login userspring.data.mongodb.password= //Passwordspring.data.mongodb.database= //Database name

一般来说,如果所有内容都是全新安装的,并且你没有更改或修改任何Mongo属性,则只需提供一个数据库名称(已经通过GUI创建了一个数据库名称)。

spring.data.mongodb.database = EmployeeManager

另外,为了启动Mongo实例,作者创建了一个.bat,它指向安装文件夹和数据文件夹。它是这样的:“C:\Program Files\MongoDB\Server\3.6\bin\mongod“ --dbpath D:\Inther\EmployeeManager\warehouse-data\db 现在,我们来快速创建模型。这里有两个模型,员工模型和部门模型。检查它们,确保有没有参数、getter、setter、equals方法和hashCode生成的构造函数。(不用担心,所有代码都在GitHub上,你可以稍后查看它:        

  • 图3 :检索JSON中的部门列表       
  • 图4:JSON中添加新员工