Spring4.X——搭建
一,Spring概念总结
spring是一个集成了许多第三方框架的大杂烩,其核心技术是IOC(控制反转,也称依赖注入)和AOP(面向切面编程),所以spring既是一个IOC容器,也是一个AOP框架。我们知道没有Spring,Struts和Hibernate可以很好的工作,在开篇中我把没有Spring的架构称为“独木桥”,有Spring的架构称为“阳光大道”。说白了,Spring解决了企业应用开发的复杂性,用基本的javaBean来完成EJB的事情,从大小和开销方向说,Spring都是轻量级的。Spring具有很多的优点:
1、使我们的分层更好。
SSH框架系统从职责上分为四层:表示层、业务逻辑层、数据持久层和域模块层(实体层)。
从上图我们可以看到Spring处于业务逻辑层,担任连接Struts和Hibernate桥梁的角色。系统的整个层次关系可以一目了然。
2、对象管理更好。
从上图,我们看到Spring将Transactions、Session以及业务层服务都放到了业务逻辑层来管理。系统的条理变得更加清晰,不仅使持久化层的职责更加单一,而且提高了系统的灵活性。
3、AOP
面向切面编程,AOP让开发人员创建非行为性的关注点,并将它们插入到应用代码红。公共服务(比如日志、持久性、事务等)就可以就可以分解成方面并应用到域对象上,同时不会增加域对象的对象模型的复杂性。
二,Spring框架的搭建
1.到spring官网http://projects.spring.io/spring-framework/下载spring插件,这里使用的是spring-framework-4.2.2.RELEASE版本;
2.导入spring插件jar包到项目的lib文件夹下:

