Spring底层封装好的类
ApplicationContext类
public class ApplicationContext {
private String path; //用户输入xml的路径
private Map<String, Object> map= new HashMap<>();
public ApplicationContext(String path) {
this.path = path;
//this代表的就是当前对象,传递this的目的就是为了在另外一个类中获得更多的参数
MyXMLParser.str(this);
}
public Map<String, Object> getMap() {
return map;
}
public void setMap(Map<String, Object> map) {
this.map = map;
}
public ApplicationContext(String path, Map<String, Object> map) {
this.path = path;
this.map = map;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
//获取单个对象
public Object getBean(String id){
return map.get(id);
}
//类型转换,泛型方法
public <T> T getBean(String id,Class<T> t){
return (T)map.get(id);
}
}
MyXMLParser解析(使用SAX解析)
public class MyXMLParser {
public static void str(ApplicationContext applicationContext){
try {
//SAX解析器工厂对象
SAXParserFactory spf = SAXParserFactory.newInstance();
//基于工厂对象获取解析器对象
SAXParser sp = spf.newSAXParser();
//使用解析器解析xml,获取applicationContext里面的路径,并且通过对象调用通过一个Map集合
sp.parse(new File(applicationContext.getPath()),new MyHandler(applicationContext.getMap()));
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
//使用静态内部类处理
private static class MyHandler extends DefaultHandler {
private Map<String, Object> map;
public MyHandler(Map<String, Object> map) {
this.map = map; //定义有参构造等于当前类中的map
}
//clazz
private Class clazz;
private Object o;
//参数分别为:uri标识 标签前缀 标签名 标签的属性
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes){
//System.out.println(qName+"解析开始"); //查询开始的标签名
try {
if ("bean".equals(qName)) { //判读bean是否为开始标签名
//获得bean的id和class属性值
String id = attributes.getValue("id");
String name = attributes.getValue("class");
//使用反射创建Class对象,获得bean里面的class对象
clazz = Class.forName(name); //通过变量name得到对象
//创建当前类对象,并且为无参构造,public修饰的
o = clazz.getConstructor().newInstance();
//System.out.println(id + "--" + name);
//System.out.println(o);
//通过map键判断xml是否出现重复的id,出现重复的直接报错
if (map.containsKey(id)){
throw new RuntimeException("配置文件中不可以存在重复ID名称为"+id);
}
//把创建的对象保存到map集合中,key:就是bean的id,value:就是根据class创建出的对象
map.put(id,o);
}
if ("property".equals(qName)){ //判断property是否为开始标签
//获得property的name和age属性值
String name = attributes.getValue("name");
String value = attributes.getValue("value");
//通过反射设置属性name到对象中
Field field = clazz.getDeclaredField(name);
field.setAccessible(true); //突破封装限制
//实体类中age属性是int类型,而配置文件中age是字符串类型,需要判断
//field.set(o,value); //等价于set.field(k,v),往o对象中设置field值也就是name的值,然后往o对象field中设置value值
String simpleName = field.getType().getSimpleName(); //判断实体类的属性的类型
switch (simpleName){
case"String":
field.set(o,value);
break;
case"int":
field.set(o,Integer.parseInt(value));
break;
}
//System.out.println(simpleName);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
}
}
}
dtd文件
<!-- 该dtd文件需要打包到jar中, 所以需要放在src中 -->
<!-- 1.标签的指定 -->
<!ELEMENT beans (bean*)>
<!ELEMENT bean (property*)>
<!ELEMENT property (#PCDATA)>
<!-- 2.属性的指定 -->
<!--
bean:
id : 该对象的唯一标识
class : 包名+类名
property:
name : 属性名
value : 属性值
-->
<!ATTLIST bean id CDATA #REQUIRED class CDATA #REQUIRED>
<!ATTLIST property name CDATA #REQUIRED value CDATA #REQUIRED>
需要程序员写的类
实体类
public class Student {
private String name;
private int age;
public Student(){
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
applicationContext.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!--使用dtd进行根标签规范-->
<!DOCTYPE beans SYSTEM "bean.dtd">
<beans>
<bean id="stu" class="com.zhang.demo.Student">
<property name="name" value="zs"></property>
<property name="age" value="19"></property>
</bean>
<bean id="stu2" class="com.zhang.demo.Student">
<property name="name" value="ls"></property>
<property name="age" value="29"></property>
</bean>
</beans>
测试类
public class Test {
public static void main(String[] args) {
ApplicationContext app = new ApplicationContext("D:\\Code\\javase\\spring-ioc-xy\\src\\applicationContext.xml");
//获取底层创建的所有的对象
Map<String, Object> map = app.getMap();
System.out.println(map);
//根据传入的id获得指定对象
//Object stu = app.getBean("stu");
//System.out.println(stu);
//根据传入的id获得指定对象,不用强制转换
//Student stu1 = app.getBean("stu", Student.class);
//User user = app.getBean("user", User.class);
}
}
注意:我们自己封装一个类必须具备三点:项目的Jar包,API文档,源码