---
title: "简单spring框架原理"
author: "shaohan126448"
date: "2011-01-28T19:16:11Z"
tags: ["Bean", "J#", "Spring", "XML", "框架"]
categories: ["企业架构"]
slug: "spring"
draft: false
lastmod: "2011-01-28T19:16:11Z"
id: 899193
url: "https://shaohan126448.iteye.com/blog/899193"
---

首先定义一个接口：  
     public interface Action {  
    String excute(String msg);  
}

然后定义一个类，来实现这个接口：  
    
public class UppperCaseAction implements Action{  
    private String message;  
public String getMessage() {  
return message;  
}  
public void setMessage(String message) {  
this.message = message;  
}  
public String excute(String msg) {

return (message+" "+msg).toUpperCase();  
}

}

然后就是配置文件了：applicationContext.xml  
   &lt;?xml version="1.0" encoding="UTF-8"?&gt;  
&lt;beans  
xmlns="http://www.springframework.org/schema/beans"  
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"&gt;  
    &lt;bean name="uppercase" class="com.hcy.Spring.UppperCaseAction"&gt;  
       &lt;property name="message"&gt;  
         &lt;value&gt;hello&lt;/value&gt;  
       &lt;/property&gt;  
    &lt;/bean&gt;  
&lt;/beans&gt;

通过反射获取bean实例的类：  
   package com.hcy.model;

import java.lang.reflect.InvocationTargetException;  
import java.lang.reflect.Method;  
import java.util.HashMap;

import com.hcy.opxml.Mapping;  
import com.hcy.opxml.ParseXml;

public class ApplicationContext {  
private static HashMap&lt;String,Mapping&gt; map=null;  
static{  
map=ParseXml.ParseApplicationContext();  
}  
    public Object getBean(String id){   
    Object obj=null;  
    Method\[] methods=null;  
    try {  
    System.out.println(map.get(id).getClassName());  
Class clz=Class.forName(map.get(id).getClassName());  
obj=clz.newInstance();  
methods=clz.getMethods();  
} catch (ClassNotFoundException e) {  
// TODO Auto-generated catch block  
e.printStackTrace();  
} catch (InstantiationException e) {  
// TODO Auto-generated catch block  
e.printStackTrace();  
} catch (IllegalAccessException e) {  
// TODO Auto-generated catch block  
e.printStackTrace();  
}  
for(Method m:methods){  
if(m.getName().equalsIgnoreCase(("set"+map.get(id).getProperty()))){  
try {  
m.invoke(obj, new Object\[]{map.get(id).getValue()});  
} catch (IllegalArgumentException e) {  
// TODO Auto-generated catch block  
e.printStackTrace();  
} catch (IllegalAccessException e) {  
// TODO Auto-generated catch block  
e.printStackTrace();  
} catch (InvocationTargetException e) {  
// TODO Auto-generated catch block  
e.printStackTrace();  
}  
}  
}  
System.out.println(obj.toString());  
    return obj;  
    }  
}

解析xml的类：  
   package com.hcy.opxml;

import java.io.File;  
import java.io.IOException;  
import java.util.HashMap;

import javax.xml.parsers.DocumentBuilder;  
import javax.xml.parsers.DocumentBuilderFactory;  
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;  
import org.w3c.dom.Element;  
import org.w3c.dom.NodeList;  
import org.xml.sax.SAXException;

public class ParseXml {  
    private static HashMap&lt;String ,Mapping&gt; map=new HashMap&lt;String ,Mapping&gt;();  
public  static HashMap&lt;String,Mapping&gt; ParseApplicationContext(){  
    DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();  
    DocumentBuilder builder=null;  
    try {  
builder=factory.newDocumentBuilder();  
} catch (ParserConfigurationException e) {  
// TODO Auto-generated catch block  
e.printStackTrace();  
}  
Document doc=null;  
try {  
doc = builder.parse(new File("src/applicationContext.xml"));  
} catch (SAXException e1) {  
// TODO Auto-generated catch block  
e1.printStackTrace();  
} catch (IOException e1) {  
// TODO Auto-generated catch block  
e1.printStackTrace();  
}  
NodeList nodes=doc.getElementsByTagName("bean");  
for(int i=0;i&lt;nodes.getLength();i++){  
Element e=(Element)nodes.item(i);  
String id=e.getAttribute("name");  
String className=e.getAttribute("class");  
nodes=e.getElementsByTagName("property");  
for(int j=0;j&lt;nodes.getLength();j++){  
e=(Element)nodes.item(j);  
String property=e.getAttribute("name");  
String value=e.getElementsByTagName("value").item(0).getFirstChild().getNodeValue();  
Mapping mapping=new Mapping();  
mapping.setClassName(className);  
mapping.setId(id);  
mapping.setProperty(property);  
mapping.setValue(value);  
System.out.println(id+" "+className+" "+property+" "+value);  
map.put(mapping.getId(), mapping);  
}  
}  
    return map;  
     }  
public static void main(String\[] args){  
ParseXml px=new ParseXml();  
px.ParseApplicationContext();  
}

}

通过xml当中的配置，与对应的类对应起来，准确地得到bean的实例：  
      package com.hcy.opxml;

public class Mapping {  
       private String id;  
       private String className;  
       private String property;  
       private String value;  
        
public String getClassName() {  
return className;  
}  
public void setClassName(String className) {  
this.className = className;  
}  
public String getId() {  
return id;  
}  
public void setId(String id) {  
this.id = id;  
}  
public String getProperty() {  
return property;  
}  
public void setProperty(String property) {  
this.property = property;  
}  
public String getValue() {  
return value;  
}  
public void setValue(String value) {  
this.value = value;  
}  
        
}

接下来就是测试类：  
public static void main(String\[] args) {  
ApplicationContext ac=new FileSystemXmlApplicationContext("src/applicationContext.xml");  
        //通过反射机制得到bean的实例  
        Action act=(Action)ac.getBean("uppercase");  
        System.out.println(act.excute("yes"));  
}