spring和其它框架的整合

[writer:杰辰 ]

本文记录了 Spring 与 Struts2, Hibernate, Mybaits 的整合

单独配置 Spring

1.在 src 下创建 applicationContext.xml,并导入约束

1
2
3
4
5
6
7
8
9
10
11
12
13
<?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: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>

2.配置 spring 随项目启动

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">

<!-- 让spring随web启动而创建的监听器 -->
<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>

...

</web-app>

Spring与Strus2的整合

整合原理:action 的创建与组装由 spring 负责

1.导包
struts2-spring-plugin-2.3.24.jar (位于 struts2 的lib中)

2.配置常量

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<!-- 在 struts.xml 中添加 -->
<?xml version="1.0" encoding="UTF-8"?>

"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>

<!--
struts.objectFactory = spring 将action的创建交给spring容器
struts.objectFactory.spring.autoWire = name spring负责装配Action依赖属性
-->
<constant name="struts.objectFactory" value="spring"/>

...

</struts>

3.spring 负责创建 action 以及组装

1
2
3
4
5
6
7
8
9
10
<!-- 在 applicationContext.xml 中完成 action 的组建,如下 -->
<!-- 注意:Action对象作用范围一定是多例的.这样才符合struts2架构 -->
<bean name="userAction" class="com.paxsz.web.action.UserAction" scope="prototype">
<property name="us" ref="userService"/>
</bean>

<!-- 在 struts.xml 中使用 -->
<action name="UserAction_*" class="userAction" method="{1}">
<result>/success.jsp</result>
</action>

Spring与hibernate的整合

整合原理:将 sessionFactory 对象交给 spring 容器管理

1.在 src 下创建 db.properties

1
2
3
4
jdbc.jdbcUrl=jdbc:mysql:///my_db
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.password=654321

2.在 applicationContext.xml 中配置连接池

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?xml version="1.0" encoding="UTF-8"?>
<beans>

<!-- 读取db.properties文件 -->
<context:property-placeholder location="classpath:db.properties"/>

<!-- 配置c3p0连接池 -->
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"/>
<property name="driverClass" value="${jdbc.driverClass}"/>
<property name="user" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
</bean>

...
</beans>

3.将 SessionFactory 配置到 spring 容器中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?xml version="1.0" encoding="UTF-8"?>
<beans>

...

<!-- 将SessionFactory配置到spring容器中 -->
<bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 将连接池注入到sessionFactory, hibernate会通过连接池获得连接 -->
<property name="dataSource" ref="dataSource"/>
<!-- 配置hibernate基本信息 -->
<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>
<!-- 引入orm元数据,指定orm元数据所在的包路径,spring会自动读取包中的所有配置 -->
<property name="mappingDirectoryLocations" value="classpath:com/paxsz/bean"/>
</bean>

...

</beans>

4.配置事务管理

1
2
3
4
5
6
7
8
9
10
11
12
13
大专栏  spring和其它框架的整合"line">14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?xml version="1.0" encoding="UTF-8"?>
<beans>

...

<!-- 核心事务管理器 -->
<bean name="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>

<!-- 配置事务通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>
<tx:method name="persist*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>
<tx:method name="update*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>
<tx:method name="modify*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>
<tx:method name="delete*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>
<tx:method name="remove*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>
<tx:method name="get*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true"/>
<tx:method name="find*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true"/>
</tx:attributes>
</tx:advice>

<!-- 配置将通知织入目标 -->
<aop:config>
<!-- 配置切点 -->
<aop:pointcut id="pc" expression="execution(* com.paxsz.service.*ServiceImpl.*(..))"/>
<!-- 配置切面 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="pc"/>
</aop:config>

<!-- 向 dao 中注入 sessionFactory ,XXXDaoImpl 继承于 HibernateDaoSupport -->
<bean name="userDao" class="com.paxsz.dao.impl.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>

...

</beans>

5.扩大session作用范围
为了避免使用懒加载时出现 no-session 问题,需要扩大session的作用范围

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

<?xml version="1.0" encoding="UTF-8"?>
<web-app>

...

<!-- 扩大session作用范围
注意: 任何filter一定要在struts的filter之前调用
-->
<filter>
<filter-name>openSessionInView</filter-name>
<filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>openSessionInView</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

...

</web-app>

Spring与Mybatis的整合

整合原理:将 SqlSessionFactory 对象交给 spring 容器管理

1.导包
mybatis-spring-1.2.2.jar

2.SqlMapConfig.xml 的配置

1
2
3
4
5
6
7
8
9
10
11
12
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 设置别名 -->
<typeAliases>
<!-- 指定扫描包,会把包内所有的类都设置别名,别名的名称就是类名,大小写不敏感 -->
<package name="cn.paxsz.pojo" />
</typeAliases>

</configuration>

3.在 spring 中配置 SqlSessionFactory

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<!-- 在 applicationContext.xml 中添加 -->
<?xml version="1.0" encoding="UTF-8"?>
<beans>

<!-- 1.省略 dataSource,见上面 -->

<!-- 2.配置SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 配置mybatis核心配置文件 -->
<property name="configLocation" value="classpath:SqlMapConfig.xml" />
<!-- 配置数据源 -->
<property name="dataSource" ref="dataSource" />
</bean>

...

</beans>

3.配置 mapper 代理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<!-- 在 applicationContext 中添加 -->
<?xml version="1.0" encoding="UTF-8"?>
<beans>

...

<!-- 两种方式配置 mapper 代理 -->

<!-- ① 单独配置 Mapper 代理对象 -->
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<!-- 配置 Mapper 接口 -->
<property name="mapperInterface" value="cn.itcast.mybatis.mapper.UserMapper" />
<!-- 配置 sqlSessionFactory -->
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>

<!-- ② 扫描包方式配置代理,每个 mapper 代理对象的 id 就是类名,首字母小写 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 配置 Mapper 接口 -->
<property name="basePackage" value="cn.itcast.mybatis.mapper" />
</bean>

...


</beans>

猜你喜欢

转载自www.cnblogs.com/liuzhongrong/p/12263208.html
今日推荐