我是Spring Batch的初学者.我正在关注这个guide以创建一个Spring Batch的HelloWorld.在使用main方法的类中,当我尝试使用新的ClassPathXmlApplicationContext(“…”)来获取Application Context时,IDE会显示一条错误消息
Unhandled exception type 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错误已解决,但出现了另一条错误消息:
No exception of type BeansException can be thrown; an exception type
must be a subclass of throwable
请参阅下面的代码块:
public static void main(String args[]) {
try {
AbstractApplicationContext applicationContext = new ClassPathXmlApplicationContext("simpleJob.xml");
JobParametersBuilder builder = new JobParametersBuilder();
builder.addString("Date",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)
解:
问题是由于构建路径中包含的.jar文件的不同版本.应该包含的.jar文件是:spring-context-4.2.5.RELEASE.jar,spring-beans-4.2.5.RELEASE.jar和spring-core-4.2.5.RELEASE.jar(注意相同的版本号码 – 4.2.5).
至于spring-batch-core-3.0.6.RELEASE.jar,spring-batch-infrastructure-3.0.6.RELEASE.jar等,它们不一定需要具有相同的版本号(4.2.5).
在包含正确的.jar文件之后,对于新的ClassPathXmlApplicationContext(“…”),甚至不会出现“未处理的异常类型BeansException”错误消息;