在做SpringMVC项目时,启动maven时遇到了以下报错,看了许多博文都没解决,以下分享我的改错经历
org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [com.itheima.config.SpringConfig]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/jdbc.properties]
该报错的核心是Could not open ServletContext resource,表示无法打开ServletContext资源,经过查阅相关博文发现,导致该错误是因为SpringConfig配置类中@PropertySource的配置写错,正确写法是@PropertySource("classpath:jdbc.properties")。
还有一个报错就是
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'bookServiceImpl': Unsatisfied dependency expressed through field 'bookDao'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.itheima.dao.BookDao' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
该报错的主要意思是:bookServiceImpl bean创造错误,不能通过bookDao满足依赖表达,意思就是说bean加载失败,不能正确识别BookDao接口的注解,经过检查发现,是MyBatisConfig中mapperScannerConfigurer()方法缺少了@Bean注解。
在测试时,还遇到Could not load CacheAwareContextLoaderDelegate错误,经过查看博文发现spring-webmvc与spring-test版本要一致才行,被各种版本搞emo了。
经过本次改错的经历,我认识到了尽管报错一样,但真正的错误很可能不一样,要解决错误还要结合报错语句给出的提示,不能只是通过博文大概地去比对别人错误的解决办法。