报错信息如下
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'employeeController': Unsatisfied dependency expressed through field 'employeeDao'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'employeeDao': Unsatisfied dependency expressed through field 'departmentDao'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'departmentDao' defined in file [E:\idea_workspace\springboot-study\springboot-03-web2\target\classes\com\lyp\dao\DepartmentDao.class]: Instantiation of bean failed; nested exception is java.lang.ExceptionInInitializerError
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:660) ~[spring-beans-5.3.8.jar:5.3.8]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640) ~[spring-beans-5.3.8.jar:5.3.8]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:119) ~[spring-beans-5.3.8.jar:5.3.8]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399) ~[spring-beans-5.3.8.jar:5.3.8]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1413) ~[spring-beans-5.3.8.jar:5.3.8]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:601) ~[spring-beans-5.3.8.jar:5.3.8]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:524) ~[spring-beans-5.3.8.jar:5.3.8]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.8.jar:5.3.8]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.8.jar:5.3.8]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.8.jar:5.3.8]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.8.jar:5.3.8]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:944) ~[spring-beans-5.3.8.jar:5.3.8]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918) ~[spring-context-5.3.8.jar:5.3.8]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) ~[spring-context-5.3.8.jar:5.3.8]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145) ~[spring-boot-2.5.2.jar:2.5.2]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:754) [spring-boot-2.5.2.jar:2.5.2]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:434) [spring-boot-2.5.2.jar:2.5.2]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:338) [spring-boot-2.5.2.jar:2.5.2]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1343) [spring-boot-2.5.2.jar:2.5.2]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1332) [spring-boot-2.5.2.jar:2.5.2]
at com.lyp.Springboot03Web2Application.main(Springboot03Web2Application.java:12) [classes/:na]
原因分析:
由于 DepartmentDao 内部写错,因此导致DepartmentDao实例化失败!!!
问题解决:
这里少写一句,导致创建 DepartmentDao 的Bean出错!
其他可能出现的错误:
- 1、可能是实现类没有加@Servcie注解
- 2.可能是启动类上面没有加@MapperScan(“com.xxx.mapper”)
- 3.可能是ServiceImpl中的dao包没有导入 @Autowired
- 4、可能是启动类缺少 @EnableAutoConfiguration
- @EnableAutoConfiguration的主要作用是:
启用Spring应用程序上下文的自动配置,尝试猜测和配置您可能需要的bean。自动配置类通常基于类路径和定义的bean应用。例如,如果你有 tomcat-embedded.jar在您的类路径中,你可能会想要一个TomcatEmbeddedServletContainerFactory(除非你定义自己的EmbeddedServletContainerFactory bean)。
当使用SpringBootApplication时,上下文的自动配置将自动启用并添加此注释,因此不会产生额外的效果。
自动配置尝试尽可能地智能化,并且在您定义更多自己的配置时将会备份。您总是可以手工排除()任何您不想应用的配置(如果您无法访问它们的话)。您也可以通过spring. autoconfigure将它们排除。排除属性。在用户定义的bean被注册之后,自动配置总是被应用。
用@ enableautoconfiguration注释的类的包,通常通过@ springbootapplication,具有特殊的意义,经常被用作“默认”。例如,它将用于扫描@ entity类。通常建议在根包中放置@ enableautoconfiguration(如果您不使用@ springbootapplication),以便可以搜索所有子包和类。
自动配置类是常规的Spring配置bean。它们使用SpringFactoriesLoader机制(对该类进行键控)定位。通常,自动配置bean都是“有条件bean”(通常使用@ conditionalonclass和@ conditionalonmissingbean注释)。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/123037.html