异常
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'xxxTask': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.xxxxx..ProductService com.xxxxx.xxxTask.productService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'productService' defined in URL [jar:file:/C:/apache-tomcat-8.5.65-windows-x64/apache-tomcat-8.5.65/webapps/xxx_war/WEB-INF/lib/core-1.0-SNAPSHOT.jar!/com/xxxxx/xx/service/ProductServiceImpl.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanInitializationException: com.xxxxx.xx.dao.data.Dao can't get a sessionFactory
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:287)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:313)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:871)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:813)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:730)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:485)
... 62 more
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.xxxxx.xx.service.ProductService com.xxxxx.xxxTask.productService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'productService' defined in URL [jar:file:/C:/apache-tomcat-8.5.65-windows-x64/apache-tomcat-8.5.65/webapps/xxx_war/WEB-INF/lib/core-1.0-SNAPSHOT.jar!/com/xxxxx/xx/service/ProductServiceImpl.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanInitializationException: com.xxxxx.xx.dao.data.Dao can't get a sessionFactory
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:513)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:92)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:284)
... 71 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'productService' defined in URL [jar:file:/C:/apache-tomcat-8.5.65-windows-x64/apache-tomcat-8.5.65/webapps/xxx_war/WEB-INF/lib/core-1.0-SNAPSHOT.jar!/com/xxxxx/xx/service/ProductServiceImpl.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanInitializationException: com.xxxxx.xx.dao.data.Dao can't get a sessionFactory
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:527)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:871)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:813)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:730)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:485)
... 73 more
Caused by: org.springframework.beans.factory.BeanInitializationException: com.xxxxx.xx.dao.data.Dao can't get a sessionFactory
at com.xxxxx.framework.dynamicdao.AutoInjectDynamicDaoBeanPostProcessor.getRealSessionFactory(AutoInjectDynamicDaoBeanPostProcessor.java:97)
at com.xxxxx.framework.dynamicdao.AutoInjectDynamicDaoBeanPostProcessor.postProcessBeforeInitialization(AutoInjectDynamicDaoBeanPostProcessor.java:50)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:394)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1448)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
... 82 more
原因
查看代码之后,发现注入代码并没有问题,而报错最终出现在 Dao 类,并且提示 BeanInitializationException: com.xxxxx.xx.dao.data.Dao can't get a sessionFactory
,既然代码没有问题,而 Dao 类一般跟数据库有关,猜测是 xml
相关配置文件中有什么问题。最终发现 Dao 类所在包并没有配置扫描。
解决
在配置文件中进行配置。
注:并没有讲具体该如何配置,因为这不重要,不同的项目中实际配置项并不相同,这里最重要是一个思路,由 Dao 类的 sessionFactory 联想到配置文件可能有问题的思路。