当前位置: 首页 > 知识库问答 >
问题:

Spring batch基于java的配置autowire不工作

单于海荣
2023-03-14

我试图将我现有的基于XML的spring批处理项目转换为基于java的配置。@Autowired对象返回null,尽管我已经在基本包中提到了componentscan。

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

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个答案

宗政学
2023-03-14

请确保您的具有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
 类似资料:
  • 我正在尝试将dwr包含在使用SpringMVC4.0.5的项目中。项目中使用基于Java的配置。对于基于java的配置,我在MVCConfiguration中使用以下代码行。 扩展类I的WebApplicationInitializer为dwr请求添加了映射。 但是在部署时,它显示错误,说明“应设置所需的‘配置器’属性”。如果配置需要任何修改,请建议如何设置配置器。 将 dwrController

  • 3.12 基于Java的容器配置 {#toc_5} 3.12.1 基本概念:@Bean 和 @Configuration {#toc_6} 最核心的是Spring支持全新的Java配置,例如@Configuration注解的类和@Bean注解的方法。 @Bean注解用来说明通过Spring IoC容器来管理时一个新对象的实例化,配置和初始化的方法。这对于熟悉Spring以XML配置的方式,@Bea

  • 只是一个一般问题,当您定义基于Java的配置Web应用程序时。即有一个类:ApplicationContext和一个WebApplicationInitializer类。 Spring如何知道它必须加载bean,因为不存在xml配置文件..tomcat如何知道没有web.xml的webapp 这是一个新手问题。我很感激。:)

  • 问题内容: 我尝试不使用任何xml。 像这样一个:转换为@Bean 问题在这里。 尝试将“ com.cloudlb.domain.User”转换为Class []无效。 错误:投放问题。 先感谢您。 问题答案:

  • 困惑: 对我来说没有代码段工作,每次我面对404,我想我错过了什么?

  • 当我使用Spring framework时,我经常看到2个术语基于Java和基于注释的配置/自动生成。 如果它们不一样,你能告诉我它们之间有什么不同吗?