版权声明:本文为博主原创文章,欢迎一起学习交流。 https://blog.csdn.net/ym01213/article/details/85728568
SSH项目配置文件加载过程
整合SSH
1、pom文件中锁定版本
2、搭建struts2环境
- 创建struts.xml
- web.xml中配置核心过滤器
<!-- 过滤器 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
注意struts版本在2.5之上的,过滤器是org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter,2.1.3版本后是org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter。
3、搭建spring环境
- 创建配置文件applicationContext.xml
- web.xml中配置监听器
<!-- 配置监听器:默认加载WEB-INF/applicationContext.xml -->
<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>
4、搭建Hibernate环境
- 配置hibernate.cfg.xml
<hibernate-configuration>
<session-factory>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<!-- hbm2ddl.auto: 生成表结构的策略配置 update(最常用的取值): 如果当前数据库中不存在表结构,那么自动创建表结构.
如果存在表结构,并且表结构与实体一致,那么不做修改 如果存在表结构,并且表结构与实体不一致,那么会修改表结构.会保留原有列. -->
<property name="hbm2ddl.auto">update</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 引入ORM 映射文件 填写src之后的路径 -->
<mapping resource="xxx.hbm.xml" />
</session-factory>
</hibernate-configuration>
5、整合struts2和spring
关键点:action对象的创建,交给spring;
- 创建action类
- 将action对象配置到spring配置文件中;
- struts.xml配置文件中,action标签的class属性值配置为spring工厂中action的id;
6、整合spring和hibernate
- 配置数据源datasource;
<!-- 加载属性配置文件 -->
<context:property-placeholder location="classpath:db.properties" ></context:property-placeholder>
<!-- 配置DataSource -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
- 配置sessionFactory
<!-- 配置sessionFactory对象 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.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>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
为了保证getCurrentSession方法获取Session实例
<prop key="current_session_context_class">thread</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>zafu/domain/User.hbm.xml</value>
<value>zafu/domain/FeedbackMsg.hbm.xml</value>
<value>zafu/domain/Carbondata.hbm.xml</value>
</list>
</property> -->
<property name="configLocations" value="classpath:hibernate.cfg.xml"></property>
</bean>
- 配置事务管理:(顶级接口PlatFormTransactionManager)
- JDBC: DataSourceTransactionManager
- Hibernate: HibernateTransactionManager
<!-- 声明Hibernate事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
- 事务管理
<!-- xml方式管理事务 -->
<!-- 定义事务通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 匹配业务类中的方法 -->
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<aop:config>
<!-- 配置切点,真正被增强的方法 -->
<aop:pointcut id="cut" expression="execution(* cn.com.plagh.service.*.*(..))" />
<!-- 配置切面,通知+切点 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="cut" />
</aop:config>
<!-- 注解方式管理事务 -->
<!-- 1.开启注解驱动 2.注解作用到类或方法上@Transactional -->
<tx:annotation-driven transaction-manager="transactionManager" />