I am trying to convert my existing XML based spring batch project to java based configuration. @Autowired objects returning null even though I have mentioned componentscan at the base package.
I have tried with below similar code in my project, all objects with @Autowired returning null. UtilClass object not autowired in my RootServlet, getting a nullpointer exception
web.xml
<context-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.batch.sample.AppConfig</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>RootServlet</servlet-name>
<servlet-class>com.batch.sample.servlet.RootServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RootServlet</servlet-name>
<url-pattern>/execute</url-pattern>
</servlet-mapping>
AppConfig.java
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableBatchProcessing
@ComponentScan("com.batch.sample")
public class AppConfig {
@Autowired
private JobBuilderFactory jobs;
@Autowired
private StepBuilderFactory steps;
//Job beans not included
}
RootServlet.java
package com.batch.sample.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.batch.sample.util.UtilClass;
@Component
public class RootServlet extends HttpServlet {
@Autowired
UtilClass utilClass;
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
JobLauncher jobLauncher = utilClass.getJobLauncherObject();
Job job = utilClass.getJobObject();
try {
jobLauncher.run(job, new JobParameters());
} catch (Exception e) {
e.printStackTrace();
}
}
}
UtilClass.java
package com.batch.sample.util;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
@Component
public class UtilClass {
public Job getJobObject() {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
return context.getBean("dataLoaderJob",Job.class);
}
public JobLauncher getJobLauncherObject() {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
return context.getBean(JobLauncher.class);
}
}
Please make sure your class with main method always in the parent/root package
For example