配置spring+hibernate基本框架时的一些低级错误(网摘)

本文详细介绍了Spring框架配置过程中的关键步骤与常见问题,包括web.xml中的监听器配置、TransactionProxyFactoryBean的正确位置、DAO代理配置技巧、bean id重复问题及路径配置注意事项等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  关键字: 出错 原因 spring dao type
一、配置web.xml中若未配listener,则导致老是不加载applicationContext
Java代码 复制代码
  1. <listener>   
  2.     <listener-class>   
  3.         org.springframework.web.context.ContextLoaderListener   
  4.     </listener-class>   
  5. </listener>   
  6.        //if xmlfile in src,it will in /WEB-INF/classes/ after compiled   
  7.     <context-param>   
  8.     <param-name>contextConfigLocation</param-name>   
  9.     <param-value>   
  10.         /WEB-INF/classes/applicationContext.xml   
  11.     </param-value>   
  12. </context-param>   
  13. <!-- the two combinations above is to unload "applicationContext.xml"  
  14.     which includes DAO and sessionFactory -->   
  15. <servlet>   
  16.     <servlet-name>springapp</servlet-name>   
  17.     <servlet-class>   
  18.         org.springframework.web.servlet.DispatcherServlet   
  19.     </servlet-class>   
  20.     <load-on-startup>2</load-on-startup>   
  21. </servlet>   
  22. <servlet-mapping>   
  23.     <servlet-name>springapp</servlet-name>   
  24.     <url-pattern>*.do</url-pattern>   
  25. </servlet-mapping>   
  26. <!-- the two combinations above is to link Actions to  Controllers    
  27.     which config in "springapp-servlet.xml",   
  28.     notice the servlet-name should match the configuration xml file -->   
  29.     <welcome-file-list>   
  30.     <welcome-file>/homepage.jsp</welcome-file>   
  31.     </welcome-file-list>   
  32.     <error-page>   
  33.         <error-code>400</error-code>   
  34.         <location>/common/error.jsp?errorCode=400</location>   
  35.     </error-page>  
	<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代码 复制代码
  1. <beans   
  2.     xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">   
  5.   
  6.     <import resource="dao/dao-proxy.xml"/>   
  7.     <bean id="dataSourceMysql"  
  8.         class="org.apache.commons.dbcp.BasicDataSource">   
  9.         <property name="driverClassName"  
  10.             value="com.mysql.jdbc.Driver">   
  11.         </property>   
  12.         <property name="url" value="jdbc:mysql://localhost:3306/sand"></property>   
  13.         <property name="username" value="root"></property>   
  14.         <property name="password" value="manager"></property>   
  15.     </bean>   
  16.     <bean id="sessionFactory"  
  17.         class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">   
  18.         <property name="dataSource">   
  19.             <ref bean="dataSourceMysql" />   
  20.         </property>   
  21.         <property name="hibernateProperties">   
  22.             <props>   
  23.                 <prop key="hibernate.dialect">   
  24.                     org.hibernate.dialect.MySQLDialect   
  25.                 </prop>   
  26.             </props>   
  27.         </property>   
  28.         <property name="mappingResources">   
  29.             <list>   
  30.                 <value>   
  31.                 dao/balance/Balance.hbm.xml   
  32.                 </value>   
  33.             </list>   
  34.         </property>   
  35.     </bean>   
  36.        
  37.     <bean id="transactionManager"  
  38.         class="org.springframework.orm.hibernate3.HibernateTransactionManager">   
  39.         <property name="sessionFactory" ref="sessionFactory" />   
  40.     </bean>   
  41.         //a super proxy can be inherited in dao-proxy.   
  42.     <bean id="baseTransactionProxy"  
  43.         class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"  
  44.         abstract="true">   
  45.         <property name="transactionManager" ref="transactionManager" />   
  46.         <property name="proxyTargetClass" value="true"></property>   
  47.         <property name="transactionAttributes">   
  48.             <props>   
  49.                 <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>   
  50.             </props>   
  51.         </property>   
  52.     </bean>   
  53. </beans>  
