无法自动装配字段

无法自动装配字段

本文介绍了无法自动装配字段:Spring-Hibernate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我在尝试将Hibernate与Spring MVC项目集成并添加Autowired功能时遇到的异常。


$ b 异常: $ b

分派器 - 小服务程序.xml

 <! - 启用基于注释的配置 - > 
< context:annotation-config />

<! - 基本套餐 - >
< context:component-scan base-package =com.inspireme。 />

< bean class =org.springframework.web.servlet.view.InternalResourceViewResolver>
< property name =prefix>
<值> / WEB-INF / jsp /< /值>
< / property>
< property name =suffix>
<值> .jsp< /值>
< / property>
< / bean>

class =org.springframework.beans.factory.config.PropertyPlaceholderConfigurer
p:location =WEB-INF / classes / properties / database.properties >
< / bean>

< bean id =dataSource
class =org.apache.commons.dbcp.BasicDataSourcedestroy-method =close
p:driverClassName =$ {jdbc.driverClassName}
p:url =$ {jdbc.databaseurl}p:username =$ {jdbc.username}
p:password =$ {jdbc.password}>
< / bean>

< bean id =sessionFactory
class =org.springframework.orm.hibernate3.LocalSessionFactoryBean>
< property name =dataSourceref =dataSource>< / property>
< property name =configLocation>
< value> classpath:hibernate.cfg.xml< / value>
< / property>
< property name =configurationClass>
< value> org.hibernate.cfg.AnnotationConfiguration< / value>
< / property>
< property name =hibernateProperties>
<道具>
< prop key =hibernate.dialect> $ {jdbc.dialect}< / prop>
< prop key =hibernate.show_sql> true< / prop>
< /道具>
< / property>
< / bean>

< mvc:resources mapping =/ resources / **location =/ resources / inspiremetheme //>

< mvc:annotation-driven />

< tx:annotation-driven />

<! - 自动布线的开始 - >>
< bean id =userBOclass =com.inspireme.boimpl.UserBOImpl>< / bean>
< bean id =userDAOclass =com.inspireme.daoimpl.UserDAOImpl>< / bean>

< bean id =transactionManager
class =org.springframework.orm.hibernate3.HibernateTransactionManager>
< property name =sessionFactoryref =sessionFactory>< / property>
< / bean>

hibernate.cfg.xml

 <?xml version ='1.0'encoding ='utf-8'?> 
<!DOCTYPE hibernate-configuration PUBLIC
- // Hibernate / Hibernate配置DTD // EN
http://hibernate.sourceforge.net/hibernate-configuration-3.0。 DTD>

< hibernate-configuration>
< session-factory>
< mapping class =com.inspireme.dto.UserDTO>< / mapping>
< / session-factory>
< / hibernate-configuration>

控制器类

  package com.inspireme.controller; 

@Controller
@RequestMapping(/)
public class InspireMeController {

public ApplicationContext applicationContext;

private static final Logger LOGGER = Logger.getLogger(InspireMeController.class);

@Autowired
私人的UserBO userBO;

服务等级:

  package com.inspireme.boimpl; 

@Service
public class UserBOImpl implements UserBO {

private static final Logger LOGGER = Logger.getLogger(UserBOImpl.class);

@Autowired
private UserDAO userDAO;

存储库类

  package com.inspireme.daoimpl; 

@Repository
public class UserDAOImpl implements UserDAO {

@Autowired
private SessionFactory sessionFactory;

为什么我得到这个异常,因为所有的3个bean都是在dispatcher-servlet.xml中定义的文件。



我指的是以下来整合Hibernate。

解决方案

java.lang.NoClassDefFoundError:Lorg / hibernate / cache / CacheProvider 缺少jar。

不完全确定构建如何在您的案例中工作但试试 classpath:/hibernate/hibernate.cfg.xml classpath:hibernate / hibernate.cfg.xml 。或者在部署的战争中更好地检查该文件在 WEB-INF / classes 内的位置。


Following is the exception which I am getting while I am trying to integrate Hibernate with my Spring MVC project and add Autowired feature.

Exception:

Dispatcher-servlet.xml

 <!-- Enable Annotation based configuration -->
 <context:annotation-config />

 <!-- Base package -->
 <context:component-scan base-package="com.inspireme." />

 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix">
        <value>/WEB-INF/jsp/</value>
      </property>
      <property name="suffix">
        <value>.jsp</value>
      </property>
 </bean>

 <!-- Database properties configuration - START -->
 <bean id="propertyConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
    p:location="WEB-INF/classes/properties/database.properties">
 </bean>
 <!-- Database properties configuration - END -->

 <!-- Accessing DB with available credentials - START -->
 <bean id="dataSource"
    class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
    p:driverClassName="${jdbc.driverClassName}"
    p:url="${jdbc.databaseurl}" p:username="${jdbc.username}"
    p:password="${jdbc.password}">
 </bean>
 <!-- Accessing DB with available credentials - END -->

<!-- Hibernate Settings - START -->
    <bean id="sessionFactory"
       class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
       <property name="dataSource" ref="dataSource"></property>
       <property name="configLocation">
           <value>classpath:hibernate.cfg.xml</value>
       </property>
       <property name="configurationClass">
           <value>org.hibernate.cfg.AnnotationConfiguration</value>
       </property>
       <property name="hibernateProperties">
           <props>
               <prop key="hibernate.dialect">${jdbc.dialect}</prop>
               <prop key="hibernate.show_sql">true</prop>
           </props>
       </property>
   </bean>
<!-- Hibernate Settings - END -->

 <mvc:resources mapping="/resources/**" location="/resources/inspiremetheme/" />

 <mvc:annotation-driven/>

 <tx:annotation-driven />

 <!-- Start of Autowired beans -->
 <bean id="userBO" class="com.inspireme.boimpl.UserBOImpl"></bean>
 <bean id="userDAO" class="com.inspireme.daoimpl.UserDAOImpl"></bean>
 <!-- End of Autowired beans -->

 <bean id="transactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"></property>
</bean>

hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <mapping class="com.inspireme.dto.UserDTO"></mapping>
    </session-factory>
</hibernate-configuration>

Controller class

package com.inspireme.controller;

@Controller
@RequestMapping("/")
public class InspireMeController {

    public ApplicationContext applicationContext;

    private static final Logger LOGGER = Logger.getLogger(InspireMeController.class);

    @Autowired
    private UserBO userBO;

Service class:

package com.inspireme.boimpl;

@Service
public class UserBOImpl implements UserBO {

    private static final Logger LOGGER = Logger.getLogger(UserBOImpl.class);

    @Autowired
    private UserDAO userDAO;

Repository class

package com.inspireme.daoimpl;

@Repository
public class UserDAOImpl implements UserDAO {

    @Autowired
    private SessionFactory sessionFactory;

Why am I getting this exception since all the 3 beans, are defined in the dispatcher-servlet.xml file.

I am referring the following source to integrate Hibernate.

解决方案

java.lang.NoClassDefFoundError: Lorg/hibernate/cache/CacheProvider jar is missing.

Not exactly sure how the build works in your case but try classpath:/hibernate/hibernate.cfg.xml or classpath:hibernate/hibernate.cfg.xml. Or better check where exactly that file is within WEB-INF/classes in the deployed war.

这篇关于无法自动装配字段:Spring-Hibernate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 08:39