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

来源:互联网 发布:小米自动抢购软件神器 编辑:程序博客网 时间:2024/06/03 08:09
 关键字: 出错 原因 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>  


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


三、在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>  


四、在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>  


五、注意路径前的“/”以及包路径中的“.”在某些配置中要用“/”代替。
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">  


六、其他小毛病

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


七、最无语的错误

在sql查询窗口中,就是运行不了,害得我检查了半天!
结果没把我气死,sql语句中表名中下划线旁边的空格!
Java代码 复制代码
  1. create table rbac_group_role...  
原创粉丝点击