spring学习笔记(一)-----sping的简单模拟

   xm+dom4j+javaReflect实现sping的一个简单模拟

 

    Bean.xml代码

<?xml version="1.0" encoding="UTF-8"?>
<beans>
	<bean id="hello" class="com.ynxinhua.bean.Hello">
		<property name="message" value="我的第一个Spring2.5程序!"/>
	</bean>
	
	<bean id="student" class="com.ynxinhua.bean.Student">
		<property name="name" value="张三"/>
		<property name="age" value="20"/>
		<property name="sex" value="男"/>
	</bean>
</beans>

 

     Hello.java

package com.ynxinhua.bean;

public class Hello {
	private String message;

	public void setMessage(String message) {
		this.message = message;
	}

	public String getMessage() {
		return message;
	}
}

 

    Student.java

package com.ynxinhua.bean;
public class Student  {
	private String name;
	private int age;
	private String sex;

	public Student() {

	}

	public Student(String name, int age, String sex) {
		this.name = name;
		this.age = age;
		this.sex = sex;
	}

	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 getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

}

 

    ApplicationContext.java

package com.ynxinhua.spring;

public interface ApplicationContext {
	public Object getbean(String beanId);//根据配置的id属性获取一个bean实例

}

 

    ClassPathXmlApplicationContext.java

package com.ynxinhua.spring;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class ClassPathXmlApplicationContext implements ApplicationContext {
	private Map beans = new HashMap();

	public ClassPathXmlApplicationContext(String filepath) throws Exception {
		SAXReader reader = new SAXReader();
		Document document = reader.read("bin/" + filepath);// 构建dom4j树
		Element root = document.getRootElement();// 获取根元素
		List<Element> list = root.elements("bean");// 获取所有bean元素
		for (Element e : list) {
			String beanid = e.attributeValue("id");// 获取id的属性值
			String className = e.attributeValue("class");// 获取class的属性值
			Class class1 = Class.forName(className);
			Object beanObject = class1.newInstance();// 实例化bean对象
			beans.put(beanid, beanObject);// 放入beans中
			List<Element> list2 = e.elements("property");

			for (Element e1 : list2) {
				String name = e1.attributeValue("name");
				String value = e1.attributeValue("value");
				// System.out.println(class1.getDeclaredField("name").getType().getName());
				String parameterType = class1.getDeclaredField(name).getType().getName();// 获得属性的参数类型
				String methodName = "set" + name.substring(0, 1).toUpperCase() + name.substring(1);
				Method method = null;
				if (parameterType.equals("int")) {
					method = class1.getMethod(methodName, int.class);
				}
				else{
				method = class1.getMethod(methodName, Class.forName(parameterType));
				}
				if (parameterType.equals("int")) {
					method.invoke(beanObject, Integer.parseInt(value));
				}
				else{
				method.invoke(beanObject, value);
				}
			}
		}
	}

	public Map getBeans() {
		return beans;
	}

	public void setBeans(Map beans) {
		this.beans = beans;
	}

	@Override
	public Object getbean(String beanId) {

		return beans.get(beanId);
	}

}

 

    Demo.java

package com.ynxinhua.demo;

import com.ynxinhua.bean.Hello;
import com.ynxinhua.bean.Student;
import com.ynxinhua.spring.ApplicationContext;
import com.ynxinhua.spring.ClassPathXmlApplicationContext;

public class Demo {

	public static void main(String[] args) throws Exception {
		ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
		Hello hello=(Hello) context.getbean("hello");
		System.out.println(hello.getMessage());
		
		Student student=(Student) context.getbean("student");
		System.out.println(student.getName());
		System.out.println(student.getAge());
		System.out.println(student.getSex());

	}

}

    例子源码见附件.导入myeclipse运行的时候需要dom4j的jar包

 

 

你可能感兴趣的:(java,spring,bean,xml,MyEclipse)