[英]@Autowired annotation without spring boot
我正在开发一个每天只在容器内运行一次的 JAR 文件,因此它不需要 Spring 启动功能。
但仍然利用 spring 的特性,我有一个应用程序配置
@Configuration
@ComponentScan("com.arun.fp")
public class ApplicationConfig {
}
然后这是我的主要课程,它绝对工作正常
package com.arun.fp;
import com.arun.fp.utils.JobUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import java.time.Instant;
public class JobMain {
private ApplicationContext applicationContext;
public JobMain(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
public static void main(String... args) {
var applicationContext = new AnnotationConfigApplicationContext(ApplicationConfig.class);
new JobMain(applicationContext).startJob();
}
public void startJob() {
var jobUtils = applicationContext.getBean(JobUtils.class);
var jobId = jobUtils.generateJobId();
try {
System.out.println(" -------------- ");
var startTime = Instant.now();
System.out.println("Job with ID: " + jobId + " Started at " + startTime);
/* var fileProcessingJob = new FileProcessorJob();
fileProcessingJob.startProcess();*/
Thread.sleep(2000);
var endTime = Instant.now();
System.out.println("Job with ID: " + jobId + " Ended at " + endTime);
var duration = jobUtils.getJobDurationInSeconds(startTime, endTime);
System.out.println("Job with ID: " + jobId + " took " + duration + " seconds to complete the job");
} catch (Exception exception) {
}
}
}
但不是获取applicationContext.getBean(..)
并尝试像这样使用@Autowired
注释
@Autowired
private JobUtils jobUtils;
但它不是自动装配的,并且只会变为空..
我可以只将@Autowired
用于 Spring Boot 应用程序吗? 不适用于使用 spring 的 Java 应用程序?
当然你可以使用@Autowired
但由于你的JobMain
不是一个 Spring bean,它当然不会被自动装配。 基本上这是复制这个(更多的理由在这里。
因此,要修复使您的JobMain
成为@Component
并且不要自己构造新实例。
import java.time.Instant;
@Component
public class JobMain {
@Autowired
private JobUtils jobUtils;
public static void main(String... args) {
new AnnotationConfigApplicationContext(ApplicationConfig.class);
}
@PostConstruct // actually you are better of using a listener for instance.
public void startJob() {
var jobId = jobUtils.generateJobId();
try {
System.out.println(" -------------- ");
var startTime = Instant.now();
System.out.println("Job with ID: " + jobId + " Started at " + startTime);
/* var fileProcessingJob = new FileProcessorJob();
fileProcessingJob.startProcess();*/
Thread.sleep(2000);
var endTime = Instant.now();
System.out.println("Job with ID: " + jobId + " Ended at " + endTime);
var duration = jobUtils.getJobDurationInSeconds(startTime, endTime);
System.out.println("Job with ID: " + jobId + " took " + duration + " seconds to complete the job");
} catch (Exception exception) {
}
}
}
但是,正如我在评论中已经提到的,没有什么可以阻止您将 Spring Boot 用于 CLI 应用程序。 只是不要包含网络内容,它会做你想做的事(开始工作,完成后会停止)。
假设您的JobMain
在com.arun.fp
您可以执行以下操作。
ApplicationConfig
类(不需要)。JobMain
spring-boot-starter
作为依赖java -jar your-job.jar
运行(它现在将启动,完成工作并完成)。@SpringBootApplication
public class JobMain {
public static void main(String... args) {
SpringApplication.run(JobMain.class, args);
}
@Bean
public CommandLineRunner jobRunner(JobUtils jobUtils) {
return (args) -> {
var jobId = jobUtils.generateJobId();
try {
System.out.println(" -------------- ");
var startTime = Instant.now();
System.out.println("Job with ID: " + jobId + " Started at " + startTime);
/* var fileProcessingJob = new FileProcessorJob();
fileProcessingJob.startProcess();*/
Thread.sleep(2000);
var endTime = Instant.now();
System.out.println("Job with ID: " + jobId + " Ended at " + endTime);
var duration = jobUtils.getJobDurationInSeconds(startTime, endTime);
System.out.println("Job with ID: " + jobId + " took " + duration + " seconds to complete the job");
} catch (Exception exception) {
}
}
}
这将启动作业,因为CommandLineRunner
将在所有 bean 启动后执行。 当CommandLineRunner
完成它的工作时,应用程序将停止,因为没有更多进程保持活动状态。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.