一、简介:
spring框架,是进行对象管理,对象关联,解耦的一个中间层框架。SSH(Struts+Spring+hibernate)三大Spring在中间就起着一个承上启下的作用。
Spring是一个轻量级的IOC和AOP容器框架:
1、轻量级:程序实现不是很复杂,代码不是很多,占用资源不是很多,没有侵入性;
2、IOC(Inversion ofControl 控制反转):对象创建责任的反转(重点,核心);
3、Aop(AspectOriented Programming):一种面向横切面编程的思想方式,可以进行功能性扩展,看前边的一篇转载的博客:面向横切面(AOP)编程。
4、容器:可以容纳对象,并且可以控制对象的生命周期;
(因为这些概念,在这次的ssh中没有很深的体会,所以就不细致讲解了)
二、配置过程:
1、引入Sprign jar包:
Spring3.2 开发最基本jar包
spring-beans-3.2.0.RELEASE.jar
spring-context-3.2.0.RELEASE.jar
spring-core-3.2.0.RELEASE.jar
spring-expression-3.2.0.RELEASE.jar
com.springsource.org.apache.commons.logging-1.1.1.jar
com.springsource.org.apache.log4j-1.2.15.jar
AOP开发
spring-aop-3.2.0.RELEASE.jar
spring-aspects-3.2.0.RELEASE.jar
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
Spring Jdbc开发
spring-jdbc-3.2.0.RELEASE.jar
spring-tx-3.2.0.RELEASE.jar
Spring事务管理
spring-tx-3.2.0.RELEASE.jar
Spring整合其他ORM框架
spring-orm-3.2.0.RELEASE.jar
Spring在web中使用
spring-web-3.2.0.RELEASE.jar
Spring整合Junit测试
spring-test-3.2.0.RELEASE.jar
2、编写核心配置文件:
* 引入applicationContext.xml(框架的核心配置文件)到SRC中:
*Web.xml---配置核心监听器:
<!-- 配置Spring的核心监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
log4j.properties(控制log4j的日志输出基本,输出目的地以及输出格式)
3、在Spring对象中声明和初始化对象:
***********************以后台商品管理为例*******************************
<!-- 后台商品管理的Action -->
<beanid="adminProductAction"class="cn.itcast.shop.product.adminaction.AdminProductAction"scope="prototype">
<propertyname="productService" ref="productService"/>
</bean>
<!-- 后台商品管理的Service -->
<beanid="productService"class="cn.itcast.shop.product.service.ProductService">
<propertyname="productDao" ref="productDao"/>
</bean>
<!-- 后台商品管理的Dao -->
<beanid="productDao"class="cn.itcast.shop.product.dao.ProductDao">
<propertyname="sessionFactory" ref="sessionFactory"/>
</bean>
分析:
1、框架创建对象的方式:
框架默认创建对象是单例的:scope="singleton"
当然action我们想要多例创建,需要设置:scope="prototype"
2、工厂BeanFactory,如果使用BeanFactory来加载Spring配置文件,那么是在调用getBean时,框架调用对象的默认构造方法来创建对象。BeanFactory功能是对bean对象的生命周期进行管理的。(创建,初始化,销毁)。
3、ApplicationContext(推荐,也是框架默认的)(百度到的,我也不是很理解)
对象来加载Spring配置文件,会在加载时解析配置文件,创建对象,而不是在getBean时创建。其实,ApplicationContext接口继承了BeanFactory,所以具备BeanFactory所有功能,同时增加扩展的功能,例如加载资源配置文件,国际化支持等!
三、最后
单看spring,并没感觉有多么吸引人,但是结合到其他的框架,它的优势就显示出来了。