一.思想简介
1.IOC
反转控制:即直接使用IOC容器提供的对象(将对象控制权交由框架管理)
2.DI
二.使用
1.整体框架
2.获取容器提供的对象
①整体流程
根据xml配置文件找到对应id的类(Class.forName) 本质为反射,编译时可以不确定类类型 将类的实例化对象和id注入到IOC容器中(.newInstance) 本质为调用无参构造器 在调用方法获取对象时对比id或者类型即可
②根据bean的id获取对象
通过对比id获取容器中的对象 缺点:返回值为Object类型 例子
< bean id = " helloworld" class = " com.atguigu.spring.bean.HelloWorld" > </ bean>
public void testHelloWorld(){
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
HelloWorld helloworld = (HelloWorld) ac.getBean("helloworld");
helloworld.sayHello();
}
③根据bean的类型获取对象
通过对比类型(instanceof)来获取对象,返回值类型为对象的类型 优点:可以通过继承的类,实现的接口来寻找对象 缺点:同类型查找,只能有一个匹配的对象 例子:
@Test
public void testHelloWorld(){
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
HelloWorld bean = ac.getBean(HelloWorld.class);
bean.sayHello();
}
④根据id和类型获取对象
@Test
public void testHelloWorld(){
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
HelloWorld bean = ac.getBean("helloworld", HelloWorld.class);
bean.sayHello();
}
3.DI的依赖注入
①set注入
< bean id = " studentOne" class = " com.atguigu.spring.bean.Student" >
< property name = " id" value = " 1001" > </ property>
< property name = " name" value = " 张三" > </ property>
< property name = " age" value = " 23" > </ property>
< property name = " sex" value = " 男" > </ property>
</ bean>
②构造器注入
< bean id = " studentTwo" class = " com.atguigu.spring.bean.Student" >
< constructor-arg value = " 1002" > </ constructor-arg>
< constructor-arg value = " 李四" > </ constructor-arg>
< constructor-arg value = " 33" > </ constructor-arg>
< constructor-arg value = " 女" > </ constructor-arg>
</ bean>
③p命名空间注入
< bean id = " studentSix" class = " com.atguigu.spring.bean.Student"
p: id= " 1006" p: name= " 小明" p: clazz-ref= " clazzOne" p: teacherMapref= " teacherMap" > </ bean>
4.DI依赖注入的类型
①字面量
< property name = " name" >
< null />
</ property>
< property name = " expression" value = " a < b" />
< property name = " expression" >
< value> <![CDATA[a < b]]> </ value>
</ property>
②类类型赋值
< property name = " clazz" ref = " clazzOne" > </ property>
< property name = " clazz" >
< bean id = " clazzInner" class = " com.atguigu.spring.bean.Clazz" >
< property name = " clazzId" value = " 2222" > </ property>
< property name = " clazzName" value = " 远大前程班" > </ property>
</ bean>
</ property>
< bean id = " studentFour" class = " com.atguigu.spring.bean.Student" >
< property name = " id" value = " 1004" > </ property>
< property name = " name" value = " 赵六" > </ property>
< property name = " age" value = " 26" > </ property>
< property name = " sex" value = " 女" > </ property>
< property name = " clazz" ref = " clazzOne" > </ property>
< property name = " clazz.clazzId" value = " 3333" > </ property>
< property name = " clazz.clazzName" value = " 最强王者班" > </ property>
</ bean>
③数组类型赋值
< property name = " hobbies" >
< array>
< value> 抽烟</ value>
< value> 喝酒</ value>
< value> 烫头</ value>
</ array>
</ property>
④集合类型list赋值
< property name = " students" >
< list>
< ref bean = " studentOne" > </ ref>
< ref bean = " studentTwo" > </ ref>
< ref bean = " studentThree" > </ ref>
</ list>
</ property>
< util: list id = " studentlist" >
< ref bean = " studentOne" > </ ref>
< ref bean = " studentTwo" > </ ref>
< ref bean = " studentThree" > </ ref>
</ util>
⑤集合类型map赋值
< property name = " teacherMap" >
< map>
< entry>
< key>
< value> 10010</ value>
</ key>
< ref bean = " teacherOne" > </ ref>
</ entry>
< entry>
< key>
< value> 10086</ value>
</ key>
< ref bean = " teacherTwo" > </ ref>
</ entry>
</ map>
</ property>
< util: map id = " teacherMap" >
< entry>
< key>
< value> 10010</ value>
</ key>
< ref bean = " teacherOne" > </ ref>
</ entry>
< entry>
< key>
< value> 10086</ value>
</ key>
< ref bean = " teacherTwo" > </ ref>
</ entry>
</ util: map>