读书人

beans.xml配置异常

发布时间: 2012-04-15 18:39:21 作者: rapoo

beans.xml配置错误
beans.xml

XML code
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns:context="http://www.springframework.org/schema/context" 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/tx                                  http://www.springframework.org/schema/tx/spring-tx-3.0.xsd                                 http://www.springframework.org/schema/aop                                 http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">    <context:annotation-config></context:annotation-config>    <context:component-scan base-package="com.chk"></context:component-scan>    <!-- 使用annotation管理 Transaction 事务管理 -->    <!-- <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven> -->    <!-- 使用XML方法管理Transaction事务 -->    <!-- <bean name="userService" class="com.chk.service.UserService"></bean> -->    <!-- <aop:aspectj-autoproxy></aop:aspectj-autoproxy> -->    <!-- <bean id="logInterceptor" class="com.chk.aop.LogInterceptor"></bean>         <aop:config> <aop:pointcut expression="execution(public * com.chk.impl..*(..))"         id="servicePointcut" /> <aop:aspect id="logAspect" ref="logInterceptor">         <aop:before method="beforMothed" pointcut-ref="servicePointcut" /> </aop:aspect>         </aop:config> -->    <!-- 配置数据库连接 -->    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"        destroy-method="close">        <!-- results in a setDriverClassName(String) call -->        <property name="driverClassName" value="com.mysql.jdbc.Driver" />        <property name="url" value="jdbc:mysql://localhost:3306/oa" />        <property name="username" value="root" />        <property name="password" value="chengke168" />    </bean>    <!-- 产生sessionFactory -->    <bean id="sessionFactory"        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">        <!-- 连接数据库 -->        <property name="dataSource" ref="dataSource" />        <property name="annotatedClasses">            <list>                <value>com.chk.model.Orgnization</value>                <value>com.chk.model.Person</value>            </list>        </property>        <!-- <property name="packagesToScan">            <list>                <value>com.chk.model</value>            </list>        </property> -->        <property name="hibernateProperties">            <props>                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>                <prop key="hibernate.show_sql">true</prop>                <prop key="hibernate.format_sql">true</prop>                <prop key="hibernate.hbm2ddl.auto">create</prop>            </props>        </property>    </bean>    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">        <property name="sessionFactory" ref="sessionFactory"></property>    </bean>    <aop:config>        <aop:pointcut expression="execution(public * com.chk.service..*(..))"            id="buessinessService" />        <aop:advisor advice-ref="txAdvice" pointcut-ref="buessinessService" />    </aop:config>    <tx:advice id="txAdvice" transaction-manager="transactionManager">        <tx:attributes>            <tx:method name="*" read-only="true" />            <tx:method name="add*" />        </tx:attributes>    </tx:advice>    <bean id="transactionManager"        class="org.springframework.orm.hibernate3.HibernateTransactionManager">        <property name="sessionFactory" ref="sessionFactory"></property>    </bean>    <!-- <bean id="orgManager" class="com.chk.service.impl.OrgManagerImpl">        <property name="sessionFactory" ref="sessionFactory"></property>    </bean> --></beans> 



hibernate.cfg.xml
XML code
<?xml version='1.0' encoding='utf-8'?><!DOCTYPE hibernate-configuration PUBLIC        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"><hibernate-configuration>    <session-factory>        <!-- Database connection settings -->        <property name="connection.driver_class">            com.mysql.jdbc.Driver        </property>        <property name="connection.url">            jdbc:mysql://localhost:3306/oa        </property>        <property name="connection.username">root</property>        <property name="connection.password">chengke168</property>        <!-- JDBC connection pool (use the built-in) -->        <!-- <property name="connection.pool_size">1</property> -->        <!-- SQL dialect -->        <property name="dialect">            org.hibernate.dialect.MySQLDialect        </property>        <!-- JDBC connection pool (use the built-in) -->        <property name="connection.pool_size">1</property>        <!-- Enable Hibernate's automatic session context management -->        <property name="current_session_context_class">thread</property>        <property name="javax.persistence.validation.mode">none</property>        <!-- Disable the second-level cache -->        <!-- <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> -->        <!-- Echo all executed SQL to stdout -->        <property name="show_sql">true</property>        <property name="format_sql">true</property>        <!-- Drop and re-create the database schema on startup -->        <property name="hbm2ddl.auto">cteate</property>        <mapping class="com.chk.model.Person" />        <mapping class="com.chk.model.Orgnization" />    </session-factory></hibernate-configuration>


问题如下:
我如果用beans.xml产生sessionFactory,就会报错,说我的bean嵌套不对,如果我把beans.xml中生成sessionFactory的bean注销掉,使用
XML code
    <!-- 配置SessionFactory -->    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">        <property name="configLocation">            <value>classpath:hibernate.cfg.xml</value>        </property>    </bean>

来生成sessionFactory,就没有问题,

我现在就想知道,我在Beans.xml中配置的sessionFactory哪里出错了?

[解决办法]
这样配的话,会禁止使用JPA hibernate-validator来做数据验证。如果不配成none的话,你需要在项目中加入hibernate-validator相关的两个jar包。
参见官方文档
http://docs.jboss.org/hibernate/entitymanager/3.6/reference/en/html/configuration.html

读书人网 >Java Web开发

热点推荐