Spring批处理基于Java的配置自动装配不起作用

Posted

技术标签:

【中文标题】Spring批处理基于Java的配置自动装配不起作用【英文标题】:Spring batch java based configuration autowire not working 【发布时间】:2019-09-27 14:46:11 【问题描述】:

我正在尝试将我现有的基于 XML 的 spring 批处理项目转换为基于 java 的配置。 @Autowired 对象返回 null,即使我在基础包中提到了 componentscan。

我在我的项目中尝试过使用以下类似代码,所有带有 @Autowired 的对象都返回 null。 UtilClass 对象未在我的 RootServlet 中自动装配,得到一个空指针异常

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


【问题讨论】:

【参考方案1】:

请确保您的类与 main 方法始终在父/根包中

例如

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

【讨论】:

我的应用没有main方法,我没有使用spring boot【参考方案2】:

您正在UtilClass 中重新创建一个空的应用程序上下文,它不知道您在com.batch.sample.AppConfig 中定义的 Spring Batch bean。由于UtilClass由Spring管理(注解@Component),你可以注入应用上下文:

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

【讨论】:

我的问题 UtilCalss 对象本身在 RootServlet 类中为 null。我的应用程序有很多我不能在所有地方都使用 ApplicationContext 的对象。

以上是关于Spring批处理基于Java的配置自动装配不起作用的主要内容,如果未能解决你的问题,请参考以下文章

spring的装配

Spring框架bean的配置:基于注解的配置,Autowired 自动装配 Bean,泛型依赖注入

Spring自动装配(基于注解)

Spring学习bean装配详解之 XML方式配置

spring学习总结——装配Bean学习三(xml装配bean)

基于XML配置Spring的自动装配