我正在尝试将现有的基于XML的Spring Batch项目转换为基于Java的配置。即使我已经在基本软件包中提到了componentscan,@ Autowired对象仍返回null。
我在项目中尝试使用下面的类似代码,所有带有@Autowired的对象均返回null。未在我的RootServlet中自动接线的UtilClass对象,出现空指针异常
web.xml
ScreenshotHelper sh = new ScreenshotHelper(this); // assuming you create the helper as part of your form codebehind
AppConfig.java
<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>
RootServlet.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
}
UtilClass.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();
}
}
}
答案 0 :(得分:0)
请确保带有main方法的类始终位于父/根包中
例如
for (int i = 0; i < m.getRowDimension(); i++) {
System.arraycopy(mData[i], 0, newData[i], 0, mData[i].length);
}
答案 1 :(得分:0)
您正在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);
}
}