我是靠谱客的博主 单纯哈密瓜,最近开发中收集的这篇文章主要介绍简单模拟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"));
}

最后附上整个工程:

最后

以上就是单纯哈密瓜为你收集整理的简单模拟spring框架原理的全部内容,希望文章能够帮你解决简单模拟spring框架原理所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(89)

评论列表共有 0 条评论

立即
投稿
返回
顶部