大神帮我看看 spring 整合 hibernate4 时sessionFactory 的问题

X243964552 2014-10-07 06:38:02
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: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/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"
>
<context:annotation-config/>
<!-- 扫描包-->
<context:component-scan base-package="com.guanlan.bean"/>
<context:component-scan base-package="com.guanlan.dao"/>
<context:component-scan base-package="com.guanlan.service"/>


<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan" value="com.guanlan.bean.*"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>
<prop key="javax.persistence.validation.mode">none</prop>
</props>
</property>
</bean>



<bean id="dataSource"
class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="url" value="jdbc:mysql://localhost:3306/guanlan?useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="123"/>
<property name="filters" value="stat" />
<property name="maxActive" value="20" />
<property name="initialSize" value="1" />
<property name="maxWait" value="60000" />
<property name="minIdle" value="1" />
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<property name="minEvictableIdleTimeMillis" value="300000" />
<property name="validationQuery" value="SELECT 'x'" />
<property name="testWhileIdle" value="true" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
<property name="poolPreparedStatements" value="true" />
<property name="maxPoolPreparedStatementPerConnectionSize" value="50" />
</bean>

<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
<bean id="txManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>


<!-- 配置AOP,Spring是通过AOP来进行事务管理的 -->
<aop:config>
<!-- 设置pointCut表示哪些方法要加入事务处理 -->
<aop:pointcut id="allMethods"
expression="execution(* com.guanlan.service..*.*(..))" />
<!-- 通过advisor来确定具体要加入事务控制的方法 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="allMethods" />
</aop:config>
<!-- 配置哪些方法要加入事务控制 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<!-- 让所有的方法都加入事务管理,为了提高效率,可以把一些查询之类的方法设置为只读的事务 -->
<tx:method name="*" propagation="REQUIRED" read-only="true"/>
<!-- 以下方法都是可能设计修改的方法,就无法设置为只读 -->
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="del*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="save*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>

</beans>

dispatcher-servlet.xml 文件


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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/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/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">


<!--打开springMVC对 注解的支持-->
<mvc:annotation-driven/>
<!-- 处理JSON乱码 -->


<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes" value="text/plain;charset=UTF-8"/>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>

<context:component-scan base-package="com.guanlan.controller"/>

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

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

<!-- 上传 需要使用到的配置-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="1046666000"/>
</bean>
<!--配置全局的异常-->
<bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<prop key="com.guanlan.util.SportException">error</prop>
</props>
</property>
</bean>

</beans>




web.xml 文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">

<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

<filter>
<filter-name>SpringFilter</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>SpringFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>



<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>

<filter>
<filter-name>openSessionInViewerFilter</filter-name>
<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>singleSession</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>openSessionInViewerFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>



错误代码

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 org.hibernate.MappingException: Failed to load annotated classes from classpath
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1568)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:540)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302)
at


包结构

用到的jar
...全文
377 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
wyh5253 2015-09-15
  • 打赏
  • 举报
