php网站源码安装教程,网站开发工程师asp考试,淘客的手机网站怎么做,网络技术专业定义一个容器#xff0c;使用ConcurrentHashMap 做为单例对象的容器
先解析beans.xml得到第一个bean对象的信息#xff0c;id#xff0c;class#xff0c;属性和属性值使用反射生成对象#xff0c;并赋值将创建好的bean对象放入到singletonObjects集合中提供getBean(id)方…定义一个容器使用ConcurrentHashMap 做为单例对象的容器
先解析beans.xml得到第一个bean对象的信息idclass属性和属性值使用反射生成对象并赋值将创建好的bean对象放入到singletonObjects集合中提供getBean(id)方法可以返回对应的bean对象
monster bean
public class Monster {private Integer id;private String name;private String skill;public Monster() {}public Integer getId() {return id;}public void setId(Integer id) {this.id id;}public String getName() {return name;}public void setName(String name) {this.name name;}public String getSkill() {return skill;}public void setSkill(String skill) {this.skill skill;}Overridepublic String toString() {return Monster{ id id , name name \ , skill skill \ };}
}
package com.sparrow.spring.application;import com.sparrow.spring.bean.Monster;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;import java.io.File;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;/*** Author: 诉衷情の麻雀* Description: TODO* DateTime: 2023/7/24 8:09**/
public class SpaApplicationContext {private ConcurrentHashMapString, Object singletonObjects new ConcurrentHashMap();//构造器 接收一个容器的配置文件public SpaApplicationContext(String iocBeanXmlFile) throws Exception {//1. 得到类加载路径String path this.getClass().getResource(/).getPath();//2.创建SaxReaderSAXReader saxReader new SAXReader();//3.得到Document对象Document document saxReader.read(new File(path iocBeanXmlFile));//4.得到rootElementElement rootElement document.getRootElement();ListElement elements rootElement.elements(bean);for (Element bean : elements) {String id bean.attributeValue(id);String classFullPath bean.attributeValue(class);Integer monsterId null;String name ;String skill ;//遍历bean下面的property属性ListElement property bean.elements(property);for (Element elementProperty : property) {//如果是id把值存起来if (id.equalsIgnoreCase(elementProperty.attributeValue(name))) {monsterId Integer.valueOf(elementProperty.attributeValue(value));} else if (name.equalsIgnoreCase(elementProperty.attributeValue(name))) {name elementProperty.attributeValue(value);} else if (skill.equalsIgnoreCase(elementProperty.attributeValue(name))) {skill elementProperty.attributeValue(value);}//利用反射 根据类的全路径 进行实例化Class? aClass Class.forName(classFullPath);Monster o (Monster) aClass.newInstance();o.setId(monsterId);o.setName(name);o.setSkill(skill);//放入容器中singletonObjects.put(id, o);}}}public Object getBean(String id) {return singletonObjects.get(id);}
}
beans.xml
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdbean classcom.sparrow.spring.bean.Monster idmonster01property nameid value1/property namename value白骨精/property nameskill value吃唐僧//beanbean classcom.sparrow.spring.bean.Monster idmonster02property nameid value2/property namename value老鼠精/property nameskill value吸血//bean/beanspublic class Test {public static void main(String[] args) throws Exception {SpaApplicationContext spaApplicationContext new SpaApplicationContext(beans.xml);Monster monster02 (Monster) spaApplicationContext.getBean(monster02);Monster monster01 (Monster) spaApplicationContext.getBean(monster01);System.out.println(monster02);System.out.println(monster01);}
}