SSH框架整合(Spring整合Struts2,和Spring整合Hibernate)
2017-05-05 13:55
375 查看
SSH整合配置:
导包:共40个jar包,其中:40=41-1(重复)
Hibernate5(9):hibernate解压目录/lib/required
struts2.3(14):
struts2解压目录/apps/blank.war(空白项目)
注意:struts2.3与hibernate5中有一个重复的jar包,将较低版本删除。
struts2-spring-plugin-xxx.jar (spring整合struts2),
一旦导入该包,struts2框架启动时就会寻找spring容器。如果找不到将会报错。所以在搭建框架时.如果导 入了该包.需要先配置spring。再配置struts。
spring4(17):
4+2;
spring-jdbc、spring-tx、spring-orm;
Druid连接池、MySql数据库驱动;
spring-aop、spring-aspects;
aop联盟、aspectJ织入;
spring-test、junit4;
spring-web(让spring容器的创建与项目的启动绑定)
web(1):
jstl.jar
Spring整合Web项目:
在Web项目启动时创建Spring容器,只创建一次原理:spring提供了ApplicationContextListener.。ServletContext对象的创建与销毁监听器。
spring在监听器的监听创建ServletContext对象的方法中,创建了spring容器。
在监听器的销毁方法中关闭spring容器。
换句话讲,就是将spring容器的生命周期与ServletContext进行绑定。
配置监听器&指定配置文件路径 web.xml中:
<!-- 让Spring容器随项目的启动而创建 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 指定Spring配置路径 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext_*.xml</param-value> </context-param>
Struts2整合Web项目:
编写Action:配置struts.xml:src目录下
<struts> <!-- 开启 struts开发模式 struts.devMode = false--> <constant name="struts.devMode" value="true" /> <!-- 开启使用Spring对象工厂 --> <constant name="struts.objectFactory" value="spring" /> <package name="XXX" namespace="/" extends="struts-default"> <action name="UserAction_*" class="userAction" method="{1}"> <result>/success.jsp</result> <result name="index">/index.jsp</result> </action> </package> </struts>
配置入口过滤器 web.xml中:
<!-- struts2配置入口过滤器 --> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>openSessionView</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
Spring整合Struts2:
整合原理:整合方式1(不使用):Action依然由struts2创建。Action对象中的依赖由Spring注入。
配置常量:
struts.objectFactory=spring,开启使用spring对象工厂
struts.objectFactory.spring.autoWire=name,使用spring容器自动注入依赖属性,根据属性名自动注入 依赖
自动注入属性:属性名与bean的name名一致,spring就可以自动注入属性。
整合方式2:由spring创建action以及组装依赖关系。
struts2只负责从spring获取action
配置常量:struts.objectFactory=spring,开启使用spring对象工厂,前面已经配置。
将Action和Service配置到spring容器:
ApplicationContext_action.xml中:
<!-- 配置Action --> <bean name="userAction" class="cn.ssh.demo.action.UserAction" scope="prototype"> <property name="userService" ref="userService"></property> </bean>
ApplicationContext_service.xml中:
<!-- 配置Service --> <bean name="userService" class="cn.ssh.demo.service.impl.UserServiceImpl"> <property name="userDao" ref="userDao"></property> </bean>
说明:struts2中class属性
Action中的class属性,填写完整类名,struts2会获得类并创建对象;
填写bean的name,struts2会从spring容器中获取对应的bean name的对象
将SessionFactory交给Spring管理:
方式1(不使用):配置信息放在hibernate.cfg.xml中,然后在spring配置sessionFactory时,注入 hibernate的配置信息。方式2:省略hibernatecfg.xml,在spring中直接配置,因此ApplicationContext_dao.xml中:
<!-- 读取外部 properties文件 --> <context:property-placeholder location="classpath:db.properties" /> <!-- 配置 数据源 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="${jdbc.driverClass}" /> <property name="url" value="${jdbc.jdbcUrl}" /> <property name="username" value="${jdbc.user}" /> <property name="password" value="${jdbc.password}" /> </bean> <!-- 将hibernate配置信息放置在Spring中 --> <bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <!-- 注入C3P0连接池 --> <property name="dataSource" ref="dataSource" /> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <!-- 可选配置 --> <prop key="hibernate.show_sql">true</prop> <!-- 是否显示sql --> <prop key="hibernate.format_sql">true</prop> <!-- 是否格式化sql --> <prop key="hibernate.hbm2ddl.auto">update</prop> <!-- 是否由hibernate生成ddl语句,以及如何生成 --> </props> </property> <!-- 引入 映射文件 引入 pojo目录下的所有映射文件 --> <property name="mappingDirectoryLocations" value="classpath:cn/ssh/demo/pojo" /> </bean> <!-- 配置Dao --> <bean name="userDao" class="cn.ssh.demo.dao.impl.UserDaoImpl"> <property name="sessionFactory" ref="sessionFactory"></property> </bean>数据库信息,db.properties中:
jdbc.jdbcUrl=jdbc:mysql:///ssh jdbc.driverClass=com.mysql.jdbc.Driver jdbc.user=root jdbc.password=root
AOP事务管理:
ApplicationContext_trans.xml中配置 :<!-- 配置核心事务管理器 --> <bean name="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> <!-- 将sessionFactory注入到事务管理器中 --> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 配置事务通知 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="save*" isolation="REPEATABLE_READ" read-only="false" propagation="REQUIRED" /> <tx:method name="delete*" isolation="REPEATABLE_READ" read-only="false" propagation="REQUIRED" /> <tx:method name="update*" isolation="REPEATABLE_READ" read-only="false" propagation="REQUIRED" /> <tx:method name="find*" isolation="REPEATABLE_READ" read-only="true" propagation="REQUIRED" /> <tx:method name="*" isolation="REPEATABLE_READ" read-only="false" propagation="REQUIRED" /> </tx:attributes> </tx:advice> <!-- 配置事务切面 --> <aop:config> <!-- 配置切点 --> <aop:pointcut expression="execution(* cn.ssh.demo.service.impl.*.*(..))" id="txPC" /> <!-- 配置切面 --> <aop:advisor advice-ref="txAdvice" pointcut-ref="txPC" /> </aop:config>
解决懒加载使用时的no-session问题:
首先,Hibernate查询优化:使用懒加载策略效率更高。问题解决:
确保在JSP环境中session处于开启状态。
(Spring已实现)在使用session的service层不关闭session。将session的关闭转移到过滤器的后处理代码 中。
配置过滤器 web.xml中:
<!-- 配置struts2以外的过滤器时,务必配置在struts2之前 --> <!-- 解决 no-sesison问题 --> <filter> <filter-name>openSessionView</filter-name> <filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class> </filter>
未完待续……
相关文章推荐
- 整合Struts2 Spring Hibernate,搭建SSH框架
- SSH框架网上商城项目第1战之整合Struts2、Hibernate4.3和Spring4.2
- SSH框架的整合详解(Struts2 + Spring + Hibernate)
- Struts2,Spring3,Hibernate4整合--SSH框架
- SSH框架整合(Spring整合Struts2,和Spring整合Hibernate)
- Struts2与Spring、Hibernate三者整合的过程示例
- Spring2.5+Struts2+Jpa(Hibernate实现)整合之一
- Spring2.5+Struts2+Jpa(Hibernate实现)整合之四
- Struts2 Spring Hibernate 的简单整合
- struts2 spring3 hibernate3整合
- Struts2 Spring 整合, Spring 使用 configLocation属性 联接hibernate.cfg.xml 找到不Action
- 虚拟数据层:Struts2、Hibernate、Spring整合的泛型DAO
- Struts2整合Spring Hibernate的CRUD的实例
- 最简单的 Struts2 Spring2 Hibernate3 整合 实例
- Struts2、hibernate和spring下载,整合所需jar包
- SSH2(Struts2、Spring3与Hibernate3)的整合
- 虚拟数据层 Struts2、Hibernate、Spring整合的泛型DAO Version 2010.9.27
- MyEclipse下struts2、spring、hibernate整合
- Spring2.5+Struts2+Jpa(Hibernate实现)整合之三
- JSON,Struts2,Spring,Hibernate,Extjs整合应用案例