java - @Autowired 注解没有弹簧启动
问题描述
我正在开发一个 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
的 an@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 - 用作
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
完成它的工作时,应用程序将停止,因为没有更多的进程保持活动。
推荐阅读
- linux - 关于在shell编程中保存表达式的值
- mysql - 安装 MySQL 但进入应用配置时无法启动服务器
- julia - Julia中的滑动窗口功能
- r - 如何将变量添加到 R 中的字符串中?我基本上是在尝试参数化字符串,就像在 SQL 中一样
- python - 如何在 Trie 中进行深度搜索?
- sql - 将值替换为不同的值
- angular - 当鼠标悬停在 mat-menu 上时,显示菜单的图标消失
- java - 如何在 Android Studio 中的主活动以外的默认活动中单击按钮上的链接?
- javascript - 父组件和子组件之间的传递函数问题
- python - os.system() 返回的文本末尾的 0 代表什么?