报错情况
1月 08, 2021 6:14:16 下午 org.springframework.context.support.AbstractApplicationContext refresh 警告: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [beanPostProcessorExample] for bean with name 'helloWorld' defined in class path resource [Beans.xml]; nested exception is java.lang.ClassNotFoundException: beanPostProcessorExample Exception in thread "main" org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [beanPostProcessorExample] for bean with name 'helloWorld' defined in class path resource [Beans.xml]; nested exception is java.lang.ClassNotFoundException: beanPostProcessorExample at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1382) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineTargetType(AbstractAutowireCapableBeanFactory.java:663) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:630) at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1491) at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1014) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:828) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:865) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:548) at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:144) at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:85) at beanPostProcessorExample.MainApp.main(MainApp.java:7) Caused by: java.lang.ClassNotFoundException: beanPostProcessorExample at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:606) at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:168) at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522) at org.springframework.util.ClassUtils.forName(ClassUtils.java:275) at org.springframework.beans.factory.support.AbstractBeanDefinition.resolveBeanClass(AbstractBeanDefinition.java:437) at org.springframework.beans.factory.support.AbstractBeanFactory.doResolveBeanClass(AbstractBeanFactory.java:1430) at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1374) ... 10 more
工程文件
错误原因
在看到报错信息中
java.lang.ClassNotFoundException: beanPostProcessorExample
我查看了所有用到该包的代码,最后发现Beans.xml中的class属性缺失类名
于是我补充了类名
运行成功!
结论:查看错误信息内造成原因(Caused by ...),如果是缺失类,可以查看所有用到类的代码,添加即可。