spring4+springmvc4+hibernate4异常

来源:互联网 发布:spark源码讲解 许鹏 编辑:程序博客网 时间:2024/05/19 19:39

最近公司再用springmvc+spring+hibernate做项目,spring和springmvc用了同一个文件

web.xml

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://xmlns.jcp.org/xml/ns/javaee"xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"id="WebApp_ID" version="3.1"><display-name>zgc</display-name><filter>          <filter-name>characterEncoding</filter-name>          <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>          <init-param>              <param-name>encoding</param-name>              <param-value>utf-8</param-value>          </init-param>                </filter>      <filter-mapping>          <filter-name>characterEncoding</filter-name>          <url-pattern>/*</url-pattern>      </filter-mapping>  <servlet>          <servlet-name>springServlet</servlet-name>          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>          <init-param>              <param-name>contextConfigLocation</param-name>              <param-value>classpath:spring/mvc/action-servlet.xml</param-value>          </init-param>          <load-on-startup>1</load-on-startup>          <async-supported>true</async-supported>      </servlet>        <servlet-mapping>          <servlet-name>springServlet</servlet-name>          <url-pattern>/</url-pattern>      </servlet-mapping>  <welcome-file-list><welcome-file>index.html</welcome-file><welcome-file>index.htm</welcome-file><welcome-file>index.jsp</welcome-file><welcome-file>default.html</welcome-file><welcome-file>default.htm</welcome-file><welcome-file>default.jsp</welcome-file></welcome-file-list></web-app>

action-servlet.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"            xmlns:aop="http://www.springframework.org/schema/aop"             xmlns:tx="http://www.springframework.org/schema/tx"            xsi:schemaLocation="http://www.springframework.org/schema/beans                                 http://www.springframework.org/schema/beans/spring-beans-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/context                                 http://www.springframework.org/schema/context/spring-context-3.0.xsd                                 http://www.springframework.org/schema/aop                                 http://www.springframework.org/schema/aop/spring-aop-3.0.xsd                                 http://www.springframework.org/schema/tx                                 http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">                <!-- DispatcherServlet Context: defines this servlet's request-processing               infrastructure -->                <!-- Enables the Spring MVC @Controller programming model -->          <!-- springmvc注解,必须配置 -->          <context:component-scan base-package="com.h3c.zgc"/>        <mvc:annotation-driven />          <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"              destroy-method="close">              <property name="driverClass">                  <value>com.mysql.jdbc.Driver</value>              </property>              <property name="jdbcUrl">                  <value>jdbc:mysql://localhost:3306/itac</value>              </property>              <property name="user">                  <value>root</value>              </property>              <property name="password">                  <value>root</value>              </property>              <!--连接池中保留的最小连接数。 -->              <property name="minPoolSize" value="10" />              <!--连接池中保留的最大连接数。Default: 15 -->              <property name="maxPoolSize" value="100" />              <!--最大空闲时间,1800秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->              <property name="maxIdleTime" value="1800" />              <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->              <property name="acquireIncrement" value="5" />              <property name="maxStatements" value="1000" />              <property name="initialPoolSize" value="10" />              <!--每60秒检查所有连接池中的空闲连接。Default: 0 -->              <property name="idleConnectionTestPeriod" value="60" />              <!--定义在从数据库获取新连接失败后重复尝试的次数。Default: 30 -->              <property name="acquireRetryAttempts" value="30" />              <property name="breakAfterAcquireFailure" value="true" />              <property name="testConnectionOnCheckout" value="false" />          </bean>                <bean id="sessionFactory"              class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">              <property name="dataSource" ref="dataSource" />              <property name="packagesToScan" value="com.h3c.zgc" />              <property name="hibernateProperties">                  <value>                      hibernate.dialect=org.hibernate.dialect.MySQL5Dialect                    hibernate.hbm2ddl.auto=update                      hibernate.connection.autocommit=true                      hibernate.show_sql=false                      hibernate.format_sql=false                      hibernate.cache.use_second_level_cache=true                      hibernate.cache.use_query_cache=false                      hibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContext                </value>              </property>          </bean>                      <bean id="txManager"              class="org.springframework.orm.hibernate4.HibernateTransactionManager">              <property name="sessionFactory" ref="sessionFactory"></property>              <property name="nestedTransactionAllowed" value="true" />                </bean>                <tx:annotation-driven transaction-manager="txManager"              proxy-target-class="true" mode="proxy" />                    <!-- 静态资源处理配置 -->          <mvc:resources mapping="/resources/**" location="/resources/" />                <!-- Resolves views selected for rendering by @Controllers to .jsp resources               in the /WEB-INF/views directory -->          <bean              class="org.springframework.web.servlet.view.InternalResourceViewResolver">              <property name="prefix" value="/views/" />              <property name="suffix" value=".jsp" />          </bean>      </beans>  

异常一、

Caused by: org.hibernate.boot.registry.selector.spi.StrategySelectionException: Unable to resolve name [org.hibernate.dialect.MySQL5Dialect  ] as strategy [org.hibernate.dialect.Dialect]

出现的异常信息甚是奇怪,异常信息很简单,数据库方言配置的问题,但是我的方言肯定是正确的,想了好久,没有想出原因,后来仔细看了下异常信息,发现[org.hibernate.dialect.MySQL5Dialect  ]
方括号最后面有个空格,感觉会不会是这个空格的问题,然后就去掉空格,重新运行,还是报错,但是已经不是这个方言的异常了。

异常二、

org.hibernate.boot.registry.classloading.spi.ClassLoadingException: Unable to load class [org.springframework.orm.hibernate4.SpringSessionContext ]at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:243)at org.hibernate.internal.SessionFactoryImpl.buildCurrentSessionContext(SessionFactoryImpl.java:1512)
这个错误还是和hibernateProperties的配置有关,有了上次空格的教训,直接就去SpringSessionContext
后面寻找看看是否存在空格,果不其然,确实有空格,去掉空格,重启tomcat,顺利启动,其实这个也算不上什么技术,如果只看配置,类似可能也不知道在哪里出现的问题,看了异常信息才觉得有可能出现异常的地方,可见异常信息是多么的重要啊


0 0