我是Spring Batch的初学者。我正在遵循这个指南创建Spring Batch的HelloWorld:guide。在具有main方法的类中,当我尝试使用
然后,我尝试通过
new ClassPathXmlApplicationContext("...")
获取应用程序上下文时,IDE显示错误消息:
未处理的异常类型BeansException
即使我有一个捕获所有类型异常的catch块,我也无法解决该错误。请参考以下代码块:
public static void main(String args[]) {
try {
//error message appears here
AbstractApplicationContext applicationContext = new ClassPathXmlApplicationContext("simpleJob.xml");
JobParametersBuilder builder = new JobParametersBuilder();
builder.addString("Date", "12/02/2011");
jobLauncher.run(job, builder.toJobParameters());
JobExecution jobExecution = jobRepository.getLastJobExecution(job.getName(), builder.toJobParameters());
System.out.println(jobExecution.toString());
}
catch(Exception e) {
e.printStackTrace();
}
}
然后,我尝试通过
import org.springframework.beans.BeansException;
导入并捕捉BeansException
来解决它。虽然未处理的BeansException错误被解决了,但出现了另一个错误消息:
请参考下面的代码块:不能抛出BeansException类型的异常;异常类型必须是Throwable的子类
public static void main(String args[]) {
try {
AbstractApplicationContext applicationContext = new ClassPathXmlApplicationContext("simpleJob.xml");
JobParametersBuilder builder = new JobParametersBuilder();
builder.addString("Date", "12/02/2011");
jobLauncher.run(job, builder.toJobParameters());
JobExecution jobExecution = jobRepository.getLastJobExecution(job.getName(), builder.toJobParameters());
System.out.println(jobExecution.toString());
}
//error message appears here
catch(BeansException e) {
//do something
}
catch(Exception e) {
e.printStackTrace();
}
}
如何正确解决这个错误?
附注:我没有自己命名为BeansException的类。
编辑:堆栈跟踪(选择错误选项):
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
No exception of type BeansException can be thrown; an exception type must be a subclass of Throwable
at SpringBatchHelloWorld.BatchLauncher.main(BatchLauncher.java:29)
org.springframework.beans.BeansException
。 - karanskyspring-context
的4.2.4.RELEASE
版本,则spring-core
也应该是4.2.4.RELEASE
版本。 - Ken Bekov