Loading... ## 介绍: 在传统的java程序设计中,我们通过new关键字来创建对象,这是程序主动去创建依赖对象;而在Spring中有一个专门的容器来创建和管理这些对象,并将对象依赖的其它对象注入该对象,这个容器我们一般称为Ioc容器。有了Ioc容器,我们就不需要关注这些对象的创建和销毁,只需要使用对象时,从容器获取即可。 ## 具体实现: ### Student实体类: ```java /** * @code Description * @code author 本当迷 * @code date 2023/1/17-11:52 */ public class Student { private String name; // 姓名 private Integer age; // 年龄 private String gender; // 性别 public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", age=" + age + ", gender='" + gender + '\'' + '}'; } } ``` ### xml配置文件: ```java <xml> <bean id="student" class="Student"> <property name="name">bdm</property> <property name="gender">male</property> <property name="age">18</property> </bean> </xml> ``` ### BeanFactory工厂: ```java import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import java.io.IOException; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.util.HashMap; import java.util.Map; /** * @code Description * @code author 本当迷 * @code date 2023/1/17-11:54 */ public class BeanFactory { // bean仓库 private static final Map<String, Object> beanMap = new HashMap<>(); // 获取bean public static Object getBean(String name){ return beanMap.get(name); } // 创建bean工厂时就初始化bean static { initialization(); } private static Map<String, String> getAttributes(NodeList attributeNodes){ Map<String, String> keyValue = new HashMap<>(); for(int i = 0; i < attributeNodes.getLength(); i++){ final Node item = attributeNodes.item(i); if(item.getNodeType() == Node.ELEMENT_NODE){ Element element = (Element) item; // 获取属性名 final String name = element.getAttribute("name"); // 获取属性名的值 final String value = element.getFirstChild().getNodeValue(); keyValue.put(name, value); } } return keyValue; } private static void initialization(){ Document document = null; try { final DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); final DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); document = documentBuilder.parse("resources/beans.xml"); } catch (ParserConfigurationException | SAXException | IOException e) { throw new RuntimeException(e); } // 获取xml中的bean标签 final NodeList bean = document.getElementsByTagName("bean"); for(int i = 0; i < bean.getLength(); i++){ final Element item = (Element) bean.item(i); // 获取bean标签中的子标签 final NodeList childNodes = item.getChildNodes(); final Map<String, String> attributes = getAttributes(childNodes); // 获取bean的id final String id = item.getAttribute("id"); // 获取bean的class final String beanClass = item.getAttribute("class"); // 使用反射实例化对象(关键代码) Object instance = null; try { final Class<?> aClass = Class.forName(beanClass); instance = aClass.getConstructor().newInstance(); final Field[] fields = aClass.getDeclaredFields(); for(Field field : fields){ field.setAccessible(true); final String name = field.getName(); final Class<?> type = field.getType(); // 如果是整型 if(type == Integer.class){ field.set(instance, Integer.valueOf( attributes.get(name))); } // 如果是字符串类型 if(type == String.class){ field.set(instance, attributes.get(name)); } } } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { e.printStackTrace(); } beanMap.put(id, instance); } } } ``` ### 调用: ```java public class MyTest { public static void main(String[] args) throws Exception { Student student = (Student) BeanFactory.getBean("student"); System.out.println(student); } } ``` ### 效果图: > 这里我们完全是使用beanFactory工厂创建出来的对象,而不是我们自己new出来的  最后修改:2023 年 01 月 17 日 © 允许规范转载 打赏 赞赏作者 支付宝微信 赞 0 如果文章有用,请随意打赏。