---
title: "配置spring+hibernate基本框架时的一些低级错误"
author: "mudid"
date: "2008-10-14T18:37:44Z"
tags: []
categories: ["企业架构"]
slug: "spring-hibernate"
draft: false
lastmod: "2008-10-14T18:37:44Z"
id: 252823
url: "https://mudid.iteye.com/blog/252823"
---

一、配置web.xml中若未配listener,则导致老是不加载applicationContext

```java

	<listener>
		<listener-class>
			org.springframework.web.context.ContextLoaderListener
		</listener-class>
	</listener>
        //if xmlfile in src,it will in /WEB-INF/classes/ after compiled
  	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			/WEB-INF/classes/applicationContext.xml
		</param-value>
	</context-param>
	<!-- the two combinations above is to unload "applicationContext.xml"
		which includes DAO and sessionFactory -->
	<servlet>
		<servlet-name>springapp</servlet-name>
		<servlet-class>
			org.springframework.web.servlet.DispatcherServlet
		</servlet-class>
		<load-on-startup>2</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>springapp</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>
	<!-- the two combinations above is to link Actions to  Controllers 
		which config in "springapp-servlet.xml",
		notice the servlet-name should match the configuration xml file -->
  	<welcome-file-list>
    	<welcome-file>/homepage.jsp</welcome-file>
  	</welcome-file-list>
  	<error-page>
  		<error-code>400</error-code>
  		<location>/common/error.jsp?errorCode=400</location>
  	</error-page>

```


二、TransactionProxyFactoryBean应该配置在applicationContext.xml中，而不是dao-proxy中，否则无法加载sessionFactory.

```java

<beans
	xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

	<import resource="dao/dao-proxy.xml"/>
	<bean id="dataSourceMysql"
		class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName"
			value="com.mysql.jdbc.Driver">
		</property>
		<property name="url" value="jdbc:mysql://localhost:3306/sand"></property>
		<property name="username" value="root"></property>
		<property name="password" value="manager"></property>
	</bean>
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource">
			<ref bean="dataSourceMysql" />
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">
					org.hibernate.dialect.MySQLDialect
				</prop>
			</props>
		</property>
		<property name="mappingResources">
			<list>
				<value>
				dao/balance/Balance.hbm.xml
				</value>
			</list>
		</property>
	</bean>
	
	<bean id="transactionManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>
        //a super proxy can be inherited in dao-proxy.
	<bean id="baseTransactionProxy"
		class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
		abstract="true">
		<property name="transactionManager" ref="transactionManager" />
		<property name="proxyTargetClass" value="true"></property>
		<property name="transactionAttributes">
			<props>
				<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
			</props>
		</property>
	</bean>
</beans>


```


三、在dao-proxy.xml配置dao代理和事务管理时，继承application中的，所以属性是parent

```java


<beans>
	<bean id="BalanceDAOImpl" class="dao.balance.BalanceDAOImpl">
		<property name="sessionFactory" ref="sessionFactory"/>
	</bean>
        // parent had configed in applicationContext
	<bean id="BalanceDAO" parent="baseTransactionProxy">
		<property name="target" ref="BalanceDAOImpl"></property>
	</bean>		
</beans>


```


四、在springapp-servlet.xml中，bean的id 记得不要和其他配置中重复，否则不加载

```java

<beans>
	<bean id="urlMappingBalance"
		class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
		<property name="interceptors">
			<list>
			</list>
		</property>
		<property name="mappings">
			<props>
				<prop key="balanceSave.do">
					balanceSave
				</prop>
			</props>
		</property>
	</bean>
	<bean id="balanceSave"
		class="service.balance.BalanceSave">
		<property name="balanceDAO" ref="BalanceDAO"></property>
	</bean>
</beans>

```


五、注意路径前的“/”以及包路径中的“.”在某些配置中要用“/”代替。

```java

//sample one,required in web.xml, unnecessary in page form.
<welcome-file>/homepage.jsp</welcome-file>
<form action="balanceSave.do" method="post">

//sample two,filepath not package path.
<property name="mappingResources">
	<list>
		<value>dao/balance/Balance.hbm.xml</value>
	</list>
</property>

//sample three,add package or full class name.
<hibernate-mapping package="dao.balance">
    <class name="Balance" table="balances" catalog="sand">

```


六、其他小毛病

在myEclipse中同时添加spring2.0和hibernate3.1后，容易出现包冲突，建议把所有包拷到WEB-INFf的lib中，然后删掉asm-2.2.3.jar,asm.jar。

花了我半天的时间的一个问题：

Java Data Access Object - Generate Precise findBy methods - DAO type - Spring DAO
<灰色 不能选>

原因：为了方便，把原来一些工程中的包和主要几个配置直接cop而不用导入spring和hibernate的capbiltiy，于是在用MyEclipse的DataBase Explorer的Hibernate Reverse Enginnering时就不能生产spring DAO。

解决办法，去以前的工程中把.myhibernatedata资源文件考到工程根目录下，其实这样还是不行地：），改.project配置文件吧。（提示：在工程根目录下，eclipse中在filter里把.resource隐藏的勾选去掉，就能看到了）


```java

<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
	<name>sand</name>
	<comment></comment>
	<projects>
//手动把和spring、hibernate相关的类从其他工程中复制上来吧，就OK了
	</projects>
	<buildSpec>
		<buildCommand>
			<name>com.genuitec.eclipse.j2eedt.core.WebClasspathBuilder</name>
		</buildCommand>
		<buildCommand>
			<name>org.eclipse.jdt.core.javabuilder</name>
		</buildCommand>	
		<buildCommand>
			<name>com.genuitec.eclipse.springframework.springbuilder</name>
		</buildCommand>
		<buildCommand>
			<name>com.genuitec.eclipse.hibernate.HibernateBuilder</name>
		</buildCommand>		
		<buildCommand>
			<name>com.genuitec.eclipse.j2eedt.core.J2EEProjectValidator</name>
		</buildCommand>
		<buildCommand>
			<name>com.genuitec.eclipse.j2eedt.core.DeploymentDescriptorValidator</name>
		</buildCommand>
		<buildCommand>
			<name>org.eclipse.wst.validation.validationbuilder</name>
		</buildCommand>
		<buildCommand>
			<name>com.genuitec.eclipse.ast.deploy.core.DeploymentBuilder</name>
		</buildCommand>
	</buildSpec>
	<natures>
		<nature>com.genuitec.eclipse.hibernate.hibernatenature</nature>
		<nature>com.genuitec.eclipse.springframework.springnature</nature>
		<nature>com.genuitec.eclipse.ast.deploy.core.deploymentnature</nature>
		<nature>com.genuitec.eclipse.j2eedt.core.webnature</nature>
		<nature>org.eclipse.jdt.core.javanature</nature>
	</natures>
</projectDescription>

```


七、最无语的错误

在sql查询窗口中，就是运行不了，害得我检查了半天！
结果没把我气死，sql语句中表名中下划线旁边的空格！

```java

create table rbac_group_role...

```