Spring框架笔记

 

## 1.优点

- Spring是一个开源的免费的框架(容器)!
- Spring是一个轻量级的、非入侵式的框架!
- 控制反转(IOC)、面向切面编程(AOP)
- 支持事务的处理,对框架整合的支持!

 

总结一句话:Spring就是一个轻量级的控制反转(IOC)和面向切面编程(AOP)的框架

# 2.IOC本质

**控制反转,是一种设计思想,DI(依赖注入)是实现IoC的一种方法,**也有人认为DI只是IoC的另一种说法,没有IoC的程序中,我们使用面向对象编程,对象的创建与对象间的依赖关系完全硬编码在程序中,对象的创建由程序自己控制,控制反转后将对象的创建转移给第三方,个人认为所谓控制反转就是:获得依赖对象的方式反转了。

 

采用XML方式配置Bean的时候,Bean的定义信息是和实现分离的,而采用注释的方式可以把两者何为一体,Bean的定义信息直接以注释的形式定义在实现类中,从而达到了零配置的目的。

 

**控制反转是一种通过描述(XML或注解)并通过第三方法生产或获取特定对象的方式,在Spring中实现控制反转的是IoC容器,其实现方法是依赖注入。**

 

# 3.IOC创建对象的方式

1. 使用无参构造创建对象,默认

2. 假设我们要使用有参构造创建对象。

1. 下标赋值

```xml
<!--第一种,下标赋值-->
<bean id = "User" class = "org.wdzl.entity.User">
<constructor-arg index= "0" value="你好啊">
</bean>
```

2. 类型

```xml
<!--第二种方式:通过类型创建,不建议使用-->
<bean id="User" class = "org.wdzl.entity.User">
<constructor-arg type= "java.lang.String" value="luhaonan">
</bean>
```

​ 3.参数名

```xml
<!--第三种,直接通过参数名来设置-->
<bean>
<bean id="User" class = "org.wdzl.entity.User">
<constructor-arg name= "name" value="卢浩南">
</bean>
```

 

总结:在配置文件加载的时候,容器中管理的对象就已经初始化了!

# 4.Spring配置说明

## 4.1、别名

```xml
<!--别名,如果添加了别名,我们也可以使用别名获取到这个对象-->
<alias name ="user" alias="userNew">
```

## 4.2、Bean的配置

```xml
<!--
id : bean 的唯一标识符,也就是相当于我们学的对象名
class:bean 对象所对应的权限定名:包名 + 类型
name : 也是别名,而且name可以同时取多个别名
-->
<bean id = "" class="" name=""> </bean>
```

## 4.3、import

这个import,一般用于团队开发使用,它可以将多个配置文件,导入合并为一个

假设,想在项目中有多个人在开发使用,这三个人负责不同的类开发,不同的类需要注册在不容的bean中,我们可以利用import将所有人的beans.xml合并为一个总的!

- 张三
- 李四
- 王五
- applicationContext.xml

```xml
<import resource = "beans.xml">
<import resource = "beans1.xml">
<import resource = "beans2.xml">
```

使用的时候,直接使用总的配置就可以了

# 5、依赖注入

## 5.1、构造器注入

就是前面的方法

## 5.2、Set方式注入(***重点)

- 依赖注入:Set注入!

- 依赖:bean对象的创建依赖于容器

- 注入:bean对象中的所有属性,由容器来注入

【环境搭建】

1. 复杂类型

```java
public class Address {
private String address;

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}
}

```

 

2. 真实测试对象

```java
public class Student {
private String name;
private Address address;
private String[] books;
private List<String> hobbys;
private Map<String,String> card;
private Set<String> games;
private String wife;
private Properties info;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Address getAddress() {
return address;
}

public void setAddress(Address address) {
this.address = address;
}

public String[] getBooks() {
return books;
}

public void setBooks(String[] books) {
this.books = books;
}

public List<String> getHobbys() {
return hobbys;
}

public void setHobbys(List<String> hobbys) {
this.hobbys = hobbys;
}

public Map<String, String> getCard() {
return card;
}

public void setCard(Map<String, String> card) {
this.card = card;
}

public Set<String> getGames() {
return games;
}

public void setGames(Set<String> games) {
this.games = games;
}

public String getWife() {
return wife;
}

public void setWife(String wife) {
this.wife = wife;
}

public Properties getInfo() {
return info;
}

public void setInfo(Properties info) {
this.info = info;
}
}
```

