3.Spring基于XML的IOC环境搭建和入门
- 核心配置文件
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="唯一标识" class="交给Spring创建类的全路径"></bean>
</beans>
- 在类中调用Spring容器中对象。
//1.获取Spring核心容器
ApplicationContext ac = new ClassPathXmlApplicationContext("核心配置文件的全路径");
//2.根据id获取对象
对象类型 对象名 = (强转的对象类型)ac.getBean("id名");
或者
对象类型 对象名 = ac.getBean("id名",类名.class);
4.ApplicationContext的三个实现类
- 三种实现类
三种实现类都可以获取Spring核心容器
ClassPathXmlApplicationContext(推荐使用)
另外两种可以通过IDEA的diagrams->show diagrams 查看ApplicationContext接口的继承,然后右键查看实现类。
5.ApplicationContext和BeanFactory的区别
ApplicationContext是立即获取Spring容器。(推荐使用)
BeanFactory是延时加载,只有使用到Spring容器中的bean才会加载。
6.Spring中bean的细节 之 创建bean对象的三种方式
1.使用默认的构造方法创建(开发中使用最多)
<bean id="id标识" class="类的全路径"></bean>
2.使用普通工厂的方法创建对象(适用于使用别人的jar包)
<bean id="id标识" class="类的全路径"></bean>
<bean id="id标识" factory-bean="工厂类的id标识" factory-method="工厂中的方法"></bean>
3. 使用工厂中的静态方法创建对象
<bean id="id标识" class="工厂类的全路径" factory-method="工厂中的方法">
7.Spring中bean的细节 之 bean的作用范围
<bean id="" class="" scope=""></bean>
scope 取值:
singleton:单例的(默认值)
property:多例的
request:作用与web应用的请求范围
session:作用与web应用的会话范围
global-session:作用域集群环境的会话范围
常用的既是第一种和第二种
8.Spring中bean的细节 之 bean的生命周期
单例的生命周期
多例的生命周期
9.spring的依赖注入
依赖注入
能注入的数据类型 有三类
基本数据类型和String
其他bean类型
复杂类型/集合类型
注入的方式 有三种
使用构造函数提供(一般不用)
使用set方法提供
使用注解提供
我们通过IOC把类的创建交给spring容器管理
可以通过依赖注入把对象注入到使用类中
"依赖注入的目的也是为了解耦,保证程序具有OCP原则,修改数据不用到勒种进行修改,直接修改配置文件就可以了."
10.构造方法注入
构造方法:
public 类名(参数类型 参数名...){
this.变量名 = 参数名;
...
}
核心配置文件配置
<bean id="" class="">
<constructor-arg name="参数名" value="参数值"></constructor-arg>
<constructor-arg name="参数名" ref="bean的id"></constructor-arg>
</bean>
constructor-arg标签属性
type
index
name(常用) 指定构造方法中的参数名
--------------------------------------
value 提供基本类型和String类型的数据
ref 指定其他的bean类型数据
优势.:
获取bean对象的时候,注入数据是必须操作,否则对象无法创建成功.
劣势:
我们创建对象的时候,就算有些数据用不到,也会注入.
11.set方法注入
1. 在需要数据注入的类中,为变量提供set方法
private string name;
public void setName(string name) {
this.name = name;
}
2.核心配置文件配置
涉及标签: property
出现的位置: bean标签内部
标签属性
name:指定注入时set方法的名称
---------------
value:提供基本数据类型和String类型数据
ref:用于指定其他bean类型数据
<bean id="" class="">
<property name="" value=""></property>
<property name="" ref=""></property>
</bean>
优势:
创建对象没有明确限制,可以使用默认构造方法
劣势:
如果某个成员变量必须有值,获取对象的时候set方法可能没有执行.
12.注入集合数据
1.类中提供set方法
private String[] string;
private List<String> list;
private Set<String> set;
private Map<String,String> map;
private Properties properties;
public void setString(String[] string) {
this.string = string;
}
public void setList(List<String> list) {
this.list = list;
}
public void setSet(Set<String> set) {
this.set = set;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
2.核心配置文件配置
<bean id="" class="">
<property name="">
<list>
<value></value>
</list>
</property>
<property name="">
<map>
<entry key="" value="">
</entry>
</map>
</property>
</bean>
"集合当中只需要记住list和map就行了,相同类型结构之间可以互换."