注意:其中struts2-spring-plugin-2.3.30.jar和commons-logging-1.1.3.jar在Struts2的jar包里;
2.导入包后,到web.xml中配置spring的监听器:
<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>
3.然后我们要把spring和Struts连起来,在Struts.xml中配置如下代码:
<!-- include文件用于分割,实现多人并发不冲突 -->
<struts>
<!-- 告知Struts2运行时使用Spring来创建对象 -->
<!--指定Struts 2默认的ObjectFactory Bean,该属性默认值是spring-->
<constant name="struts.objectFactory" value="spring" />//name属性值不能随便乱写
<include file="s001.xml" />
<include file="s002.xml" />
<include file="s003.xml" />
</struts>
4.配置好Struts.xml后,还要到Action类中进行依赖注入(set注入\构造注入\接口注入)代码如下:
//声明service,但不给它创建具体的实现类的实例,
//因为:action不应该关注具体是谁来实现service
//具体service实现类是谁,我们使用spring注入进来
private IndexService is = null;
public void setIs(IndexService is) {
System.out.println("有人帮我们注入了service的实例:"+is);
this.is = is;
}
5.配置applicationContext.xml文件;
在解压后的文件夹路径下:spring-framework-2.5.6\samples\petclinic\test\org\springframework\samples\petclinic\jpa
找到applicationContext.xml文件并Copy到项目src路径下,并进行如下配置:
<?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
<!-- 类似于财务部门一样,类就是钱,所有需要类的实例都由srping去管理 -->
<bean id="myIndexAction" class="ssh.action.IndexAction" scope="prototype">
<!-- setIs(myIndexService) -->
<property name="is" ref="myIndexService"/>
</bean> <!-- myIndexService = new ssh.service.IndexServiceImpl() -->
<bean id="myIndexService" class="ssh.service.IndexServiceImpl" scope="prototype">
<property name="id" ref="myIndexDao"/>
</bean> <bean id="myIndexDao" class="ssh.dao.IndexDaoImpl" scope="prototype">
<property name="c" ref="myConnection"></property>
</bean> <bean id="myConnection" class="ssh.util.MyConnectionImpl_SQLSERVER" scope="prototype">
</bean>
</beans>
在这里基本上搭建完成啦,可以去试试运行吧!
Spring4.X——搭建的更多相关文章
- [JAVA教程] 2016年最新spring4框架搭建视频教程 【尚学堂】
Spring4框架 主讲:邹波 类型:SSH 适合对象:学习完javase.数据库技术.jdbc者 Spring4.0作为一个广泛使用的开源框架,它由Rod Johnson创建.它是为了解决企业应用开 ...
- spring4+srpingmvc+mybatis基本框架(app后台框架搭建一)
前言: 随着spring 越来越强大,用spring4来搭建框架也是很快速,问题是你是对spring了解有多深入.如果你是新手,那么在搭建的过程中可以遇到各种各样奇葩的问题. SSM框架的搭建是作为我 ...
- spring4+springmvc+mybatis基本框架(app后台框架搭建一)
前言: 随着spring 越来越强大,用spring4来搭建框架也是很快速,问题是你是对spring了解有多深入.如果你是新手,那么在搭建的过程中可以遇到各种各样奇葩的问题. SSM框架的搭建是作为我 ...
- SSM框架整合环境构建——基于Spring4和Mybatis3
目录 环境 配置说明 所需jar包 配置db.properties 配置log4j.properties 配置spring.xml 配置mybatis-spring.xml 配置springmvc.x ...
- 用cxf开发restful风格的WebService
我们都知道cxf还可以开发restful风格的webService,下面是利用maven+spring4+cxf搭建webService服务端和客户端Demo 1.pom.xml <projec ...
- spring4+hibernate4+maven环境搭建
本文主要介绍利用maven搭建spring4+hibernate4开发环境. 首先我们创建一个maven项目,具体步骤就不详细介绍了,看看我们pom.xml文件 <project xmlns=& ...
- 《转》Spring4 Freemarker框架搭建学习
这里原帖地址:http://www.cnblogs.com/porcoGT/p/4537064.html 完整配置springmvc4,最终视图选择的是html,非静态文件. 最近自己配置spring ...
- spring mvc4.1.6 + spring4.1.6 + hibernate4.3.11 + mysql5.5.25 开发环境搭建及相关说明
一.准备工作 开始之前,先参考上一篇: struts2.3.24 + spring4.1.6 + hibernate4.3.11 + mysql5.5.25 开发环境搭建及相关说明 struts2.3 ...
- J2EE开发框架搭建(2) - springmvc4 + spring4 + hibernate4 整合
1. 打开hqhop-framework-parent项目下的pom.xml文件.加入springmvc4 , spring4 , hibernate4 ,以及数据源druid的依赖包,插件,依赖包版 ...
随机推荐
- Servlet规范简介——web框架是如何注入到Servlet中的
Servlet规范简介--web框架是如何注入到Servlet中的 引言 Web框架一般是通过一个Servlet提供统一的请求入口,将指定的资源映射到这个servlet,在这个servlet中进行框架 ...
- sql中查询中的when...then 语句
),jbrq, ),'/','-')as jbrq, ' then '启用' when ' then '未启用' else '修改' end cbzt,shzt from jc_yscbfxb sel ...
- 【NuGet】打包上传一条龙服务
昨天写了搭建自己的NuGet程序源,但是领导不满意之前的打包上传~~,无奈只能去爬点思路了,这里参考的其他博文,但是还是想写下来. 第一步.建立一个批处理文件 在文件里,有三条命令: nuget pa ...
- HDU-SupportOrNot训练实录
菜鸡队训练实录. 现场赛记录: 2016:[名称:奖项/排名] ZJPSC:Gold/1 CCPC中南邀请赛:Gold/1 ICPC Dalian:Gold/24 ICPC Beijing:Gold/ ...
- JavaScript使用封装
基本封装方法 请看下面的例子: var Person = function(name,age){ this.name = name; this.age = age || "未填写" ...
- ViewPager取消左右滑动切换功能
ViewPager取消左右滑动切换功能 最近做项目要求某种情况下ViewPager不能滑动,那么我们只需要重写这个方法就可以禁止ViewPager滑动 IndexViewPager.java: imp ...
- [8.2] Robot in a Grid
Imagine a robot sitting on the upper left corner of grid with r rows and c columns. The robot can on ...
- javascript获取childNodes详情,删除空节点
chidNodes返回的是node的集合, 每个node都包含有nodeType属性. nodeType取值: 元素节点:1 属性节点:2 文本节点:3 注释节点:8 页面上是由无数个节点组成 ...
- 【BZOJ】3996: [TJOI2015]线性代数
题意 给出一个\(N \times N\)的矩阵\(B\)和一个\(1 \times N\)的矩阵\(C\).求出一个\(1 \times N\)的01矩阵\(A\),使得\[ D = ( A * B ...
- [RxJava^Android]项目经验分享 --- RxLifecycle功能实现分析(二)
接着上一篇文章的内容,这篇文章一边分析RxLifecycle的实现原理,一边学习RxJava操作符. 首先RxLifecycle在基础类里定义BehaviorSubject并绑定Activity或 ...