SSH三大框架整合配置详细步骤(3)
5 配置Spring2.5
5.1 基础配置
1) 导入spring包。下载spring-framework-2.5.6并解压后,在spring-framework-2.5.6"dist目录下找到spring.jar,引入到工程中。
说明:spring.jar是包含有完整发布的单个jar包,spring.jar中包含除了 spring-mock.jar里所包含的内容外其它所有jar包的内容,因为只有在开发环境下才会用到spring-mock.jar来进行辅助测试,正式应用系统中是用不得这些类的。除了spring.jar文件,Spring还包括有其它13个独立的jar包,各自包含着对应的Spring组件,用户可以根据自己的需要来选择组合自己的jar包,而不必引入整个spring.jar的所有类文件。这里,为了使用方便,我们引入整个spring.jar。
2) 配置web.xml文件。Jar包引入完成后,就开始配置spring了,首先修改web.xml文件,增加如下代码:
<!--
* 从类路径下加载spring的配置文件, 多个配置文件可以用逗号和空格区分
* classpath: 关键字特指类路径下加载-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:applicationContext*.xml
</param-value>
</context-param>
在这里,我们指定了spring配置文件的路径,即WEB-INF/classes/spring目录下的所有以applicationContext开头命名的xml文件。
3) 在src下面新建applicationContext.xml文件。首先给这个文件加上spring的标头:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
</beans>
注意:标头是2.5的 不要引入2.0, 错了可能Spring就不能正确加载。
5.2 示例
Spring基本配置完毕,让我们建个示例来测试一下吧,首先在test.spring包下创建两个java文件:TUser.java、SpringTest.java。
TUser.java:
2
3public class TUser implements java.io.Serializable {
4 private String username;
5 private String allname;
6 private String address;
7
8 public String getUsername() {
9 return this.username;
10 }
11 public void setUsername(String username) {
12 this.username = username;
13 }
14 public String getAllname() {
15 return this.allname;
16 }
17 public void setAllname(String allname) {
18 this.allname = allname;
19 }
20 public String getAddress() {
21 return this.address;
22 }
23 public void setAddress(String address) {
24 this.address = address;
25 }
26}
27
SpringTest.java:
2
3import org.springframework.context.ApplicationContext;
4import org.springframework.context.support.ClassPathXmlApplicationContext;
5
6public class SpringTest {
7 public static void main( String[] args ) {
8 //加载spring配置文件,初始化IoC容器
9 ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
10 //从容器 接管Bean
11 TUser user = (TUser) ac.getBean("TUser");
12 //输出欢迎信息
13 System.out.println( "Hello:" + user.getUsername() + ";u is in " + user.getAddress() + " ; and u is " + user.getAllname() );
14 }
15}
16
创建完毕后,就剩最后一步了,在applicationContext.xml中配置一个bean,在xml中增加如下代码:
<property name="username" value="小张"></property>
<property name="allname" value="张三"></property>
<property name="address" value="青岛市"></property>
</bean>
好了,下面运行一下吧,右键单击SpringTest.java选择run as àJava Application,运行结果如下:
输出 Hello:小张;u is in 青岛市; and u is 张三
如果你的运行结果和上面一样,且没有异常,则说明Spring配置成功了。是不是很简单?不要骄傲,重要的是Spring与Hibernate、Struts的整合。继续吧!
5.3 整合Struts
Spring与Struts的整合其实就是把Struts的Action类交给Spring来管理,下面开始吧!
1) 导入jar包。在Struts2.1.6的lib目录中找到struts2-spring-plugin-2.1.6.jar,引入到工程中。
2) 配置web.xml文件。在web.xml中加入以下代码:
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
1) 现在就来看如何把struts的action交给spring。以struts示例中的login.action为例,首先创建一个LoginAction类的Bean。在applicationContext.xml中增加如下代码:
<bean id="loginAction" class="test.LoginAction" scope="prototype">
</bean>
这里,我们把这个bean的id设为loginAction。Scope设为prototype,含义是每一次请求创建一个LoginAction类的实例,Scope还有另一个值“singleton”意为“单例模式”。
接下来修改struts.xml文件,把原来login.action的配置做如下修改:
把<action name="login" class=" test.LoginAction ">
改为
<action name="login" class="loginAction">
注意到有什么区别了吗?class值设为了loginAction,即LoginAction类的bean的ID。这样我们就把LoginAction类交给了spring管理。至于具体是怎么处理的,秘密在struts2-spring-plugin-2.1.6.jar中,有空自己就去研究吧,现在会用就可以了。
5.4 整合Hibernate
Spring整合Hibernate主要是对hibernate的Session进行管理,包含Session的创建、提交、关闭的整个生命周期。Spring对事务的管理应用了AOP的技术,配置前请先了解一下AOP的知识。
1) 配置sessionFactory,让spring来创建Session。在applicationContext.xml中增加如下代码:
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:spring/hibernate.cfg.xml</value>
</property>
</bean>
我们原来是用HibernateSessionFactory.java来创建Session的,现在删除即可,交给Spring创建。这里,创建了一个Session工厂类的Bean,其ID为“sessionFactory”。
2) 配置事务管理器。增加如下代码:
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
这里创建了一个id为transactionManager的事务管理器,它匹配一个session工厂,<ref bean="sessionFactory"/>这个sessionFactory是指session工厂的ID。
3) 对事务管理器进行事务设置。增加如下代码:
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="del*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
这里创建了一个advice(通知),对事务管理器进行事务设置,这里意思是指,对于以save、del、update开头的方法应用事务。
4) 下面就把事务应用到具体的类。看如下代码:
<aop:pointcut id="smMethod"
expression="execution(* test.service.impl.*.*(..))"/>
<aop:advisor pointcut-ref="smMethod" advice-ref="smAdvice"/>
</aop:config>
这里配置的作用是把我们上面创建的advice应用到具体的类中。以上代码的意思指,给test.service.impl下的所有类的所有方法应用smAdvice。
5) 示例:使用Session。
配置基本完毕了,自己去测试吧,这里就不先写了。
SSH三大框架整合配置详细步骤(3)的更多相关文章
- SSH三大框架整合配置详细步骤(2)
4 配置Hibernate Hibernate MySql连接配置 在Hibernate中,可以配置很多种数据库,例如MySql.Sql Server和Oracle,Hibernate MySql连接 ...
- SSH三大框架整合配置详细步骤(1)
配置Struts2.0 3.1 基础配置 1)引入Struts必需的五个jar包.下载struts-2.1.6-all.zip解压后,struts-2.1.6\lib目录下是struts所有的相关ja ...
- SSH三大框架整合配置详解
首先,三大框架整合,肯定是要导入相当多的jar包,这是不容置疑的! 这里就不一一列举了,直接截图吧: (1) 基于配置文件的整合: 第一步:我们需要在we ...
- Maven SSH三大框架整合的加载流程
<Maven精品教程视频\day02视频\03ssh配置文件加载过程.avi;> 此课程中讲 SSH三大框架整合的加载流程,还可以,初步接触的朋友可以听一听. < \day02视频\ ...
- SSH三大框架整合案例
SSH三大框架的整合 SSH三个框架的知识点 一.Hibernate框架 1. Hibernate的核心配置文件 1.1 数据库信息.连接池配置 1.2 Hibernate信息 1.3 映射配置 ...
- JavaWeb_(SSH)三大框架整合struts+hibernate+spring_Demo
三大框架整合 一.SSH导包 二.书写Spring 三.书写Struts 四.整合Spring与Struts 五.书写(与整合)Hibernate.引入c3p0连接池并使用hibernate模板 六. ...
- 关于ssh三大框架整合的碎碎念
三大框架整合,无非就是一个导jar包,修改配置文件的过程.完了就没事了. 还是有很多细节性的问题 比如在spring中写applicationContext.xml文件时不提示: 解决方法如下: 如果 ...
- 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:SSM(Spring+Spring MVC+MyBatis)框架整合搭建详细步骤
因为 Spring MVC 是 Spring 框架中的一个子模块,所以 Spring 与 SpringMVC 之间不存在整合的问题.实际上,SSM 框架的整合只涉及 Spring 与 MyBatis ...
- SSH三大框架整合步骤
Struts2:需要整合的第一个框架: 1.创建一个动态web项目 2.导入struts2必须的jar 放到 lib目录下 ,再 build path 添加web工程中 3.配置struts2的核心配 ...
随机推荐
- 如何用纯 CSS 创作一种有削铁如泥感觉的菜单导航特效
效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/XqYroe 可交互视频教 ...
- Python旅途——简单语法
1. 前言 在我们对环境以及pycharm安装好之后,我们就可以开始我们的Python之旅了,那么,我们学习一门语言应该如何开始呢?就像我们学习汉语一样,从abcd这些拼音学起,而对于我们Python ...
- 杭电 4004 The Frog's Games 青蛙跳水 (二分法,贪心)
Description The annual Games in frogs' kingdom started again. The most famous game is the Ironfrog T ...
- Cable master 求电缆的最大长度(二分法)
Description Inhabitants of the Wonderland have decided to hold a regional programming contest. The J ...
- 【BZOJ 1076】[SCOI2008]奖励关(期望)
Description 你正在玩你最喜欢的电子游戏,并且刚刚进入一个奖励关.在这个奖励关里,系统将依次随机抛出k次宝物, 每次你都可以选择吃或者不吃(必须在抛出下一个宝物之前做出选择,且现在决定不吃的 ...
- Leetcode 241.为运算表达式设计优先级
为运算表达式设计优先级 给定一个含有数字和运算符的字符串,为表达式添加括号,改变其运算优先级以求出不同的结果.你需要给出所有可能的组合的结果.有效的运算符号包含 +, - 以及 * . 示例 1: 输 ...
- Food Delivery (区间DP)
When we are focusing on solving problems, we usually prefer to stay in front of computers rather tha ...
- HDU 1102 Kruscal算法
题目大意:给定村庄的数量,和一个矩阵表示每个村庄到对应村庄的距离,矩阵主对角线上均为1 在给定一个数目Q,输入Q行之间已经有通道的a,b 计算还要至少修建多少长度的轨道 这道题目用Kruscal方法进 ...
- [luoguP1773] 符文之语_NOI导刊2010提高(02)(DP)
传送门 f[i][j]表示前i个数余数为j的最优解 sum[i][j]表示字符串i~j所构成的数 #include <cstdio> #include <cstring> #d ...
- 洛谷P2527 [SHOI2001]Panda的烦恼
题目描述 panda是个数学怪人,他非常喜欢研究跟别人相反的事情.最近他正在研究筛法,众所周知,对一个范围内的整数,经过筛法处理以后,剩下的全部都是质数,不过panda对这些不感兴趣,他只对被筛掉 ...