先发下骚扰,进了这间公司,感觉是废了,连一个明确的任务都没有,一家小公司实习。。闲得蛋疼,空闲只好自己找点东西给自己充下电,回补下自己的java基础.
不说了..
还是转入主题吧..
一直以来都只是想学如何用一个东西,学了spring、struts、hibernate框架,也都只是会用简单的运用,完全不怎么理解它里面的东西,也没怎么想研究过它里面的原理。我想,这也是太多数大学生的一种心理吧,觉得只会用这行了。
现在我写的这个东东,也不是我原创的,我也是参照前人的,可以说是基本上和前人的一样,没有改善,
就是照着前人的来写。
在spring中的,依赖注入是通过xml来配置,在这里,也是一样,通过dom4j来读取xml配置文件,主要通过实现ElementHandler接口来处理xml元素
第一步:先创建实体Bean,一个Person类,一个Persons类
Person类
package com.ioc.bean;
/**
* @author chenpeng
* @create_time Aug 10, 2010 8:58:30 AM
* @email onlychenpeng@msn.com
* @usage //todo
*/
public class Person {
private String name;
private int age;
private float height;
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 float getHeight() {
return height;
}
public void setHeight(float height) {
this.height = height;
}
}
Persons类
package com.ioc.bean;
public class Persons {
private Person i;
private Person you;
public Person getI() {
return i;
}
public void setI(Person i) {
this.i = i;
}
public Person getYou() {
return you;
}
public void setYou(Person you) {
this.you = you;
}
}
第二步:写xml配置文件
ioc_config.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans>
<bean id="me" class="com.ioc.bean.Person">
<property name="name">
<value>chenpeng</value>
</property>
<property name="age">
<value>24</value>
</property>
<property name="height">
<value>1.78</value>
</property>
</bean>
<bean id="you" class="com.ioc.bean.Person">
<property name="name">
<value>ying</value>
</property>
<property name="age">
<value>22</value>
</property>
<property name="height">
<value>1.58</value>
</property>
</bean>
<bean id="us" class="com.ioc.bean.Persons">
<property name="i">
<ref bean="me" />
</property>
<property name="you">
<ref bean="you" />
</property>
</bean>
</beans>
第三步:xml的操作类,读取xml的内容,根据xml的标签进行依次读取,用户可以通过getBean(beanId)方法来获取bean.
BeanFactory类
/**
*
*/
package com.ioc.factory;
import java.io.File;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import org.dom4j.Attribute;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.ElementHandler;
import org.dom4j.ElementPath;
import org.dom4j.io.SAXReader;
/**
* @author chenpeng
* @create_time Aug 10, 2010 9:02:23 AM
* @email onlychenpeng@msn.com
* @usage //todo
*/
public class BeanFactory {
private Map<String,Object> beanMap=new HashMap<String,Object>();
/**
* 初始化xml文件
* */
public void init(String xmlUrl){
SAXReader saxReader=new SAXReader();
File file=new File(xmlUrl);
try{
saxReader.addHandler("/beans/bean",new BeanHandler());
saxReader.read(file);
}
catch(DocumentException e){
System.out.println(e.getMessage());
}
}
/**
* 根据beanid来获取bean
* */
public Object getBean(String beanId){
Object obj=null;
obj=beanMap.get(beanId);
return obj;
}
private void setFieldValue(Object obj, Field field, String value) {
String fieldType=field.getType().getSimpleName();
try{
if("int".equals(fieldType)){
field.setInt(obj, new Integer(value));
}else if("float".equals(fieldType)){
field.setFloat(obj, new Float(value));
}else{
field.set(obj,value);
}
}
catch(Exception e){
e.printStackTrace();
}
}
private void setFieldValue(Object obj, Field field, Object bean) {
try {
field.set(obj, bean);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
/*************************以下是标签处理器***************************************/
/**
* 处理/beans/bean标签
* */
class BeanHandler implements ElementHandler {
private Object obj=null;
public void onStart(ElementPath path) {
Element beanElement=path.getCurrent(); //获取当前的元素
Attribute classAttribute=beanElement.attribute("class"); //获取class属性
Class<?> bean=null;
try{
//根据类名通过反射构造一个class
bean=Class.forName(classAttribute.getText());
}
catch(ClassNotFoundException e){
e.printStackTrace();
}
//获取bean中的所有的域
Field fields[]=bean.getDeclaredFields();
Map<String,Field> fieldMap=new HashMap<String,Field>();
//所有的域放在一个map中
for(Field field:fields){
fieldMap.put(field.getName(), field);
}
try{
obj=bean.newInstance(); //构造出一个bean实例对象
}
catch(Exception e){
e.printStackTrace();
}
//为property标签增加handler
path.addHandler("property", new PropertyHandler(fieldMap,obj));
}
public void onEnd(ElementPath path) {
Element currentElement=path.getCurrent();
Attribute idAttribute=currentElement.attribute("id");
beanMap.put(idAttribute.getText(), obj);
//去掉property处理器
path.removeHandler("property");
}
}
/**
* 处理property标签
* */
class PropertyHandler implements ElementHandler {
private Map<String,Field> fieldMap;
private Object obj;
public PropertyHandler(Map<String,Field> fieldMap,Object obj){
this.fieldMap=fieldMap;
this.obj=obj;
}
public void onEnd(ElementPath path) {
path.removeHandler("value");
path.removeHandler("ref");
}
public void onStart(ElementPath path) {
Element propertyElement=path.getCurrent();
Attribute nameAttribute=propertyElement.attribute("name");
path.addHandler("value", new ValueHandler(fieldMap,obj,nameAttribute));
path.addHandler("ref", new RefHandler(fieldMap,obj,nameAttribute));
}
}
/**
* 处理value标签
* */
class ValueHandler implements ElementHandler{
private Map<String,Field> fieldMap;
private Object obj;
private Attribute nameAttribute;
public ValueHandler(Map<String,Field> fieldMap,Object obj,Attribute nameAttribute){
this.fieldMap=fieldMap;
this.obj=obj;
this.nameAttribute=nameAttribute;
}
public void onEnd(ElementPath path) {
Element valueElement=path.getCurrent();
String strValue=valueElement.getText();
Field tempField=fieldMap.get(nameAttribute.getValue());
if(tempField!=null){
tempField.setAccessible(true);
//设置bean的属性的值
setFieldValue(obj,tempField,strValue);
}
}
public void onStart(ElementPath arg0) {
}
}
/**
* 处理ref标签
* */
class RefHandler implements ElementHandler{
private Map<String,Field> fieldMap;
private Object obj;
private Attribute nameAttribute;
private Object bean;
public RefHandler(Map<String,Field> fieldMap,Object obj,Attribute nameAttribute){
this.fieldMap=fieldMap;
this.obj=obj;
this.nameAttribute=nameAttribute;
}
public void onEnd(ElementPath arg0) {
Field tempField=fieldMap.get(nameAttribute.getValue());
if(tempField!=null){
tempField.setAccessible(true);
//设置bean的属性的值
setFieldValue(obj,tempField,bean);
}
}
public void onStart(ElementPath path) {
Element refElement=path.getCurrent();
Attribute beanAttribute=refElement.attribute("bean");
bean=getBean(beanAttribute.getValue());
}
}
}
第四步:通过测试单元来进行测试
package com.ioc.bean;
import com.ioc.factory.BeanFactory;
import junit.framework.TestCase;
public class TestIOC extends TestCase {
public void TestIOCBeans(){
String xmlUrl="src/ioc_config.xml";
BeanFactory factory=new BeanFactory();
factory.init(xmlUrl);
Person me=(Person)factory.getBean("me");
System.out.println("me's name:"+me.getName());
System.out.println("me's age:"+me.getAge());
System.out.println("me's height:"+me.getHeight());
Person you=(Person)factory.getBean("you");
System.out.println("you's name:"+you.getName());
System.out.println("you's age:"+you.getAge());
System.out.println("you's height:"+you.getHeight());
Persons us=(Persons)factory.getBean("us");
System.out.println(us.getI());
System.out.println(us.getYou());
System.out.println(us.getI().equals(me));
System.out.println(us.getYou().equals(you));
}
}
测试结果如下:
me's name:chenpeng
me's age:24
me's height:1.78
you's name:ying
you's age:22
you's height:1.58
com.ioc.bean.Person@4b035d
com.ioc.bean.Person@ca0115
true
true
不知道什么原因,工程文件老是上传不成功,上传到一半就显示找不到页面了