【问题标题】:Spring batch java based configuration autowire not workingSpring批处理基于Java的配置自动装配不起作用
【发布时间】: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);
    }
}

【问题讨论】:

    标签: java servlets spring-batch spring-java-config


    【解决方案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 的对象。
    猜你喜欢
    • 1970-01-01
    • 2017-05-27
    • 1970-01-01
    • 2019-07-23
    • 2013-03-13
    • 2014-09-01
    • 2017-01-19
    • 2016-06-06
    • 2018-03-08
    相关资源
    最近更新 更多