Spring简介
Spring 为简化企业级应用开发而生。
使用 Spring 可以使简单的 JavaBean 实现以前只有 EJB 才能实现的功能。
Spring 是一个 IOC(DI) 和 AOP 容器的开源框架。
-
具体描述 Spring:
– 轻量级:Spring 是非侵入性的 - 基于 Spring 开发的应用中的对象可以不依赖于 Spring 的 API
– 依赖注入(DI — dependency injection、IOC)
– 面向切面编程(AOP—aspectorientedprogramming)
– 容器:Spring是一个容器,因为它包含并且管理应用对象的生命周期
– 框架: Spring 实现了使用简单的组件配置组合成一个复杂的应用. 在 Spring 中可以使用 XML 和 Java 注解组合这些对象
– 一站式:在 IOC 和 AOP 的基础上可以整合各种企业应用的开 源框架和优秀的第三方类库 (实际上 Spring 自身也提供了展现 层的 SpringMVC 和 持久层的 Spring JDBC) -
Spring模块:
1. 创建 Spring IOC 容器并获取 Bean
IOC & DI 简介
IOC(Inversion of Control): 其思想是反转资源获取的方向。 传统的资源查找方式要求组件向容器发起请求查找资源,作为回应, 容器适时的返回资源.。而应用了 IOC 之后, 则是容器主动地将资源推送给它所管理的组件, 组件所 要做的仅是选择一种合适的方式来接受资源. 这种行为也被称为查找的被动形式
DI(DependencyInjection)—IOC的另一种表述方式: 即
组件以一些预先定义好的方式(例如: setter 方法)接受来自如容器的资源注入. 相对于 IOC 而言,这种表述更直接
需求:生成 HTML 或 PDF 格式的不同类型的报表。以此为例,展现IOC的前世今生。
- IOC 前生 — 分离接口与实现
- IOC 前生 — 采用工厂设计模式
- IOC — 采用反转控制
在 Spring IOC 容器读取 Bean 配置创建 Bean 实例之前, 必须对 Spring IOC 容器进行实例化. 只有在容器实例化后, 才可以从 IOC 容器里获取 Bean 实例并使用.
IOC 容器的创建,Spring 中提供了两种类型:
– BeanFactory: IOC 容器的基本实现.
– ApplicationContext: 提供了更多的高级特性. 是 BeanFactory 的子接口.
=> BeanFactory是Spring框架的基础设施,面向Spring本身;
=> ApplicationContext 面向使用 Spring 框架的开发者,几乎所有的应用场合都直接使用 ApplicationContext 而非底层的 BeanFactory
=> 无论使用何种方式, 配置文件是相同的.
1. 创建 Spring IOC 容器:ApplicationContext
ApplicationContext 的主要实现类:
– ClassPathXmlApplicationContext: 从 类路径下 加载配置文件
– FileSystemXmlApplicationContext: 从 文件系统中 加载配置文件
=> ConfigurableApplicationContext 扩展于 ApplicationContext,新增加两个主要方 法:refresh() 和 close(), 让 ApplicationContext 具有启动、刷新和关闭上 下文的能力
=> ApplicationContext 在初始化上下文时就实例化所有单例的Bean。
=> WebApplicationContext 是专门为 WEB 应用 而准备的,它允许从相对于 WEB 根目录的路径中完成初始化工作
Spring IOC 容器读取 Bean 配置前提条件:
1. 实例化 Spring IOC 容器
2. 实例化 Bean
// 1.创建 IOC 容器,ApplicationContext 代表 IOC 容器
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
2.从 Spring IOC 容器中获取 Bean 实例
通过调用 ApplicationContext 的 getBean() 方法,从 Spring IOC 容器中获取 Bean。
getBean()是父接口BeanFactory()的方法。
<!-- 利用 id 定位到 IOC 容器中的 bean -->
HelloWorld helloWorld = (HelloWorld)ctx.getBean("helloWorld");
<!-- 利用类型返回 IOC 容器中的 Bean,但要求 IOC 容器中必须只能有一个该类型的Bean -->
HelloWorld helloWorld1 = ctx.getBean(HelloWorld.class);
2. 创建并配置 bean
1. 创建 bean
在resource目录下创建 applicationContext.xml 文件,内容如下
<?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"
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-4.0.xsd">
</beans>
2. 配置 bean:
bean的配置方式:全类名(反射)
<!-- 通过全类名的方式类配置 bean
class: bean 的全类名,通过反射的方式在 IOC 容器中创建Bean。所有要求 Bean 中必须有无参数的构造器。
id: 标识容器中的 bean。id 必须唯一。
-->
<bean id="helloword" class="com.spring.helloword.HelloWord">
</bean>
=> 若 id 没有指定,Spring 自动将权限定性类名作为 Bean 的名字
=> id可以指定多个名字,名字之间可用逗号、分号、或空格分隔
依赖注入(DI)的方式
Spring 支持 3 种依赖注入的方式
– 属性注入
– 构造器注入
– 工厂方法注入(很少使用,不推荐)
属性注入
属性注入即通过setter方法注入Bean的属性值或依赖的对象。
属性注入是实际应用中最常用的注入方式。
属性注入:
使用<property>元素
使用name属性指定Bean 的属性名称
value 属性或 <value> 子节点指定属性值
<bean id="car" class="com.spring.helloword.Car">
<property name="name" value="奥迪"></property>
</bean>
构造方法注入
通过构造方法注入Bean的属性值或依赖的对象,它保证 了 Bean 实例在实例化后就可以使用。
构造器注入:
在 <constructor-arg> 元素里声明属性,
<constructor-arg> 中没有 name 属性
使用构造器注入属性值可以指定参数的位置和参数的类型!以区分重载的构造器!
- 按索引匹配入参:
<bean id="car1" class="com.spring.helloword.Car">
<constructor-arg value="奥迪" index="0"></constructor-arg>
<constructor-arg value="长春一汽" index="1"></constructor-arg>
<constructor-arg value="500000" index="2"></constructor-arg>
</bean>
- 按类型匹配入参:
<bean id="car2" class="com.spring.helloword.Car">
<constructor-arg value="奥迪" type="java.Lang.String"></constructor-arg>
<constructor-arg value="长春一汽" type="java.Lang.String"></constructor-arg>
<constructor-arg value="500000" type=