gpt4 book ai didi

java - 如何从 Spring Controller 启动 CommandLineRunner

转载 作者:行者123 更新时间:2023-12-02 11:02:29 25 4
gpt4 key购买 nike

我在启动的顶级包中有一个主要的 Spring Boot 应用程序,在子包中有不同的 Controller :

ca.example.batch.MainBatchApplication
ca.example.batch.job1.Job1Controller (/batch/startJob1)
ca.example.batch.job2.Job2Controller (/batch/startJob2)

我可以通过访问 URL 来启动 Spring Batch:http://localhost:8080/batch/startJob1http://localhost:8080/batch/startJob2 .

但是,在另一个包中我有:

ca.example.batch.job3.Job3Controller
ca.example.batch.job3.Job3Application

...这不是 Spring Batch,而是 Spring CommandLineRunner。我想知道是否有一种方法可以启动 MainBatchApplication 而无需自动启动 CommandLineRunner,而是通过 Controller 运行它,即 http://localhost:8080/batch/startJob3 .

我正在查看的 Controller 代码是:

package ca.example.batch.job3;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class Job3Controller {

@RequestMapping("/batch/startJob3")
public String handle() throws Exception {

Job3Application app = new Job3Application();

Logger logger = LoggerFactory.getLogger(this.getClass());

logger.info("app: " + app);

String args = "";
app.run(args);

return "COMPLETE";
}
}

Job3Application 是:

package ca.example.batch.job3;

import static java.lang.System.exit;
import java.util.List;
import javax.sql.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.Banner;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.annotation.Import;
import ca.example.batch.common.CommonLibraryReference;
import ca.example.batch.common.domain.WasHost;
import ca.example.batch.common.svc.WasHostService;

@SpringBootApplication
@Import(CommonLibraryReference.class)
public class Job3Application implements CommandLineRunner {

private final Logger logger = LoggerFactory.getLogger(Job3Application.class);

@Autowired
public DataSource dataSource;

@Autowired
public WasHostService wasHostService;

public Job3Application() {
}

public static void main(String[] args) throws Exception {
new SpringApplicationBuilder(Job3Application.class)
.web(WebApplicationType.NONE)
.bannerMode(Banner.Mode.OFF)
.run(args);
}

@Override
public void run(String... strings) throws Exception {
logger.info("Loading data...");

logger.info("wasHostService: " + wasHostService);
List<WasHost> hostList = wasHostService.findAll();
if (!hostList.isEmpty()) {
for (WasHost host : hostList) {
logger.info("hostname: " + host.getHostname());
}
} else {
logger.info("No hosts found in database. Aborting data collection.");
exit(0);
}
}
}

我在日志中收到的错误是:

""2018-07-07 12:56:09 [http-nio-9191-exec-1] INFO  o.a.c.c.C.[Tomcat].[localhost].[/] - Initializing Spring FrameworkServlet 'dispatcherServlet'
""2018-07-07 12:56:09 [http-nio-9191-exec-1] INFO c.e.b.job3.Job3Controller - app: ca.example.batch.job3.Job3Application@472d7ac
""2018-07-07 12:56:09 [http-nio-9191-exec-1] INFO c.e.b.job3.Job3Application - Loading data...
""2018-07-07 12:56:09 [http-nio-9191-exec-1] INFO c.e.b.job3.Job3Application - wasHostService: null
""2018-07-07 12:56:09 [http-nio-9191-exec-1] ERROR o.a.c.c.C.[.[.[.[dispatcherServlet] - Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
"java.lang.NullPointerException: null
at ca.example.batch.job3.Job3Application.run(Job3Application.java:47)
at ca.example.batch.job3.Job3Controller.handle(Job3Controller.java:21)

...当我启动 Controller 请求时。

如果这不是正确的方法,请提出建议。

基本上,我想做的是从 Controller 内启动 main() ,但使用 MainBatchApplication 运行时来运行它(如果这有意义吗?)。当程序完成时,它将返回代码发送回 Controller 并显示在浏览器中。

谢谢你,乔伊

最佳答案

ca.example.batch.MainBatchApplication 是您启动的主应用程序。因此它会扫描ca.example.batch包中的所有组件。这意味着它应该检测到ca.example.batch.job3.Job3Application,因此您应该能够在Job3Controller

@Autowire它>

这应该有效:

@Controller
public class Job3Controller {
private final Job3Application job3Application;

public Job3Controller (Job3Application job3Application){
this.job3Application = job3Application;
}

@RequestMapping("/batch/startJob3")
public String handle() throws Exception {
String[] args = ...
this.job3Application.run(args);
return "COMPLETE";
}

....
}

关于java - 如何从 Spring Controller 启动 CommandLineRunner,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51219065/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com