<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代码 复制代码
  1.   
  2. <beans>   
  3.     <bean id="BalanceDAOImpl" class="dao.balance.BalanceDAOImpl">   
  4.         <property name="sessionFactory" ref="sessionFactory"/>   
  5.     </bean>   
  6.         // parent had configed in applicationContext   
  7.     <bean id="BalanceDAO" parent="baseTransactionProxy">   
  8.         <property name="target" ref="BalanceDAOImpl"></property>   
  9.     </bean>          
  10. </beans>  
<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代码 复制代码
  1. <beans>   
  2.     <bean id="urlMappingBalance"  
  3.         class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">   
  4.         <property name="interceptors">   
  5.             <list>   
  6.             </list>   
  7.         </property>   
  8.         <property name="mappings">   
  9.             <props>   
  10.                 <prop key="balanceSave.do">   
  11.                     balanceSave   
  12.                 </prop>   
  13.             </props>   
  14.         </property>   
  15.     </bean>   
  16.     <bean id="balanceSave"  
  17.         class="service.balance.BalanceSave">   
  18.         <property name="balanceDAO" ref="BalanceDAO"></property>   
  19.     </bean>   
  20. </beans>  
<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代码 复制代码
  1. //sample one,required in web.xml, unnecessary in page form.   
  2. <welcome-file>/homepage.jsp</welcome-file>   
  3. <form action="balanceSave.do" method="post">   
  4.   
  5. //sample two,filepath not package path.   
  6. <property name="mappingResources">   
  7.     <list>   
  8.         <value>dao/balance/Balance.hbm.xml</value>   
  9.     </list>   
  10. </property>   
  11.   
  12. //sample three,add package or full class name.   
  13. <hibernate-mapping package="dao.balance">   
  14.     <class name="Balance" table="balances" catalog="sand">  
//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代码 复制代码
  1. <?xml version="1.0" encoding="UTF-8"?>   
  2. <projectDescription>   
  3.     <name>sand</name>   
  4.     <comment></comment>   
  5.     <projects>   
  6. //手动把和spring、hibernate相关的类从其他工程中复制上来吧,就OK了   
  7.     </projects>   
  8.     <buildSpec>   
  9.         <buildCommand>   
  10.             <name>com.genuitec.eclipse.j2eedt.core.WebClasspathBuilder</name>   
  11.         </buildCommand>   
  12.         <buildCommand>   
  13.             <name>org.eclipse.jdt.core.javabuilder</name>   
  14.         </buildCommand>      
  15.         <buildCommand>   
  16.             <name>com.genuitec.eclipse.springframework.springbuilder</name>   
  17.         </buildCommand>   
  18.         <buildCommand>   
  19.             <name>com.genuitec.eclipse.hibernate.HibernateBuilder</name>   
  20.         </buildCommand>          
  21.         <buildCommand>   
  22.             <name>com.genuitec.eclipse.j2eedt.core.J2EEProjectValidator</name>   
  23.         </buildCommand>   
  24.         <buildCommand>   
  25.             <name>com.genuitec.eclipse.j2eedt.core.DeploymentDescriptorValidator</name>   
  26.         </buildCommand>   
  27.         <buildCommand>   
  28.             <name>org.eclipse.wst.validation.validationbuilder</name>   
  29.         </buildCommand>   
  30.         <buildCommand>   
  31.             <name>com.genuitec.eclipse.ast.deploy.core.DeploymentBuilder</name>   
  32.         </buildCommand>   
  33.     </buildSpec>   
  34.     <natures>   
  35.         <nature>com.genuitec.eclipse.hibernate.hibernatenature</nature>   
  36.         <nature>com.genuitec.eclipse.springframework.springnature</nature>   
  37.         <nature>com.genuitec.eclipse.ast.deploy.core.deploymentnature</nature>   
  38.         <nature>com.genuitec.eclipse.j2eedt.core.webnature</nature>   
  39.         <nature>org.eclipse.jdt.core.javanature</nature>   
  40.     </natures>   
  41. </projectDescription>  
<?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代码 复制代码
  1. create table rbac_group_role...  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值