將hibernate.cfg.xml檔案都放到spring中時報錯
阿新 • • 發佈:2018-11-11
報錯如下所示:
私以為是配置檔案出現問題了。
<?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" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd "> <!-- 配置c3p0連線池 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <!-- 注入屬性值 --> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="jdbcUrl" value="jdbc:mysql:///spring_day04"></property> <property name="user" value="root"></property> <property name="password" value=""></property> </bean> <!-- sessionFactory的建立交給spring管理 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <!-- 指定使用hibernate核心配置檔案中,沒有資料庫配置,資料庫配置在spring裡面配置,注入dataSource --> <property name="dataSource" ref="dataSource"></property> <!-- 指定使用hibernate核心配置檔案 --> <!-- <property name="configLocations" value="classpath:hibernate.cfg.xml"></property> --> <!-- 配置hibernate基本資訊 --> <property name="hibernateProperties"> <props> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> </props> </property> <!-- 配置對映檔案引入 --> <property name="mappingResources"> <list> <value>cn/itcast/entity/User.hbm.xml</value> </list> </property> </bean> <!-- 第一步 配置事務管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> <!-- 注入sessionFactory --> <property name="sessionFactory" ref="sessionFactory"> </property> </bean> <!-- 第二步 開啟事務註解 --> <tx:annotation-driven transaction-manager="transactionManager" /> <!-- 配置action物件 --> <bean id="userAction" class="cn.itcast.action.UserAction" scope="prototype"> <!-- 注入service --> <property name="userService" ref="userService"></property> </bean> <!-- 建立service物件 --> <bean id="userService" class="cn.itcast.service.UserService"> <!-- 注入dao 介面 = 實現類物件 --> <property name="userDao" ref="userDaoImpl"> </property> </bean> <!-- 建立實現類物件 --> <bean id="userDaoImpl" class="cn.itcast.dao.UserDaoImpl"> <property name="hibernateTemplate" ref="hibernateTemplate"></property> </bean> <!-- 建立hibernateTemplate物件 --> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate"> <!-- 注入sessionFactory --> <property name="sessionFactory" ref="sessionFactory"> </property> </bean> </beans>
專案中的index.jsp可以載入。
但是不知為何當配置檔案變成下面的樣子時,程式就正常運行了。
<?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" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd "> <!-- 配置c3p0連線池 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <!-- 注入屬性值 --> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="jdbcUrl" value="jdbc:mysql:///spring_day04"></property> <property name="user" value="root"></property> <property name="password" value=""></property> </bean> <!-- sessionFactory的建立交給spring管理 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <!-- 指定使用hibernate核心配置檔案中,沒有資料庫配置,資料庫配置在spring裡面配置,注入dataSource --> <property name="dataSource" ref="dataSource"></property> <!-- 指定使用hibernate核心配置檔案 --> <!-- <property name="configLocations" value="classpath:hibernate.cfg.xml"></property> --> <!-- 配置hibernate基本資訊 --> <property name="hibernateProperties"> <props> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> </props> </property> <!-- 配置對映檔案引入 --> <property name="mappingResources"> <list> <value>cn/itcast/entity/User.hbm.xml</value> </list> </property> </bean> <!-- 第一步 配置事務管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> <!-- 注入sessionFactory --> <property name="sessionFactory" ref="sessionFactory"> </property> </bean> <!-- 第二步 開啟事務註解 --> <tx:annotation-driven transaction-manager="transactionManager"/> <!-- 配置action物件 --> <!-- <bean id="userAction" class="cn.itcast.action.UserAction" scope="prototype"> 注入service <property name="userService" ref="userService"></property> </bean> 建立service物件 <bean id="userService" class="cn.itcast.service.UserService"> 注入dao 介面 = 實現類物件 <property name="userDao" ref="userDaoImpl"> </property> </bean> 建立實現類物件 <bean id="userDaoImpl" class="cn.itcast.dao.UserDaoImpl"> <property name="hibernateTemplate" ref="hibernateTemplate"></property> </bean> 建立hibernateTemplate物件 <bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate"> 注入sessionFactory <property name="sessionFactory" ref="sessionFactory"> </property> </bean> --> <!-- 引入其他spring配置檔案 --> <import resource="classpath:user.xml"/> </beans>