-
Spring, 春天. Spring框架在开发中的角色时用于管理其他的框架和对象. 换句话说, Spring可以使已有的技术更好用.
-
Spring之父. Rod Johnson.
-
轮子理论, 不要重复发明轮子.
-
关于版本. 在spring3之前, spring只提供一个jar, spring.jar; 从spring3之后, 官方对spring做了拆分, 分成了多个jar包(spring-aop, spring-beans, spring-context, spring-expression, spring-core, spring-jdbc, spring-tx, spring-web, spring-webmvc, …).
-
spring各版本下载路径:
https://repo.spring.io/webapp/#/artifacts/browse/tree/General/libs-release-local/org/springframework/spring -
我们学习的版本是4.3.18.RELEASES.
-
学习Spring的主要内容:
a) IoC / DI: 控制反正 / 依赖注入
b) AOP: 面向切面编程
c) 声明式事务
第三节 介绍Spring Framework Runtime -
Test
对应spring-test.jar. Spring提供的测试工具, 可以整合JUnit测试, 简化测试环节. -
Core Container
Spring的核心组件, 包含了Spring框架最基本的支撑.
a) Beans, 对应spring-beans.jar. Spring进行对象管理时依赖的jar包.
b) Core, 对应spring-core.jar, Spring核心jar包.
c) Context, 对应spring-context.jar, Spring容器上下文对象.
d) SpEL, 对应spring-expression.jar, Spring表达式语言. -
AOP
面向切面编程, 对应spring-aop.jar. -
Data Access
Spring对数据访问层的封装
a) JDBC, 对应spring-jdbc.jar. Spring对jdbc的封装, 当需要使用spring连接数据库时使用. spring-jdbc.jar需要依赖spring-tx.jar.
b) Transactions, 对应spring-tx.jar. 事务管理
c) ORM, 对应spring-orm.jar. spring整合第三方orm框架需要使用的jar包, 例如Hibernate框架. -
Web
Spring对javax下的接口或类做的扩展功能.
a) spring-web.jar, 对Servlet, filter, Listener等做的增强.
b) spring-webmvc.jar, 实际上就是SpringMVC框架. 需要依赖spring环境和spring-web.jar.
第四节 搭建Spring基础环境 -
创建项目并导包
创建java工程, 导入jar包
a) spring核心jar包: 4个
b) 日志包: commons-logging.jar, 被spring-core.jar所依赖. -
提供配置文件
配置文件要求是一个xml文件. 命名没有要求, 位置没有要求. 一般这个文件会放在src下, 命名为applicationContext.xml.
<!--
xmlns: xml namespace, 当前配置文件默认使用哪个命名空间
xmlns:xsi xml schema instance, schema约束规范的提供者
xsi:schemaLocation schema约束文件的地址, 值的表示方式为: key:value
key是一个标识
value是xsd文件的url地址
-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--Spring管理User对象-->
<bean class="com.bjsxt.pojo.User" id="user" />
</beans>
- 编写测试代码
public class TestSpring {
public static void main(String[] args) {
// Ctrl + h: Hierarchy面板, 层级展示当前类(接口)的结构
// Ctrl + Alt + b: 查看当前接口下的所有子接口和实现类
// 加载Spring配置文件, 得到Spring容器对象
ApplicationContext context =
new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
// 从容器中获取需要的对象
User user = context.getBean("user", User.class);
System.out.println(user);
User user2 = context.getBean("user", User.class);
System.out.println(user2);
System.out.println(user == user2); // true
}
}
- 总结
a) 默认情况下, 容器加载时, 对象就会被创建;
b) 默认情况下, 对象在spring容器中是单例的.
第五节 IoC/DI详解 - IoC概述
a) IoC, Inversion of Control, 控制反转
b) Control, 对对象的管理.
c) Inversion, 反转. 在没有使用spring之前, 由程序员主导对象的管理; 将对象管理的操作反过来交给Spring的过程就被称之为控制反转.
d) 由Spring去管理对象(创建对象)的方案. - Spring创建对象的几种方式
a) 通过构造器创建对象
无参构造器. 默认Spring会使用无参构造器创建对象
有参构造器. 可以在下通过标签指定使用有参构造器创建对象. 属性介绍:
a)index: 索引, 从0开始, 表示参数的索引位置
b)name: 名称, 表示参数的名称
c)type: 类型, 表示参数的类型
d)value: 赋值. 当值是简单类型时可以使用. 基本类型, 包装类型, String, resource, class
e)ref: (reference)赋值. 当值为非简单类型时使用, 表示需要引用一个
<bean>.
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--Spring管理User对象-->
<bean class="com.bjsxt.pojo.User" id="user">
<constructor-arg name="id" index="0" type="java.lang.Integer" value="123" />
<constructor-arg name="name" index="1" type="java.lang.String" value="张无忌" />
</bean>
</beans>
b) 通过工厂创建对象
实例工厂. 需要先创建工厂对象, 然后调用指定的方法创建目标对象
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--创建工厂对象-->
<bean id="factory" class="com.bjsxt.factory.UserFactory1" />
<!--通过实例工厂创建目标对象-->
<bean id="user" factory-bean="factory" factory-method="getInstance" />
</beans>
静态工厂. 直接通过工厂类调用静态方法就可以创建目标对象
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--通过静态工厂创建目标对象-->
<bean id="user" class="com.bjsxt.factory.UserFactory2" factory-method="getInstance" />
</beans>
- DI概述
a) DI, Dependency Injection. 依赖注入.
b) Dependency, 依赖. 两个对象A和B, A对象有一个属性是B类型的, 我们就可以说A依赖B.
c) Injection, 注入. 为对象的属性赋值的过程.
d) DI可以被认为是IoC的一部分. - 依赖注入的几种方式
a) 构造器注入, 参照IoC.
b) 工厂注入, 参照IoC.
c) 设值注入, 类似于先创建对象, 然后调用setter方法为属性赋值.
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean class="java.util.Date" id="birthday" />
<bean class="com.bjsxt.pojo.Address" id="addr">
<property name="city" value="广州" />
</bean>
<bean class="com.bjsxt.pojo.Card" id="card">
<property name="num" value="187236187236" />
<property name="balance" value="1000" />
</bean>
<bean id="user" class="com.bjsxt.pojo.User">
<!--Properties类型-->
<property name="info">
<props>
<prop key="driver">com.mysql.jdbc.Driver</prop>
<prop key="username">root</prop>
</props>
</property>
<!--Map集合-->
<property name="cardMap">
<map>
<entry key="建行">
<bean class="com.bjsxt.pojo.Card">
<property name="num" value="18273618273" />
<property name="balance" value="200" />
</bean>
</entry>
<entry>
<key>
<value>农行</value>
</key>
<ref bean="card" />
</entry>
</map>
</property>
<!--Set集合-->
<property name="cards">
<set>
<bean class="com.bjsxt.pojo.Card">
<property name="num" value="762318923781" />
<property name="balance">
<value>1</value>
</property>
</bean>
<ref bean="card" />
</set>
</property>
<!--List集合-->
<property name="addrs">
<list>
<bean class="com.bjsxt.pojo.Address">
<property name="city" value="上海" />
</bean>
<ref bean="addr" />
</list>
</property>
<!--自定义对象类型-->
<property name="address">
<bean class="com.bjsxt.pojo.Address">
<property name="city" value="北京" />
</bean>
</property>
<!--数组类型-->
<property name="hob">
<array>
<value>吃饭</value>
<value>睡觉</value>
<value>打豆豆</value>
</array>
</property>
<!--Date-->
<!--<property name="birthday" ref="birthday" />-->
<!--<property name="birthday">
<ref bean="birthday" />
</property>-->
<property name="birthday">
<bean class="java.util.Date" />
</property>
<!--String-->
<property name="name" value="张三丰" />
<!--包装类型: Integer-->
<property name="age">
<value>20</value>
</property>
<!--基本数据类型: int-->
<property name="id" value="110" />
</bean>
</beans>