简单spring框架原理

首先定义一个接口:
     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
   <?xml version="1.0" encoding="UTF-8"?>
<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">
    <bean name="uppercase" class="com.hcy.Spring.UppperCaseAction">
       <property name="message">
         <value>hello</value>
       </property>
    </bean>
</beans>

通过反射获取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<String,Mapping> 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<String ,Mapping> map=new HashMap<String ,Mapping>();
public  static HashMap<String,Mapping> 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<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<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"));
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值