spring – 找不到类型为[org.hibernate.SessionFactory]的匹配bean用于依赖

前端之家收集整理的这篇文章主要介绍了spring – 找不到类型为[org.hibernate.SessionFactory]的匹配bean用于依赖前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我是Spring MVC的初学者.我遇到了一个问题“找不到类型为[org.hibernate.SessionFactory]的匹配bean依赖:预计至少有一个bean可以作为此依赖项的autowire候选者.”并花了很多时间.

我使用了maven构建管理.我的项目有一个项目,包括2个模块:springcore(jar)和springmvc(web app).而springmvc(网络应用程序)一直是依赖弹簧的核心.在模块spring核心中,我定义了所有包,如dom,dao,service及其实现,我还在src / main / resources中定义了applicationContext.xml:

applicationContext.xml中

factorybean">
            MysqLDialectsql">true

ContactDaoImpl.java

@Repository
public class ContactDaoImpl implements ContactDao {
    private Log log = LogFactory.getLog(ContactDaoImpl.class);

    @Autowired
    private SessionFactory sessionFactory;

    public List

ContactServiceImpl.java

@Service
@Transactional
public class ContactServiceImpl implements ContactService {

    @Autowired
    private ContactDao contactDao;

    @Transactional(propagation=Propagation.required)
    public List

在springmvc(web app)中,我定义了web.xml和dispatcher-servlet.xml

调度员servlet.xml中

web.xml中

当我尝试在Tomcat 6.0服务器中运行它时,这是我的问题

Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.hibernate.SessionFactory com.netvn.dao.impl.ContactDaoImpl.sessionFactory; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [org.hibernate.SessionFactory] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:502)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:84)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:282)
    ... 86 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [org.hibernate.SessionFactory] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.support.DefaultListablebeanfactory.raiseNoSuchBeanDefinitionException(DefaultListablebeanfactory.java:920)
    at org.springframework.beans.factory.support.DefaultListablebeanfactory.doResolveDependency(DefaultListablebeanfactory.java:789)
    at org.springframework.beans.factory.support.DefaultListablebeanfactory.resolveDependency(DefaultListablebeanfactory.java:703)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:474)

有人能告诉我,当我配置SessionFactory时,Web应用程序上下文无法创建SessionFactory或者出现任何问题的原因是什么.

任何支持将不胜感激.

最佳答案
一般问题是从类路径中缺少配置/类,无论您自己的库.再次检查导入和配置是否正确:

>使用类路径扫描来实现实体类的自动检测:
< bean id =“sessionFactory”class =“org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean”>
< property name =“dataSource”ref =“dataSource”/>
< property name =“packagesToScan”value =“test.package”/>
< /豆腐>
>或者最重要的是:AnnotationSessionfactorybean类需要Hibernate 3.2或更高版本,并且存在Java Persistence API和Hibernate Annotations附加组件.

参考:https://docs.spring.io/spring/docs/3.2.8.RELEASE/javadoc-api/org/springframework/orm/hibernate3/annotation/AnnotationSessionFactoryBean.html

原文链接:https://www.f2er.com/spring/432184.html

猜你在找的Spring相关文章