SpringBoot应用程序无REST控制器

huangapple go评论81阅读模式
英文:

SpringBoot application without a rest controller

问题

我对Spring还不熟悉,目前正在开发一个Spring应用程序来在两个数据库之间进行数据迁移。

我的问题是,它将如何运行?我的意思是,如何使用Spring让Spring知道迁移过程将从哪里开始?

例如,假设我有3个表(Customer,Product和Invoices)。

我将首先处理迁移所有客户数据的类,然后是产品,最后是发票。

我对Spring在启动时如何管理这一点感到困惑。

提前感谢。

英文:

i´m new to spring and i´m currently working on a spring application to migrate data between 2 databases.

My question is, how is it going to run?? I mean, using spring, how to let spring knows where the migration process will start?

For example, assuming that i have 3 tables (Customer, Product and Invoices).

I´ll work on the classes to migrate first all data from customer, then product and finally invoices.

I´m confused on how spring will manage this when it starts.

Tks in advance.

答案1

得分: 0

正如@Marged建议的那样,CommandLineRunner应用程序将刚好实现您要寻找的功能:

@SpringBootApplication
public class YourMigrationApplication implements CommandLineRunner {
 
    public static void main(String[] args) {
        // 应用程序启动
        SpringApplication.run(SpringBootConsoleApplication.class, args);
        // 迁移完成
    }
 
    @Override
    public void run(String... args) {
        // 迁移客户数据
        // 迁移产品数据
        // 迁移发票数据
    }
}

当迁移发票数据步骤完成时,您的应用程序将退出,因为没有其他任务需要运行。(您也没有控制器等继续监听的内容)。

英文:

As @Marged suggested, A CommandLineRunner application would just achieve what you're looking for:

@SpringBootApplication
public class YourMigrationApplication implements CommandLineRunner {
 
    public static void main(String[] args) {
        // Application starts
        SpringApplication.run(SpringBootConsoleApplication.class, args);
        // Migration finished
    }
 
    @Override
    public void run(String... args) {
        // Migrate Customer data
        // Migrate Product data
        // Migrate Invoice data
    }
}

When the migrate invoice data step completes, your application will exit since there's nothing left to run. (And you don't have any controllers etc. to continue listen to).

huangapple
  • 本文由 发表于 2020年8月15日 04:35:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/63419691.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定