technology-note/java/spring/1.spring基础.md
2019-01-07 10:31:40 +08:00

179 lines
5.1 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.

---
id="2018-10-20-10-38-05"
title="spring基础"
headWord="spring是为了解决企业级应用开发的复杂性而创建的spring最根本的使命是简化Java开发。为降低开发复杂性有以下四种关键策略。 "
tags=["java", "spring"]
category="java"
serie="spring学习"
---
  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>
```