1、IOC概述
IOC(Inversion of Control),即“控制反转”,不是什么技术,而是一种设计思想。
IOC是一种编程思想 , 由主动的编程变成被动的接收。
到了现在,我们彻底不用再程序中去改动了,要实现不同的操作,只需要在xml配置文件中进行修改,所谓的IOC,一句话搞定:对象由Spring来创建、管理、装配 !
2、IOC创建对象的方式
(1)使用无参构造创建对象,默认!
(2)使用有参构造
在有参构造中,有三种方式:
<bean id="user" class="com.erdan.pojo.User">
<!--第一种,直接根据参数名字来赋值-->
<constructor-arg name="name" value="二蛋1"/>
<!--第二种,根据索引来赋值-->
<constructor-arg index="0" value="二蛋2"/>
<!--第三种,根据类型来赋值-->
<constructor-arg type="java.lang.String" value="二蛋3"/>
</bean>
总结:在配置文件加载的时候,容器中管理的对象就已经初始化了!
3、Spring配置
(1)别名
<alias name="user" alias="user2222"/>
(2)Bean的配置
id : bean 的唯一标识符,也就是对象名,在test中调用的就是这个名字;
class : bean 对象所对应的全限定名 : 包名 + 类型(“com.erdan.pojo.User”)
**name :**也是别名,而且name 可以同时取多个别名,通过user2和user3都可以访问出“二蛋”。
<bean id="user" class="com.erdan.pojo.User" name="user2 user3">
<property name="name" value="二蛋"/>
</bean>
(3)import导入
可以在user.xml中写出bean配置,将user.xml导包"appLicationContext.xml"
<import resource="user.xml"/>
测试中就可以直接写"appLicationContext.xml",就直接可以使用总的配置,就省去在改一次的麻烦。
@Test
public void testUserSpring(){
ApplicationContext context = new ClassPathXmlApplicationContext("appLicationContext.xml");
User user = (User) context.getBean("user");
user.show();
}
4、Set方式注入
- 依赖注入:Set注入!
- 依赖:bean对象的创建依赖于容器!
- 注入: bean对象中的所有属性,由容器来注入!
(1)复杂类型
package com.erdan.pojo;
public class Address {
private String address;
public void setAddress(String address) {
this.address = address;
}
public String getAddress() {
return address;
}
}
(2)真实测试对象
package com.erdan.pojo;
import java.util.*;
public class Student {
private String name;
private Address address;
private String[] books;
private List<String> list;
private Map<String,String> map;
private Set<String> set;
private String wife;
private Properties info;
public void setName(String name) {
this.name = name;
}
public void setAddress(Address address) {
this.address = address;
}
public void setBooks(String[] books) {
this.books = books;
}
public void setList(List<String> list) {
this.list = list;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
public void setSet(Set<String> set) {
this.set = set;
}
public void setWife(String wife) {
this.wife = wife;
}
public void setInfo(Properties info) {
this.info = info;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", address=" + address.getAddress()+
", books=" + Arrays.toString(books) +
", list=" + list +
", map=" + map +
", set=" + set +
", wife='" + wife + '\'' +
", info=" + info +
'}';
}
}
(3)Student.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:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd">
<bean id="addr" class="com.erdan.pojo.Address">
<property name="address" value="蒲城"/>
</bean>
<bean id="student" class="com.erdan.pojo.Student" scope="singleton">
<!--名字-->
<property name="name" value="二蛋"/>
<!--地址-->
<property name="address" ref="addr"/>
<!--书-->
<property name="books">
<array>
<value>西游记</value>
<value>红楼梦</value>
<value>水浒传</value>
<value>三国演义</value>
</array>
</property>
<!--List-->
<property name="list">
<list>
<value>list1</value>
<value>list2</value>
<value>list3</value>
<value>list4</value>
</list>
</property>
<!--Map-->
<property name="map">
<map>
<entry key="key1" value="v1"/>
<entry key="key2" value="v2"/>
</map>
</property>
<!--Set-->
<property name="set">
<set>
<value>set1</value>
<value>set2</value>
<value>set3</value>
<value>set4</value>
</set>
</property>
<!--null 注入-->
<property name="wife">
<null/>
</property>
<!--Properties 配置类-->
<property name="info">
<props>
<prop key="id">100</prop>
<prop key="name">panghu</prop>
<prop key="">boy</prop>
</props>
</property>
</bean>
</beans>
(4)测试类
@Test
public void Student(){
ApplicationContext context = new ClassPathXmlApplicationContext("appLicationContext.xml");
Student student = (Student) context.getBean("student");
System.out.println(student.toString());
}