Spring 是一个 IOC(DI) 和 AOP 容器框架.
IOC(Inversion of Control):控制反转是将组件间的依赖关系从程序内部提到外部来管理;
DI(Dependency Injection):依赖注入是指将组件的依赖通过外部以参数或其他形式注入
Spring 模块
核心容器(管理Spring应用中bean的创建、 配置和管理。)
spring-core和spring-beans模块提供了框架的基础功能,包括IOC和依赖注入功能。 BeanFactory是一个成熟的工厂模式的实现,将依赖关系的配置和描述从程序逻辑中解耦。
上下文模块(在Core和Beans模块基础上):它提供一个框架式的对象访问方式,类似于一个JNDI注册表。上下文模块从Beans模块继承其功能,并添加支持国际化(使用,例如,资源集合),事件传播,资源负载,并且透明创建上下文,例如,Servlet容器。Context模块还支持Java EE的功能,如EJB,JMX和基本的远程处理。ApplicationContext接口是Context模块的焦点。
spring-context-support支持整合普通第三方库到Spring应用程序上下文,特别是用于高速缓存(ehcache,JCache)和调度(CommonJ,Quartz)的支持。
spring-expression模块提供了强大的表达式语言去支持查询和操作运行时对象图。该语言支持设置和获取属性值,属性分配,方法调用,访问数组,集合和索引器的内容,逻辑和算术运算,变量命名以及从Spring的IoC容器中以名称检索对象。 它还支持列表投影和选择以及常见的列表聚合。
AOP
对面向切面编程提供了丰富的支持,是Spring应用系统中开发切面的基础。
Aspects
提供了与AspectJ的集成。
Instrumentation
Spring的Instrumentation模块提供了为JVM添加代理(agent)的功能。
Messaging
消息传递模块
数据访问/集成
spring-jdbc模块提供了一个JDBC抽象层,消除了需要的繁琐的JDBC编码和数据库厂商特有的错误代码解析。
spring-tx模块支持用于实现特殊接口和所有POJO(普通Java对象)的类的编程和声明式事务管理。
spring-orm模块为流行的对象关系映射(object-relational mapping )API提供集成层,包括JPA和Hibernate。使用spring-orm模块,您可以将这些O / R映射框架与Spring提供的所有其他功能结合使用,例如前面提到的简单声明性事务管理功能。
spring-oxm模块提供了一个支持对象/ XML映射实现的抽象层,如JAXB,Castor,JiBX和XStream。
spring-jms模块(Java Messaging Service) 包含用于生产和消费消息的功能。自Spring Framework 4.1以来,它提供了与 spring-messaging模块的集成。
Web
spring-web模块提供基本的面向Web的集成功能
spring-webmvc模块(也称为Web-Servlet模块)包含用于Web应用程序的Spring 的模型-视图-控制器(MVC)和REST Web Services实现。 Spring的MVC框架提供了 领域模型代码和Web表单之间的清晰分离,并与Spring Framework的所有其他功能 集成。
SPRING TOOL SUITE 是一个 Eclipse 插件,利用该插件可以更方便的在 Eclipse 平台上开发基于 Spring 的应用。
Spring HelloWord
HelloWord Bean类:
public class HelloWorld {
private String user;
public HelloWorld() {
System.out.println("HelloWorld's constructor...");
}
public void setUser(String user) {
System.out.println("setUser:" + user);
this.user = user;
}
public HelloWorld(String user) {
this.user = user;
}
public void hello(){
System.out.println("Hello: " + user);
}
}
HelloWord Bean配置beans.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:util="http://www.springframework.org/schema/util"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- 配置一个 bean -->
<bean id="helloWorld" class="com.spring.helloworld.HelloWorld">
<!-- 为属性赋值 -->
<property name="user" value="Jerry"></property>
</bean>
</beans>
HelloWord Bean获取:
public static void main(String[] args) {
//1. 创建 Spring 的 IOC 容器
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
//2. 从 IOC 容器中获取 bean 的实例
HelloWorld helloWorld = (HelloWorld) ctx.getBean("helloWorld3");
//根据类型来获取 bean 的实例: 要求在 IOC 容器中只有一个与之类型匹配的 bean, 若有多个则会抛出异常.
//一般情况下, 该方法可用, 因为一般情况下, 在一个 IOC 容器中一个类型对应的 bean 也只有一个.
// HelloWorld helloWorld1 = ctx.getBean(HelloWorld.class);
//3. 使用 bean
helloWorld.hello();
}
Spring Bean配置说明:
1 id唯一
2 若 id 没有指定,Spring 自动将权限定性类名作为 Bean 的名字(例如上面的HelloWorld若未指定则,id为com.spring.helloworld.HelloWorld)
3 id可以指定多个名字,名字之间可用逗号、分号、或空格分隔
ApplicationContext
ApplicationContext 在初始化上下文时就实例化所有单例的 Bean。
ClassPathXmlApplicationContext:从 类路径下加载配置文件
FileSystemXmlApplicationContext:从文件系统中加载配置文件
WebApplicationContext 是专门为 WEB 应用而准备的,它允许从相对于 WEB 根目录的路径中完成初始化工作
SpringBean创建方式
普通bean
<bean id="helloword" class="com.spring.HelloWorld"></bean>
静态工厂方法
<!-- 通过工厂方法的方式来配置 bean -->
<!-- 1. 通过静态工厂方法: 一个类中有一个静态方法, 可以返回一个类的实例 -->
<!-- 在 class 中指定静态工厂方法的全类名, 在 factory-method 中指定静态工厂方法的方法名 -->
<bean id="dateFormat" class="java.text.DateFormat" factory-method="getDateInstance">
<!-- 可以通过 constructor-arg 子节点为静态工厂方法指定参数 -->
<constructor-arg value="2"></constructor-arg>
</bean>
实例工厂方法
<!-- 2. 实例工厂方法: 先需要创建工厂对象, 再调用工厂的非静态方法返回实例(了解) -->
<!-- ①. 创建工厂对应的 bean -->
<bean id="simpleDateFormat" class="java.text.SimpleDateFormat">
<constructor-arg value="yyyy-MM-dd hh:mm:ss"></constructor-arg>
</bean>
<!-- ②. 有实例工厂方法来创建 bean 实例 -->
<!-- factory-bean 指向工厂 bean, factory-method 指定工厂方法-->
<bean id="datetime" factory-bean="simpleDateFormat" factory-method="parse">
<!-- 通过 constructor-arg 执行调用工厂方法需要传入的参数 -->
<constructor-arg value="1990-12-12 12:12:12"></constructor-arg>
</bean>
Spring 中有两种类型的 Bean, 一种是普通Bean, 另一种是工厂Bean, 即FactoryBean.
工厂 Bean 跟普通Bean不同, 其返回的对象不是指定类的一个实例, 其返回的是该工厂 Bean 的 getObject 方法所返回的对象
public interface FactoryBean<T> {
//返回实例
T getObject() throws Exception;
//返回类型
Class<?> getObjectType();
//实例是否单例
boolean isSingleton();
}
依赖注入的方式
属性注入
属性注入即通过 setter 方法注入Bean 的属性值或依赖的对象。
属性注入使用 <property> 元素, 使用 name 属性指定 Bean 的属性名称,value 属性或 <value> 子节点指定属性值。
<!-- 配置一个 bean -->
<bean id="helloWorld" class="com.spring.helloworld.HelloWorld">
<!-- 为属性赋值 -->
<property name="user" value="Jerry"></property>
</bean>
构造方法注入
<!-- 通过构造器注入属性值 -->
<bean id="helloWorld" class="com.spring.helloworld.HelloWorld">
<!-- 要求: 在 Bean 中必须有对应的构造器. -->
<constructor-arg value="Mike"></constructor-arg>
</bean>
<!-- 若一个 bean 有多个构造器, 可以根据 index 和 value 进行更加精确的定位. -->
<bean id="car" class="com.spring.helloworld.Car">
<constructor-arg value="KUGA" index="1"></constructor-arg>
<constructor-arg value="ChangAnFord" index="0"></constructor-arg>
<constructor-arg value="250000" type="float"></constructor-arg>
</bean>
<bean id="car2" class="com.spring.helloworld.Car">
<constructor-arg value="ChangAnMazda"></constructor-arg>
<!-- 若字面值中包含特殊字符, 则可以使用 DCDATA 来进行赋值. -->
<constructor-arg>
<value><![CDATA[<ATARZA>]]></value>
</constructor-arg>
<constructor-arg value="180" type="int"></constructor-arg>
</bean>
SpringBean XML属性值类型
字面值
可用字符串表示的值,可以通过 <value> 元素标签或 value 属性进行注入。
基本数据类型及其封装类、String 等类型都可以采取字面值注入的方式
若字面值中包含特殊字符,可以使用 <![CDATA[]]> 把字面值包裹起来。
Bean引用
通过 <ref> 元素或 ref 属性为 Bean 的属性或构造器参数指定对 Bean 的引用.
外部Bean
<bean id="service" class="com.spring.ref.Service">
<!-- 通过 ref 属性值指定当前属性指向哪一个 bean! -->
<property name="dao" ref="dao5"></property>
</bean>
内部bean
<!-- 声明使用内部 bean -->
<bean id="service2" class="com.spring.ref.Service">
<property name="dao">
<!-- 内部 bean, 类似于匿名内部类对象. 不能被外部的 bean 来引用, 也没有必要设置 id 属性 -->
<bean class="com.spring.ref.Dao">
<property name="dataSource" value="c3p0"></property>
</bean>
</property>
</bean>
p 命名
<bean id="user3" class="com.spring.helloworld.User" p:cars-ref="cars" p:userName="Titannic"></bean>
级联属性
<bean id="action" class="com.spring.ref.Action">
<property name="service" ref="service2"></property>
<!-- 设置级联属性(了解) -->
<property name="service.dao.dataSource" value="DBCP2"></property>
</bean>
null 值
<null/> 元素标签为 Bean 的字符串或其它对象类型的属性注入 null 值
集合属性
List或数组
<!-- 装配集合属性 -->
<bean id="user" class="com.spring.helloworld.User">
<property name="userName" value="Jack"></property>
<property name="cars">
<!-- 使用 list 元素来装配集合属性 -->
<list>
<ref bean="car"/>
<ref bean="car2"/>
</list>
</property>
</bean>
List Bean(定义集合)
<!-- 声明集合类型的 bean -->
<util:list id="cars">
<ref bean="car"/>
<ref bean="car2"/>
</util:list>
<bean id="user2" class="com.spring.helloworld.User">
<property name="userName" value="Rose"></property>
<!-- 引用外部声明的 list -->
<property name="cars" ref="cars"></property>
</bean>
Map
<bean class="com.spring.Grade" id="grade">
<property name="name" value="一班"></property>
<property name="students">
<map>
<entry key="stu1" value-ref="student1" />
<entry key="stu2" value-ref="student2" />
<entry key="stu3" value-ref="student3" />
</map>
</property>
</bean>
Set
<property name="empsets">
<set>
<ref bean="emp1" />
<ref bean="emp2"/>
<ref bean="emp2"/>
<ref bean="emp2"/>
<ref bean="emp2"/>
</set>
</property>
props
<property name="pp">
<props>
<prop key="pp1">abcd</prop>
<prop key="pp2">hello</prop>
</props>
</property>
XML 配置里的Bean装配方式
1、 No:即不启用自动装配。Autowire默认的值。
2、 byName:通过属性的名字的方式查找JavaBean依赖的对象并为其注入。
3、 byType:通过属性的类型查找JavaBean依赖的对象并为其注入。
4、 constructor:通byType一样,也是通过类型查找依赖对象。与byType的区别在于它不是使用Seter方法注入,而是使用构造子注入。
5、 default:由上级标签<beans>的default-autowire属性确定。
注意:在配置bean时,<bean>标签中Autowire属性的优先级比其上级标签高,即是说,如果在上级标签中定义default-autowire属性为byName,而在<bean>中定义为byType时,Spring IoC容器会优先使用<bean>标签的配置。
继承 Bean 配置(parent属性)
1、Spring 允许继承 bean 的配置, 被继承的 bean 称为父 bean. 继承这个父 Bean 的 Bean 称为子 Bean。
2、子 Bean 从父 Bean 中继承配置, 包括 Bean 的属性配置。
3、子 Bean 也可以覆盖从父 Bean 继承过来的配置。
4、若只想把父 Bean 作为模板, 可以设置父<bean> 的abstract 属性为 true, 这样 Spring 将不会实例化这个 Bean。
5、可以忽略父 Bean 的 class 属性, 让子 Bean 指定自己的类, 而共享相同的属性配置. 但此时 abstract 必须设为 true。
依赖 Bean 配置
depends-on 属性设定 Bean 前置依赖的Bean,前置依赖的 Bean 会在本 Bean 实例化之前创建好
如果前置依赖于多个 Bean,则可以通过逗号,空格或的方式配置 Bean 的名称
Bean 的作用域
通过scope属性进行设置,singleton是bean的默认作用域。