配置文件
通过配置bean标签来完成对象的管理
● id:对象名
● class:对象的模块类。(所有交给IoC容器来管理的类必须有无参构造函数,因为Spring底层是通过反射机制来创建对象,调用的是无参构造)
对象的成员变量通过property标签完成赋值
● name:成员变量名
● value:成员变量值(基本数据类型,Spring可以直接赋值,如果是其他引用类型,不能通过value赋值)
● ref:将IOC中的另外一个bena赋给当前的成员变量(DI)

<bean id="student1" class="com.yin.Student">
     <property name="id" value="1"></property>
     <property name="age" value="27"></property>
     <property name="name" value="张三"></property>
     <property name="address" ref="address"></property>
</bean>
<bean id="address" class="com.yin.Address">
    <property name="id" value="1"></property>
    <property name="name" value="科技路"></property>
</bean>

IOC底层原理
● 读取配置文件,解析XML
● 通过反射机制实例化配置文件中的所配置所有的bean

package com.southwind.ioc;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class ClassPathXmlApplicationContext implements ApplicationContext {
private Map<String,Object> ioc = new HashMap<String, Object>();
public ClassPathXmlApplicationContext(String path){
try {
SAXReader reader = new SAXReader();
Document document = reader.read("./src/main/resources/"+path);
Element root = document.getRootElement();
Iterator<Element> iterator = root.elementIterator();
while(iterator.hasNext()){
Element element = iterator.next();
String id = element.attributeValue("id");
String className = element.attributeValue("class");
//通过反射机制创建对象
Class clazz = Class.forName(className);
//获取⽆参构造函数,创建⽬标对象
Constructor constructor = clazz.getConstructor();
Object object = constructor.newInstance();
//给⽬标对象赋值
Iterator<Element> beanIter = element.elementIterator();
while(beanIter.hasNext()){
Element property = beanIter.next();
String name = property.attributeValue("name");
String valueStr = property.attributeValue("value");
String ref = property.attributeValue("ref");
if(ref == null){
String methodName =
"set"+name.substring(0,1).toUpperCase()+name.substring(1);
Field field = clazz.getDeclaredField(name);
Method method =
clazz.getDeclaredMethod(methodName,field.getType());
//根据成员变量的数据类型将 value 进⾏转换
Object value = null;
if(field.getType().getName() == "long"){
value = Long.parseLong(valueStr);
}
if(field.getType().getName() == "java.lang.String"){
value = valueStr;
}
if(field.getType().getName() == "int"){
value = Integer.parseInt(valueStr);
}
method.invoke(object,value);
}
ioc.put(id,object);
}
}
} catch (DocumentException e) {
e.printStackTrace();
} catch (ClassNotFoundException e){
e.printStackTrace();
} catch (NoSuchMethodException e){
e.printStackTrace();
} catch (InstantiationException e){
e.printStackTrace();
} catch (IllegalAccessException e){
e.printStackTrace();
} catch (InvocationTargetException e){
e.printStackTrace();
} catch (NoSuchFieldException e){
e.printStackTrace();
}
}
public Object getBean(String id) {
return ioc.get(id);
}
}

通过运⾏时类获取 bean

ApplicationContext applicationContext = new
ClassPathXmlApplicationContext("spring.xml");
Student student = (Student) applicationContext.getBean(Student.class);
System.out.println(student);

这种⽅式存在⼀个问题,配置⽂件中⼀个数据类型的对象只能有⼀个实例,否则会抛出异常,因为没有
唯⼀的 bean。
通过有参构造创建 bean
● 在实体类中创建对应的有参构造函数。
● 配置⽂件

<bean id="student2" class="com.yin.Student">
    <constructor-arg name="id" value="2"></constructor-arg>
    <constructor-arg name="name" value="xiaoming"></constructor-arg>
    <constructor-arg name="age" value="26"></constructor-arg>
    <constructor-arg name="address" ref="add" ></constructor-arg>
</bean>
<bean id="add" class="com.yin.Address">
    <property name="id" value="1"></property>
    <property name="name" value="科技路"></property>
</bean>

给 bean 注⼊集合

<bean id="student1" class="com.yin.Student">
<property name="id" value="1"></property>
<property name="age" value="27"></property>
<property name="name" value="张三"></property>
<property name="addresses" >
    <list>
        <ref bean="address"></ref>
        <ref bean="address2"></ref>
    </list>
</property>
</bean>
<bean id="address" class="com.yin.Address">
    <property name="id" value="1"></property>
    <property name="name" value="科技路"></property>
</bean>
<bean id="address2" class="com.yin.Address">
    <property name="id" value="1"></property>
    <property name="name" value="高新区"></property>
</bean>
最后修改:2021 年 05 月 10 日 09 : 10 PM
如果觉得我的文章对你有用,请随意赞赏