SSH整合参考实例
2013-02-28 22:07
435 查看
网上的教程很多,参考了一些,自己再总结一下。
版本:Struts2.2.3 Spring3.0 Hibernate3.3
myeclipse8.6
整合步骤如下:
1、新建web工程
2、添加spring
3、添加hibernate
4、添加struts2
5、配置文件
新建web工程
添加spring
添加hibernate
注:之前可以先添加dataSource 具体步骤:Window->Open Perspective->MyEclipse Database Explorer
切换到Database Explorer视图
对表进行Hibernate反向转换
添加struts2
向/WEB-INF/lib添加jar包
最小jar包
配置web.xml
配置struts.xml
配置applicationContext.xml
遇到的问题:
解决的方法:
com.ideal.service.impl.UserServiceImpl实现的是com.ideal.service.interfaces.UserService接口,由于spring实现的是接口注入,关联的实现类。这里实现注入类,所以出现了异常。
在spring配置文件加入以下代码,实现类注入
遇到的问题:
解决方法:
版本:Struts2.2.3 Spring3.0 Hibernate3.3
myeclipse8.6
整合步骤如下:
1、新建web工程
2、添加spring
3、添加hibernate
4、添加struts2
5、配置文件
新建web工程
添加spring
添加hibernate
注:之前可以先添加dataSource 具体步骤:Window->Open Perspective->MyEclipse Database Explorer
切换到Database Explorer视图
对表进行Hibernate反向转换
添加struts2
向/WEB-INF/lib添加jar包
最小jar包
配置web.xml
<?xml version="1.0" encoding="UTF-8" ?> <web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>Struts Blank</display-name> <!-- 配置spring的监听器 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <!-- 开启监听 --> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
配置struts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!-- 指定Struts 2配置文件的DTD信息 --> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="default" extends="struts-default"> <!-- spring管理action class的值为applicationContext中action bean的id --> <action name="login" class="userAction" method="login"> <result name="success">success.jsp</result> <result name="input">index.jsp</result> </action> </package> </struts>
配置applicationContext.xml
<?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:aop="http://www.springframework.org/schema/aop" 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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"> </property> <property name="url" value="jdbc:mysql://10.6.12.123:3306/db_bbs"> </property> <property name="username" value="root"></property> <property name="password" value="123456"></property> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSource" /> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </prop> <prop key="hibernate.show_sql">true</prop><!-- show sql语句 --> </props> </property> <property name="mappingResources"> <list> <value>com/ideal/model/TbTopic.hbm.xml</value> <value>com/ideal/model/TbReply.hbm.xml</value> <value>com/ideal/model/TbModerator.hbm.xml</value> <value>com/ideal/model/TbMail.hbm.xml</value> <value>com/ideal/model/TbForum.hbm.xml</value> <value>com/ideal/model/TbFile.hbm.xml</value> <value>com/ideal/model/TbDisk.hbm.xml</value> <value>com/ideal/model/TbDirectory.hbm.xml</value> <value>com/ideal/model/TbUser.hbm.xml</value></list> </property></bean> <!-- 配置Hibernate的局部事物管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory"> <ref bean="sessionFactory" /> </property> </bean> <!-- 配置事物切面Bean --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="get*" read-only="true"></tx:method> <tx:method name="*"></tx:method> </tx:attributes> </tx:advice> <!-- 配置一个切入点,匹配指定包下所有以Impl结尾的类 --> <!-- 由于Spring注入的是接口,关联的是实现类。在Spring的配置文件加上下面代码,注明可以用实现类注入 <aop:config proxy-target-class=“true”></aop:config> --> <aop:config proxy-target-class="true"> <aop:pointcut id="idealPointcut" expression="execution(* com.ideal.service.impl.*Impl.*(..))"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="idealPointcut"/> </aop:config> <bean id="TbUserDAO" class="com.ideal.dao.TbUserDAO"> <property name="sessionFactory"> <ref bean="sessionFactory" /> </property> </bean> <bean id="TbTopicDAO" class="com.ideal.dao.TbTopicDAO"> <property name="sessionFactory"> <ref bean="sessionFactory" /> </property> </bean> <bean id="TbReplyDAO" class="com.ideal.dao.TbReplyDAO"> <property name="sessionFactory"> <ref bean="sessionFactory" /> </property> </bean> <bean id="TbModeratorDAO" class="com.ideal.dao.TbModeratorDAO"> <property name="sessionFactory"> <ref bean="sessionFactory" /> </property> </bean> <bean id="TbMailDAO" class="com.ideal.dao.TbMailDAO"> <property name="sessionFactory"> <ref bean="sessionFactory" /> </property> </bean> <bean id="TbForumDAO" class="com.ideal.dao.TbForumDAO"> <property name="sessionFactory"> <ref bean="sessionFactory" /> </property> </bean> <bean id="TbFileDAO" class="com.ideal.dao.TbFileDAO"> <property name="sessionFactory"> <ref bean="sessionFactory" /> </property> </bean> <bean id="TbDiskDAO" class="com.ideal.dao.TbDiskDAO"> <property name="sessionFactory"> <ref bean="sessionFactory" /> </property> </bean> <bean id="TbDirectoryDAO" class="com.ideal.dao.TbDirectoryDAO"> <property name="sessionFactory"> <ref bean="sessionFactory" /> </property> </bean> <!-- 向UserServiceImpl注入TbUserDAO --> <bean id="userServiceImpl" class="com.ideal.service.impl.UserServiceImpl"> <property name="userDAO"> <ref bean="TbUserDAO" /> </property> </bean> <!-- 向UserAction注入UserServiceImpl --> <bean id="userAction" class="com.ideal.action.UserAction"> <property name="userServiceImpl"> <ref bean="userServiceImpl" /> </property> </bean> </beans>
遇到的问题:
Caused by: org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type '$Proxy4 implementing com.ideal.service.interfaces.UserService,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised' to required type 'com.ideal.service.impl.UserServiceImpl' for property 'userServiceImpl'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [$Proxy4 implementing com.ideal.service.interfaces.UserService,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised] to required type [com.ideal.service.impl.UserServiceImpl] for property 'userServiceImpl': no matching editors or conversion strategy found at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:462) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1354) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1313) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1067) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:511) ... 35 more Caused by: java.lang.IllegalStateException: Cannot convert value of type [$Proxy4 implementing com.ideal.service.interfaces.UserService,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised] to required type [com.ideal.service.impl.UserServiceImpl] for property 'userServiceImpl': no matching editors or conversion strategy found at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:289) at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:154) at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:452) ... 39 more
解决的方法:
com.ideal.service.impl.UserServiceImpl实现的是com.ideal.service.interfaces.UserService接口,由于spring实现的是接口注入,关联的实现类。这里实现注入类,所以出现了异常。
在spring配置文件加入以下代码,实现类注入
<aop:config proxy-target-class="true"></aop:config>
遇到的问题:
org/objectweb/asm/Type异常 nested exception is java.lang.NoClassDefFoundError: org/objectweb/asm/Type
解决方法:
原因是Spring中的cglib-nodep-2.x.x.jar与Hibernate中的cglib-2.2.jar相冲突! 两种框架整合时Spring中的cglib-nodep-2.x.x.jar是必须的,取消Hibernate中的cglib-2.2.jar即可
相关文章推荐
- Struts+Spring+Hibernate--SSH整合实例
- SSH 框架整合的简单实例
- SSH的整合简单实例
- SSH整合实例
- SSH 整合 spring struts2整合 hello 实例教程 火推
- SSH整合实例
- SSH整合时出现的InvocationTargetException问题及实例讲解
- 使用MyEclipse整合ssh(Struts、Spring、Hibernate)三大框架(环境搭载+实例源码下载)
- Struts+Spring+Hibernate--SSH整合实例
- Struts Spring Hibernate (SSH) 整合实例
- SSH整合中正确配置以及实例
- SSH整合遇到的问题:$Proxy5 cannot be cast to -- SSH 事务采用注解无法获取Service实例bean
- SSH整合实例
- Struts+Spring+Hibernate,SSH整合实例
- SSH整合实例(1)
- jBPM4实战系列(二)jBPM4.4+ssh 整合配置及完整实例
- SSH整合实例代码含jar包
- SSH整合(一)Struts简单实例
- Struts Spring Hibernate (SSH) 整合实例
- (转载)Struts Spring Hibernate (SSH) 整合实例