Spring batch java based configuration autowire not working

1.8k Views Asked by At

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);
    }
}

2

There are 2 best solutions below

1
Tech Guy On

Please make sure your class with main method always in the parent/root package

For example

com.google.application
           -ClassWithMainMethod
com.google.application.job
com.google.application.job.listener
com.google.application.job.service
com.google.application.job.utils
com.google.application.job.repository
com.google.application.job.components
com.google.application.job.configuration
1
Mahmoud Ben Hassine On

You are re-creating an empty application context in UtilClass which is not aware of your Spring Batch beans defined in com.batch.sample.AppConfig. Since UtilClass is managed by Spring (annotated with @Component), you can inject the application context:

@Component
public class UtilClass {

   @Autowired
   private ApplicationContext context;

   public Job getJobObject() {
      return context.getBean("dataLoaderJob",Job.class);
   }

   public JobLauncher getJobLauncherObject() {
      return context.getBean(JobLauncher.class);
   }
}