获取SessionFactory Bean失败:Spring依赖注入错误技术问询
你遇到的是典型的Spring依赖注入失败问题,核心是Spring容器无法找到org.hibernate.SessionFactory类型的合格Bean,导致依赖它的CategoryDAOImpl和上层的PageController都无法正常创建。先给你拆解下完整的错误链:
exception javax.servlet.ServletException: Servlet.init() for servlet dispatcher threw exception
root cause org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'pageController': Unsatisfied dependency expressed through field 'categoryDAO'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'categoryDAOImpl': Unsatisfied dependency expressed through field 'sessionFactory'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.hibernate.SessionFactory' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
root cause org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'categoryDAOImpl': Unsatisfied dependency expressed through field 'sessionFactory'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.hibernate.SessionFactory' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
root cause org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.hibernate.SessionFactory' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
结合你给出的配置信息,我整理了几个大概率的解决方向:
1. 确认父容器的SessionFactory配置被正确加载
你提到ApplicationContext.xml里配置了id="sessionFactory"的LocalSessionFactoryBean,但要注意:Spring MVC的Dispatcher-servlet.xml是子容器,它只能访问自身配置的Bean和父容器(ApplicationContext.xml)的Bean。如果父容器没被正确加载,子容器自然找不到SessionFactory。
- 解决办法:检查web.xml里是否配置了
ContextLoaderListener来加载父容器配置,示例配置如下:
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/ApplicationContext.xml</param-value> </context-param>
2. 检查LocalSessionFactoryBean的配置完整性
LocalSessionFactoryBean是FactoryBean,它负责生成SessionFactory实例并暴露给Spring容器,如果配置不完整,可能导致实例无法生成:
- 确保你的
ApplicationContext.xml里的LocalSessionFactoryBean配置包含了必要的属性,比如数据源、Hibernate配置文件、实体类扫描路径:
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="configLocation" value="classpath:hibernate.cfg.xml"/> <property name="packagesToScan" value="org.acem.sha.entity"/> <!-- 替换为你的实体类所在包 --> </bean>
- 同时确认Spring ORM和Hibernate的版本兼容(比如Spring 4.x对应Hibernate 4.x),版本不兼容也可能导致SessionFactory实例生成失败。
3. 验证依赖注入的注解配置
你在CategoryDAOImpl中使用@Autowired注入SessionFactory,可以尝试以下调整:
- 临时将
@Autowired改为@Autowired(required=false),排查是否是强制依赖导致的启动失败(这只是临时排查手段,最终要确保Bean存在); - 显式添加
@Qualifier("sessionFactory")指定Bean的id,避免容器匹配异常:
@Autowired @Qualifier("sessionFactory") private SessionFactory sessionFactory;
4. 确认组件扫描的范围和注解
Dispatcher-servlet.xml开启了org.acem.sha包的组件扫描,需要检查:
CategoryDAOImpl是否在org.acem.sha包或其子包下,不在的话Spring MVC容器无法扫描到它;CategoryDAOImpl上是否添加了@Repository注解,Spring需要通过这个注解识别DAO层Bean,否则即使扫描到类也不会注册到容器中。
最后建议你把Spring的日志级别调到DEBUG,查看容器启动时的Bean注册日志,能更精准地确认sessionFactory相关Bean是否被正确加载。
内容的提问来源于stack exchange,提问作者Ashwin Karki

