如何解决"未处理的异常类型BeansException"

kar*_*sky 7 java spring spring-batch

我是Spring Batch的初学者.我按照本指南创建了一个Spring Batch的HelloWorld.在使用main方法的类中,当我尝试使用Application Context时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();
        } 
    }
Run Code Online (Sandbox Code Playgroud)

然后,我试图解决它,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();
        } 
    }
Run Code Online (Sandbox Code Playgroud)

解决此错误的正确方法是什么?

附加说明:我没有自己的名为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)
Run Code Online (Sandbox Code Playgroud)

kar*_*sky 8

感谢Ken Bekov对这个问题的评论,我能够解决这个问题并制定了这个解决方案,正式给出了这个问题的答案.应该给Ken Bekov学分.

解决方案: 问题是由于构建路径中包含的.jar文件的不同版本.应包含的.jar文件是:spring-context-4.2.5.RELEASE.jar,spring-beans-4.2.5.RELEASE.jarspring-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文件后,甚至不会出现"未处理的异常类型BeansException"错误消息 new ClassPathXmlApplicationContext("...");