gpt4 book ai didi

java - 基于 Spring Batch Java 的配置 Autowiring 不起作用

转载 作者:行者123 更新时间:2023-12-02 05:49:17 25 4
gpt4 key购买 nike

我正在尝试将现有的基于 XML 的 Spring Batch 项目转换为基于 Java 的配置。 @Autowired 对象返回 null,即使我已经在基础包中提到了 elementscan。

我在我的项目中尝试过使用以下类似的代码,所有带有 @Autowired 的对象都返回 null。 UtilClass 对象未在我的 RootServlet 中 Autowiring ,出现空指针异常

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

最佳答案

请确保您的具有 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

关于java - 基于 Spring Batch Java 的配置 Autowiring 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56064934/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com