3.beans.xml

```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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id = "student" class="org.wdzl.pojo.Student">
<!--第一种,普通值注入,value-->
<property name="name" value="卢荣琳"/>
</bean>
</beans>
```

4.完善注入信息

```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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id = "address" class="org.wdzl.pojo.Address">
<property name="address" value="西安"/>
</bean>>


<bean id = "student" class="org.wdzl.pojo.Student">
<!--第一种,普通值注入,value-->
<property name="name" value="卢荣琳"/>

<!--第二种,bean注入,ref-->
<property name="address" ref="address"/>

<!--数组注入,ref-->
<property name="books">
<array>
<value>红楼梦</value>
<value>西游记</value>
<value>水浒传</value>
<value>三国演义</value>
</array>
</property>

<!--list-->
<property name="hobbys">
<list>
<value>听歌</value>
<value>敲代码</value>
<value>看电视</value>
</list>
</property>

<!--Map-->
<property name="card" >
<map>
<entry key="身份证" value="3165165656465156"></entry>
<entry key="银行卡" value="36456565498498"></entry>
</map>
</property>

<!--set-->
<property name="games">
<set>
<value>LOL</value>
<value>COC</value>
<value>Dota</value>
</set>
</property>

<!--null-->
<property name="wife">
<null/>
</property>

<!--Properties-->
<property name="info">
<props>
<!--<prop key="学号">03175050</prop>
<prop key="性别">男</prop>
<prop key="姓名">小明</prop>-->
<prop key="driver">03175050</prop>
<prop key="url">男</prop>
<prop key="username">小明</prop>
<prop key="password">123456</prop>

</props>
</property>
</bean>
</beans>
```

 

 

## 6.3、拓展方式注入

我们可以使用p命令和C命令进行注入

## 6.4、bean的作用域

1. 单例模式(Spring默认机制)

```xml
<bean id="user2" class="org.wdzl.pojo.User" c:name="汪涵然" c:age="18" scope="singleton"/>
```

2. 原型模式:每次从容器中get的时候,都会产生一个新对象

```xml
<bean id="user2" class="org.wdzl.pojo.User" c:name="汪涵然" c:age="18" scope="prototype"/>
```

3. 其余的request,session,application,这些只能在web开发中使用到

 

# 7、 bean的自动装配

- 自动装配是spring满足bean依赖的一种方式!
- Spring会在上下文中自动寻找,并自动给bean装配属性

 

在Spring中有三种装配的方式

1. 在xml中显示的配置
2. 在Java中显示配置
3. 隐式的自动装配bean【重要的东西】

## 7.1 测试

1. 环境搭建:一个人有两个宠物

 

## 7.2 ByName自动装配

```xml
<!--
byName:会自动在容器上下文中查找,和自己对象set方法后面的值对应的bean id~
-->
<bean id="people" class="org.wdzl.pojo.People" autowire="byName">
<property name="name" value="神啊"/>
<!-- <property name="cat" ref="cat"/>
<property name="dog" ref="dog"/>-->

</bean>
```

## 7.3 ByType自动装配

```xml
<bean class="org.wdzl.pojo.Cat"/>
<bean class="org.wdzl.pojo.Dog"/>

<!--
byName:会自动在容器上下文中查找,和自己对象set方法后面的值对应的bean id~
byType:会自动在容器上下文中查找,和自己对象属性类型相同的对应的bean
-->
<bean id="people" class="org.wdzl.pojo.People" autowire="byType">
<property name="name" value="神啊"/>
<!-- <property name="cat" ref="cat"/>
<property name="dog" ref="dog"/>-->

</bean>

```

小结:

- byname的时候,需要保证所有的bean唯一,并且这个bean需要和自动注入的属性的set的值一致
- 不要type的时候,需要保证所有的bean的class唯一,并且这个bean需要和自动注入的属性的类型一致

 

 @浮生、L

看狂神视频总结       方便用于复习

 

posted @ 2020-07-20 09:13  Whitezhang  阅读(130)  评论(0)    收藏  举报