回复
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd "> <!-- 开启注解功能 --> <!-- <context:annotation-config /> --> <!-- 引入属性文件 --> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <value>classpath:*.properties</value> </property> </bean> <!-- 使用c3p0连接池 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <!-- 指定连接数据库的驱动--> <property name="driverClass" value="${jdbc.driverClass}"/> <!-- 指定连接数据库的URL--> <property name="jdbcUrl" value="${jdbc.url}"/> <!-- 指定连接数据库的用户名--> <property name="user" value="${jdbc.user}"/> <!-- 指定连接数据库的密码--> <property name="password" value="${jdbc.password}"/> <!-- 指定连接池中保留的最大连接数. Default:15--> <property name="maxPoolSize" value="35"/> <!-- 指定连接池中保留的最小连接数--> <property name="minPoolSize" value="10"/> <!-- 指定连接池的初始化连接数 取值应在minPoolSize 与 maxPoolSize 之间.Default:3--> <property name="initialPoolSize" value="5"/> <!-- 最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。 Default:0--> <property name="maxIdleTime" value="60"/> <!-- 当连接池中的连接耗尽的时候c3p0一次同时获取的连接数. Default:3--> <property name="acquireIncrement" value="5"/> <!-- JDBC的标准,用以控制数据源内加载的PreparedStatements数量。但由于预缓存的statements属于单个connection而不是整个连接池所以设置这个参数需要考虑到多方面的因数.如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default:0--> <property name="maxStatements" value="0"/> <!-- 每60秒检查所有连接池中的空闲连接.Default:0 --> <property name="idleConnectionTestPeriod" value="60"/> <!-- 定义在从数据库获取新连接失败后重复尝试的次数。 Default:30 --> <property name="acquireRetryAttempts" value="30"/> <!-- 获取连接失败将会引起所有等待连接池来获取连接的线程抛出异常。但是数据源仍有效保留,并在下次调用getConnection()的时候继续尝试获取连接。如果设为true,那么在尝试获取连接失败后该数据源将申明已断开并永久关闭。Default:false --> <property name="breakAfterAcquireFailure" value="true"/> <!-- 银性能消耗大请只在需要的时候是哟个它。如果设为true,那么在每个connection提交的时候都将校验其有效性。建议使用idleConnectionTestPeriod或automaticTestTable等提升连接测试的性能。 Default:false--> <property name="testConnectionOnCheckin" value="true"/> <property name="testConnectionOnCheckout" value="true"/> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="configLocation" value="classpath:hibernate.cfg.xml"/> <property name="dataSource" ref="dataSource"/> <property name="mappingLocations"> <list> <value>classpath:mapping/User_hbm.xml</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop><!-- Oracle --> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.autoReconnect">true</prop> </props> </property> </bean> <!-- 定义事务管理器(声明式的事务) --> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <!-- 为事务拦截器注入一个事务管理器 --> <property name="sessionFactory" ref="sessionFactory" /> </bean> <!-- 需要引入tx的命名空间 --> <!-- 这是事务通知操作,使用的事务管理器引用自 transactionManager --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <!-- 指定哪些方法需要加入事务 --> <tx:method name="insert*" propagation="REQUIRED" /> <tx:method name="update*" propagation="REQUIRED" /> <tx:method name="delete*" propagation="REQUIRED" /> <tx:method name="get*" propagation="REQUIRED" read-only="true"/> <tx:method name="query*" propagation="REQUIRED" read-only="true"/> <tx:method name="find*" propagation="REQUIRED" read-only="true"/> <tx:method name="*" propagation="REQUIRED" /> </tx:attributes> </tx:advice> <!-- 需要引入aop的命名空间--> <aop:config> <!-- 切入点指明了在执行Service的所有方法时产生事务拦截操作 --> <aop:pointcut id="daoMethods" expression="execution(* org.wjw.service.impl.*.*(..))" /> <!-- 定义了将采用何种拦截操作,这里引用到 txAdvice --> <aop:advisor advice-ref="txAdvice" pointcut-ref="daoMethods" /> </aop:config> <bean id="userDAO" class="org.wjw.dao.impl.UserDAOImpl" scope="singleton"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <bean id="userService" class="org.wjw.service.impl.UserServiceImpl" scope="singleton"> <property name="userDao" ref="userDAO" /> </bean> <bean id="listUserAction" class="org.wjw.action.user.ListUserAction" scope="prototype"> <property name="userService" ref="userService"></property> </bean> <!-- <import resource="beans.xml"/> --> </beans>
qq_29695897 2015-07-15
  • 打赏
  • 举报
回复
楼主怎么解决的?
Mr_sqw 2014-10-08
  • 打赏
  • 举报
回复
<context:annotation-config /> <context:component-scan base-package="换成你的,比如说com.guanlan.*" />
X243964552 2014-10-07
  • 打赏
  • 举报
回复
引用 5 楼 nd707355117 的回复:
[quote=引用 4 楼 X243964552 的回复:]
嗯,我搞错了,hibernate4还是按你原来的配置,这句<property name="packagesToScan" value="com.guanlan.bean.*"/>的value="com.guanlan.bean" 将.*去掉[/quote] 试了 不行
guan_tu 2014-10-07
  • 打赏
  • 举报
回复
引用 4 楼 X243964552 的回复:
嗯,我搞错了,hibernate4还是按你原来的配置,这句<property name="packagesToScan" value="com.guanlan.bean.*"/>的value="com.guanlan.bean" 将.*去掉
X243964552 2014-10-07
  • 打赏
  • 举报
回复
引用 3 楼 nd707355117 的回复:
导入hibernate-annotations.jar
hibernate-annotations.jar已经被 hibernate-commons-annotations-4.0.2.Final.jar 整合了, 如果在加上 就直接报错了
guan_tu 2014-10-07
  • 打赏
  • 举报
回复
导入hibernate-annotations.jar
X243964552 2014-10-07
  • 打赏
  • 举报
回复
引用 1 楼 nd707355117 的回复:
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 将LocalSessionFactoryBean换成 AnnotationSessionFactoryBean 好像应该在这个包org.springframework.orm.hibernate4.annotation.AnnotationSessionFactoryBean 楼主试试
hibernate4 中就没有org.springframework.orm.hibernate4.annotation.AnnotationSessionFactoryBean 这个东西的
guan_tu 2014-10-07
  • 打赏
  • 举报
回复
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 将LocalSessionFactoryBean换成 AnnotationSessionFactoryBean 好像应该在这个包org.springframework.orm.hibernate4.annotation.AnnotationSessionFactoryBean 楼主试试

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