简单模拟spring的容器管理bean
百度广告
package com.spring;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import com.vo.BeanDefinition;
* 简单模拟spring的ClassPathXmlApplicationContext spring容器管理bean
* @author examda
*/
public class MyApplicationContext
private List list = new ArrayList();
public MyApplicationContext(String fileName)
this.readXML(fileName);
{
}
{
}
/**
*
* @throws IllegalAccessException
*/
IllegalAccessException, ClassNotFoundException
for (BeanDefinition bean : list)
if (null != bean.getClassName() && bean.getClassName().length() > 0)
.newInstance());
}
* 读取xml配置
* @param fileName
public void readXML(String fileName)
SAXBuilder builder = new SAXBuilder();
try
Document doc = builder.build(url);
xpath.addNamespace("ns",
List beans = xpath.selectNodes(doc);
{
String className = bean.getAttributeValue("class");
className);
}
catch (JDOMException e)
e.printStackTrace();
catch (IOException e)
e.printStackTrace();
}
* 得到指定id的bean
* @param id
*/
{
}
测试:
public void testMyUserService()
MyApplicationContext ctx = new MyApplicationContext("beans.xml");
us.print();
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import com.vo.BeanDefinition;
* 简单模拟spring的ClassPathXmlApplicationContext spring容器管理bean
* @author examda
*/
public class MyApplicationContext
private List list = new ArrayList();
public MyApplicationContext(String fileName)
this.readXML(fileName);
{
}
{
}
/**
*
* @throws IllegalAccessException
*/
IllegalAccessException, ClassNotFoundException
for (BeanDefinition bean : list)
if (null != bean.getClassName() && bean.getClassName().length() > 0)
.newInstance());
}
* 读取xml配置
* @param fileName
public void readXML(String fileName)
SAXBuilder builder = new SAXBuilder();
try
Document doc = builder.build(url);
xpath.addNamespace("ns",
List beans = xpath.selectNodes(doc);
{
String className = bean.getAttributeValue("class");
className);
}
catch (JDOMException e)
e.printStackTrace();
catch (IOException e)
e.printStackTrace();
}
* 得到指定id的bean
* @param id
*/
{
}
测试:
public void testMyUserService()
MyApplicationContext ctx = new MyApplicationContext("beans.xml");
us.print();
编辑推荐:
下载Word文档
温馨提示:因考试政策、内容不断变化与调整,长理培训网站提供的以上信息仅供参考,如有异议,请考生以权威部门公布的内容为准! (责任编辑:长理培训)
点击加载更多评论>>