I am developing a JAR file that would be running within a container only once per day and hence it doesnt require Spring boot features.
But still to leverage spring's feature, am having an application-config this way
@Configuration
@ComponentScan("com.arun.fp")
public class ApplicationConfig {
}
and then this is my main class and It is absolultey working fine
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) {
}
}
}
But instead of getting the applicationContext.getBean(..)
and trying to use @Autowired
annotation like this
@Autowired
private JobUtils jobUtils;
but it is not autowiring and am getting null only..
Can i use @Autowired
only for Spring Boot app ? not for a java app that uses spring ??