technology-note/spring/1.spring基础.md
2018-08-05 21:39:32 +08:00

171 lines
4.7 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

  spring是为了解决企业级应用开发的复杂性而创建的spring最根本的使命是简化Java开发。为降低开发复杂性有以下四种关键策略。
- 基于POJO的轻量级和最小侵入性编程
- 通过依赖注入和面向接口实现松耦合
- 基于切面和惯例进行声明式编程
- 通过切面和模板减少样板式代码
#### 1.依赖注入
  假设类A依赖类B通常做法是在类A中声明类B然后使用这样一方面具有极高的耦合性将类A与类B绑定在一起另一方面也让单元测试变得很困难无法在A外部获得B的执行情况。
  通过依赖注入,对象的依赖管理将不用对象本身来管理,将由一个第三方组件在创建对象时设定,依赖关系将被自动注入到对应的对象中去。
#### 2.创建应用上下文
- `ClassPathXmlApplicationContext()`从类路径创建
- `FileSystemXmlApplicationContext()`读取文件系统下的xml配置
- `XmlWebApplicationContext()` 读取web应用下的XML配置文件并装载上下文定义
#### 3.声明Bean
1. 最简单
`<bean id="bean1" class="com.example.Class"/>`
2. 带构造器
```xml
<bean id="bean1" class="com.example.Class">
<contructor-arg value="12"/> //基本数据类型使用value
<contructor-arg ref="bean2"/> //对象使用ref
</bean>
```
3. 通过工厂方法创建
如果想声明的Bean没有一个公开的构造函数通过factory-method属性来装配工厂生产的Bean
```xml
<bean id="bean1" class="com.example.class" factory-method="getInstance"/>//getInstance为获取实例的静态方法。
```
#### 4.Bean的作用域
所有Spring Bean默认都是单例的。通过配置scope属性为prototype可每次请求产生一个新的实例。
```xml
<bean id="bean3" class="com.example.class" scope="prototype">
```
scope可选值
- `singleton`每个容器中一个Bean对象只有一个实例。**默认**
- `prototype`:允许实例化任意次 ,每次请求都会创建新的
- `request`作用域为一次http请求
- `session`作用域为一个http session会话
- `global-session`作用域为一个全局http session仅在Protlet上下文中有效
#### 5.初始化和销毁Bean
当实例化需要执行初始化操作,或者销毁时需要执行清理工作。两种实现方式:
1. xml配置类中编写初始化方法和销毁方法在bean中定义。
```xml
<bean id="bean4" class="com.example.Class" init-method="start" destroy-method="destroy"/>
```
也可在Beans中定义默认初始化和销毁方法。
```xml
<beans . . . default-init-method="" default-destroy-method=""/>
```
2. 实现`InitializingBean `和`DisposableBean`接口
#### 6.setter注入
在bean中使用`<property>`元素配置属性,使用方法类似于`<constructor-arg>`
```xml
<property name="name" value="fxg"/> //注入基本数据类型
<property name="sex" ref="sex"/> //注入类
```
可使用p简写,**-ref**后缀说明装配的是一个引用
```xml
<bean id="bean5" class="com.example.class"
p:name="fxb"
p:sex-ref="sex"/>
```
#### 7.注入内部Bean
既定义其他Bean内部的Bean避免共享问题可在属性节点或者构造器参数节点上使用。
```xml
<property name="sex">
<bean class="com.example.sex"/> //没有id属性因为不会被其他bean使用
</property>
<constructor-arg>
<bean class="com.example.sex"/>
</constructor-arg>
```
#### 8.装配集合
| 集合元素 | 用途 |
| ---------------- | ------------------------------ |
| \<list\> | 装配list类型允许重复 |
| \<set\> | set不能重复 |
| \<map\> | map类型 |
| \<props\> | properties类型键值都为String |
- list
```xml
<property name="instruments">
<list>
<ref bean="guitar"/>
<ref bean="cymbal"/>
<ref bean="harmonica"/>
</list>
</property>
<ref>用来定义上下文中的其他引用,还可使用<value>,<bean>,<null/>
```
- set
```xml
<set>
<ref bean="fasdf"/>
</set>
```
用法和list相同只是不能重复
- Map
```XML
<map>
<entry key="GUITAR" value-ref="guitar"/>
</map>
```
entry元素由一个key一个value组成分别有两种形式。
| key | 键为String |
| :-------- | -------------- |
| key-ref | 键为Bean的引用 |
| value | 值为String |
| value-ref | 值为Bean的引用 |
- props
```xml
<props>
<prop key="GUITAR">guitar</prop>
</props>
```
键值都是String
#### 9.装配空值
```xml
<property name="name"><null/></property>
```