Ioc
概述:
Ioc:控制反转,就是将对象的管理托管给spring,包括对象的创建,依赖注入。目的就是解耦,降低程序的耦合度,便于维护。
Ioc采用Xml/Annotation + 工厂模式 +反射
1. 基于xml的Ioc
-
bean的创建:
在xml文件中使用<bean id="" class=""/>标签配置类 id是创建对象的标识,class是类的全路径
-
bean的依赖注入:set注入和有参构造注入
* set注入:
<bean id="" class="" > <property name="" value=""/>//name 类中属性名,value需要注入的属性值,引用类型可以使用ref属性 </bean>
* 有参构造注入:
<bean id="" class="" >
<constructor-arg name="" value=""/>
//使用属性名赋值
<constructor-arg index="" value=""/>
//使用下标赋值
</bean>
```
多种类型数据注入:
List,Map,Array,Set
List
<property name="">
<list>
<value><alue>
<value><alue>
<value><alue>
<st>
</property>
Map
<property name="map">
<map>
<entry key="" value=""/>
<entry key="" value=""/>
<entry key="" value=""/>
</map>
</property>
Set
<property name="set">
<set>
<value><alue>
<value><alue>
<value><alue>
<t>
</property>
共同数据抽取:在bean标签内使用依赖注入对如重复的数据需要写多次,而对于固定的重复数据可以单独抽取出来
1.添加命名空间
```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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd">
自动装配,就是对于复杂数据,对象引用,抽取的list,set等,可以按类型或名字查找,然后自动注入
<bean id="abc" class="com.pojo.ABC" autowire="byType"/>
//会在上下文中寻找和该bean的属性类型相同的数据自动注入,当然类型要唯一,否则会报错
<bean id="abc" class="com.pojo.ABC" autowire="byName"/>
//在上下文中寻找和属性名相同的数据自动注入
2.使用标签抽取公共数据
<util:list id=""><!--map,list,set-->
<value><alue>
<value><alue>
<value><alue>
</util:list>
bean的生命周期
1.创建—>2.依赖注入—>3.初始化—>4.获取对象—>5.销毁
- 把bean交给容器,spring创建对象
<bean id="" class="" init-method="initMethid" destroy-method="destory"/>
ApplicationContext context;
context = new ClassPathXmlApplicationContext("***.xml");
- 值注入
<bean id="" class="">
<property name="" value=""/>
</bean>
- 初始化
//bean标签中有属性init-method
<bean id="" class="" init-method="intMethod">
//值注入后会调用此类中的初始化方法initMethod()
</bean>
- 从容器中获取对象
Object o = context.getBean("***",Object.class);
- 销毁
<bean id="" class="" destroy-method="destory"/>
销毁需要手动调用方法
(ClassPathXmlApplicationContext)context.close();
才会执行bean标签的destroy方法
注:在初始化时还有后置处理器,需要实现接口BeanPostProcessor,
重写其中的方法
注:在 使用xml时,配置的后置处理器会对这个xml文件中的所有配置的bean起作用
//在值注入后spring会把对象传给PostProcessor,处理传入的对象后执行postProcessBeforeInitialization方法,结束需要返回该bean,进行初始化,初始化后执行postProcessAfterInitialization方法,然后返回bean,经过此过程才是第四步获取bean
public class PostProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("后置处理器before");
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("后置处理器after");
return bean;
}
}
导入属性文件
db.driver=com.mysql.jdbbc.Driver
db.url=jdbc:mysql//localhost:3306/db1
db.username=root
db.password=root
配置数据的复用
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
//先加入命名空间context
//使用标签
<context:property-placeholder location="classpath:dataSource/dataSource.properties"/>
//数据模板语法就是${},就可以取出数据
<bean id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="url" value="${db.url}"/>
<property name="driver" value="${db.driver}"/>
<property name="username" value="${db.username}"/>
<property name="password" value="${db.password}"/>
</bean>