Struts2
在web.xml中配置strut2的核心过滤器StrutsPrapareAndExecuteFilter
<filter>
<filter-name>struts2filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilterfilter-class>
filter>
<filter-mapping>
<filter-name>struts2filter-name>
/*
filter-mapping>
在src下创建struts.xml
<struts>
<constant name="struts.devMode" value="false" />
<package name="default" namespace="/" extends="struts-default">
package>
struts>
Spring
在web.xml中配置Spring的核心监听器ContextLoaderListener 指定配置文件的位置
<context-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:applicationContext.xmlparam-value>
context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
listener-class>
listener>
在src下创建applicationContext.xml
"http://www.springframework.org/schema/beans"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance" xmlns:cOntext=
"http://www.springframework.org/schema/context" xmlns:aop=
"http://www.springframework.org/schema/aop" xmlns:tx=
"http://www.springframework.org/schema/tx" xsi:schemaLocation=
"http:
http:
http:
http:
Hibernate
在src下创建hibernate.cfg.xml
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driverproperty>
<property name="hibernate.connection.url">jdbc:mysql:///databaseNameproperty>
<property name="hibernate.connection.username">rootproperty>
<property name="hibernate.connection.password">rootproperty>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialectproperty>
<property name="hibernate.show_sql">trueproperty>
<property name="hibernate.format_sql">trueproperty>
<property name="hibernate.hbm2ddl.auto">updateproperty>
session-factory>
hibernate-configuration>
在domain实体类 所在的目录创建Xxx.hbm.xml(hbm配置文件可以写哪里都可以,一个hbm可以映射多个类)
Spring整合Hibernate 依赖Spring的一个jar包(spring-orm-3.2.0.RELEASE.jar)
方式一:保留hibernate.cfg.xml配置文件,在applicationContext.xml中应用hibernate的配置文件
通过LocalSessionFactoryBean讲hibernate的配置,定义为spring一个bean(SessionFactory)
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml">property>
bean>
方式二: 将hibernate.cfg.xml中的内容写到spring的配置文件中(不需要单独hibernate配置文件)
将jdbc的连接参数,配置文件连接池,引入SessionFactory
在src下创建jdbc.properties,配置数据库参数
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///databaseName
jdbc.username=root
jdbc.password=root
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}">property>
<property name="jdbcUrl" value="${jdbc.url}">property>
<property name="user" value="${jdbc.username}">property>
<property name="password" value="${jdbc.password}">property>
bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource">property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialectprop>
<prop key="hibernate.show_sql">trueprop>
<prop key="hibernate.format_sql">trueprop>
<prop key="hibernate.hbm2ddl.auto">updateprop>
props>
property>
<property name="mappingDirectoryLocations">
<list>
<value>classpath:../../../domainvalue>
list>
property>
bean>
将SessionFactory注入到DAO代码,Spring为了简化Hibernate开发,提供了HibernateTemplate模板工具类
通过定义DAO继承HibernateDaoSupport类
在applicationContext.xml中将SessionFactory注入DAO中
<bean id="bookDAO" class="xx.xx.dao.BookDAO">
<property name="sessionFactory" ref="sessionFactory"/>
bean>
这样DAO就获得类HibernateTemplate工具类,DAO就可以通过模板操作数据库.
讲DAO注入到Service
在applicationContext.xml中将DAO注入Service中
<bean id="bookService" class="xx.xx.service.BookService">
<property name="bookDAO" ref="bookDAO">property>
bean>
采用注解进行声明事物管理
@Transactional
public class XxxService{...}
在applicationContext.xml中配置
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory">property>
bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
Strusts2整合Spring (通过 struts2-spring-plugin.jar 插件包,完成整合)
Struts2配置文件的加载顺序 Struts2 core包default.properties(常用属性)—>core包struts-default.xml(拦截器,结果集)–>
plugin包struts-plugin.xml(插件配置文件)–>自定义struts.properties–>自定义struts.xml–>web.xml
在struts2-spring-plugin.jar包里的struts-plugin.xml配置了新的ObjectFactory
<constant name="struts.objectFactory" value="spring" />
修改了Struts2 默认对象工厂
导入插件,造成了struts2 Action类 创建对象工厂发生了变化
方式一:由Struts2 自己管理Action的创建,自动装备Spring容器对象
name="addBook" class="xx.xx.action.AddBookAction" />
class属性配置完整类名,struts2 根据完整类名构造Action对象
当将struts.objectFactory=spring时,启用default.properties中struts.objectFactory.spring.autoWire=name
即启用对象工厂自动装配,将Spring容器对象,自动注入到Action中,用Struts2 Action属性去匹配Spring容器Bean id,
如果匹配,进行自动装配.注意:默认按照name注入,如果name不匹配,无法注入,容易出现空指针异常.
方式二:struts2将Action创建权交给Spring容器
先通过Struts2自己创建Action,如果找不到Action完整类名,就会去Spring容器去找,通过Spring bean构造Action
struts.xml
<action name="addBook" class="addBookAction">action>
applicationContext.xml
<bean id="addBookAction" class="xx.xx.action.AddBookAction" scope=prototype>
<property name="bookService" ref="bookService">property>
bean>
问题: struts2 管理Action创建,默认Action是多个实例的, spring管理Action创建 ,默认scope=singleton 单例 (使用Spring 管理Action 创建,必须将Action scope=prototype )
延迟加载
当数据存在关联时,查询数据表数据时,关联数据默认是延迟加载的.
Service层管理事务,在Service返回数据后,事务提交关闭,Session关闭,Action获得的对象成为托管对象,如果对象又是延迟加载的
会报org.hibernate.LazyInitializationException: could not initialize proxy - no Session
使用 OpenSessionInView 解决 : 将Session生命周期 延续到 web层,Action获得对象 ,成为持久对象,具有延迟加载能力
在web.xml 配置Spring 提供 OpenSessionInViewFitler
(OpenSessionInViewFilter 必须配置在 StrutsPrepareAndExecuteFilter 之前!否则无效)
<filter>
<filter-name>openSessionInViewFilterfilter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilterfilter-class>
filter>
<filter-mapping>
<filter-name>openSessionInViewFilterfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>