我是 Spring Batch 的初学者。我正在关注这个 guide创建一个 Spring Batch 的 HelloWorld。当我尝试使用 new ClassPathXmlApplicationContext("...")
获取 Application Context 时,在带有 main 方法的类中,IDE 显示一条错误消息,指出
Unhandled exception type BeansException
即使我有一个捕获所有类型异常的 catch block ,我也无法解决该错误。引用下面的代码块:
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;
解决它,并 try catch BeansException
。虽然解决了unhandled 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", "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)
最佳答案
多亏了 Ken Bekov 对问题的评论,我才得以解决这个问题,我制定了这个解决方案,正式给出了这个问题的答案。致谢 Ken Bekov。
解决方案:
问题是由于构建路径中包含不同版本的 .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 文件后,new ClassPathXmlApplicationContext("...");
关于java - "Unhandled exception type BeansException"如何解决,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35935178/