Struts2
在web.xml中配置strut2的核心过滤器StrutsPrapareAndExecuteFilter
<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>
在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>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
在src下创建applicationContext.xml
<beans xmlns="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://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
</beans>
Hibernate
在src下创建hibernate.cfg.xml
<hibernate-configuration>
<!-- JDBC基本连接参数 -->
<session-factory> <!-- 理解为连接池 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///databaseName</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<!-- 配置方言 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 常见其它配置 -->
<property name="hibernate.show_sql">true</property> <!-- 控制台上打印SQL -->
<property name="hibernate.format_sql">true</property> <!-- 控制台输出时,对SQL语句格式化 -->
<property name="hibernate.hbm2ddl.auto">update</property> <!-- 自动建表 -->
</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 : SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- 加载hibernate 配置文件 -->
<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"/>
<!-- 配置c3p0连接池 -->
<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">
<!--**将连接池注入到sessionFactory**-->
<property name="dataSource" ref="dataSource"></property>
<!--**在applicationContext.xml的sessionFactory中添加hibernate的常用属性(方言,格式化SQL,显示SQL......)**-->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<!--**hbm映射文件的引入,导入指定目录下方的所有hbm映射文件**-->
<property name="mappingDirectoryLocations">
<list>
<value>classpath:../../../domain</value>
</list>
</property>
</bean>
将SessionFactory注入到DAO代码,Spring为了简化Hibernate开发,提供了HibernateTemplate模板工具类
通过定义DAO继承HibernateDaoSupport类
在applicationContext.xml中将SessionFactory注入DAO中
<!-- 将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中
<!-- 将DAO 注入到Service -->
<bean id="bookService" class="xx.xx.service.BookService">
<property name="bookDAO" ref="bookDAO"></property>
</bean>
采用注解进行声明事物管理
@Transactional
public class XxxService{...}
在applicationContext.xml中配置
<!-- spring 采用声明式事务管理 -->
<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
<!-- Make the Spring object factory the automatic default -->
<constant name="struts.objectFactory" value="spring" />
修改了Struts2 默认对象工厂
导入插件,造成了struts2 Action类 创建对象工厂发生了变化
方式一:由Struts2 自己管理Action的创建,自动装备Spring容器对象
<action 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
<!-- class属性 伪类名 -->
<action name="addBook" class="addBookAction"></action>
applicationContext.xml
<!-- 配置Action 创建 -->
<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 之前!否则无效)
<!-- openSessionInViewFilter -->
<filter>
<filter-name>openSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>openSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>