2012-01-04 99 views
1

我想用Maven安裝SpringMVC 3.0 + Hibernate + MySql。 令人沮喪的applicationContext.xml問題。 對DAO服務的控制器工作,只是我有問題設置Hibernate到MySql。applicationContext.xml問題,設置Hibernate,Spring MVC和MySql

在線搜索了很多東西,但不同的教程有不同的說明,所以我現在都搞砸了。

的applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation=" 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-3.0.xsd 
     http://www.springframework.org/schema/mvc 
     http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
     http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> 

<context:component-scan base-package="com.ray.service.blog" /> 
<mvc:annotation-driven /> 

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" /> 
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> 

<bean id="blogDao" class="com.ray.service.blog.dao.BlogDao"> 
    <!-- <property name="sessionFactory" ref="sessionFactory"></property> --> 
    <constructor-arg ref="sessionFactory" /> 
</bean> 

<bean id="blogService" class="com.ray.service.blog.services.BlogService" /> 

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 
    <property name="dataSource" ref="dataSource" /> 
    <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">org.hibernate.dialect.MySQLDialect</prop> 
      <prop key="hibernate.show_sql">true</prop> 
     </props> 
    </property> 
</bean> 

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
    <property name="driverClassName" value="com.mysql.jdbc.Driver" /> 
    <property name="url" value="jdbc:mysql://localhost:3306/ray" /> 
    <property name="username" value="root" /> 
    <property name="password" value="root" /> 
</bean> 

這裏的堆棧跟蹤的前幾行:

[2012-01-04 10:17:27,265] ERROR [org.springframework.web.context.ContextLoader] Context initialization failed 
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'blogController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void com.ray.service.blog.controllers.BlogController.setBlogService(com.ray.service.blog.services.BlogService); nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'blogService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void com.ray.service.blog.services.BlogService.setBlogDao(com.ray.service.blog.dao.BlogDao); nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'blogDao' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Cannot resolve reference to bean 'sessionFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: org/slf4j/impl/StaticLoggerBinder 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:288) 

讓我知道如果你需要更多的信息.​​..謝謝!

回答

2

爲了解決這個錯誤添加到您的pom.xml

<dependency> 
     <groupId>org.slf4j</groupId> 
     <artifactId>slf4j-log4j12</artifactId> 
     <version>${slf4j.version}</version> 
    </dependency> 

爲了提高您的連接嘗試使用這樣

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> 
    <property name="driverClassName" value="com.mysql.jdbc.Driver" /> 
    <property name="url" value="jdbc:mysql://localhost:3306/ray" /> 
    <property name="username" value="root" /> 
    <property name="password" value="root" /> 
</bean> 

,並在pom.xml東西,你需要添加

<dependency> 
    <groupId>commons-dbcp</groupId> 
    <artifactId>commons-dbcp</artifactId> 
    <version>1.4</version> 
</dependency> 

在我們的項目中,我們通常使用這個定義或從c3p0

未來: 考慮將以下屬性已經彙集連接

<property name="initialSize" value="${hibernate.initialSize}" /> 
<property name="minIdle" value="${hibernate.minIdle}" /> 
<property name="maxActive" value="${hibernate.maxActive}" /> 

而在這個bean必須看看你的配置屬性導出到專用文件

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
     <property name="ignoreUnresolvablePlaceholders" value="true"/> 
     <property name="locations"> 
      <list> 
       <value>classpath:hibernate_jdbc.properties</value> 
      </list> 
     </property> 
    </bean> 
2

您可能錯過了slf4j的綁定。

Hibernate在3.3.x中切換到slf4j。

將列出的綁定之一here添加到項目的classpath/pom.xml中,它應該可以工作。

0

看起來是罪魁禍首是NoClassDefFoundError。組織/ SLF4J/IMPL/StaticLoggerBinder。 您需要在類路徑中包含相應的jar。您擁有的Hibernate提取應該有一個slf4j jar。