1. 程式人生 > 實用技巧 >Eclipse 報錯 org.springframework.context.support.AbstractApplicationContext refresh處理

Eclipse 報錯 org.springframework.context.support.AbstractApplicationContext refresh處理

報錯情況

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 ...),如果是缺失類,可以檢視所有用到類的程式碼,新增即可。