java自定义spring,自定义java类实现类似spring的依赖注入功能

objPool = Collections.synchronizedMap(new HashMap());

//保存配置文件中对应的文档对象

private Document doc;

//保存配置文件里面的根元素

private Element root;

/**

*

*/

public MyXmlApplicationContextUtil() {

super();

}

/**

*

*/

public MyXmlApplicationContextUtil(String filePath)throws Exception{

SAXReader reader = new SAXReader();

doc = reader.read(new File(filePath));

root = doc.getRootElement();

initPool();

initProp();

}

public Object getBean(String name) throws BeansException {

Object target = objPool.get(name);

Object obj = null;

if(target.getClass() != String.class){

obj =  target;

}else{

String clazz = (String)target;

try{

obj = Class.forName(clazz).newInstance();

}catch(Exception e){

e.printStackTrace();

}

}

return obj;

}

private void initPool()throws Exception{

for(Object obj:root.elements()){//遍历每个bean元素

Element ele = (Element)obj;

String beanId = ele.attributeValue("id");

String beanClazz = ele.attributeValue("class");

String beanScope = ele.attributeValue("scope");

if(beanScope == null || beanScope.equals("singleton")){

objPool.put(beanId, Class.forName(beanClazz).newInstance());

}else{

objPool.put(beanId, beanClazz);

}

}

}

private void initProp()throws Exception{

for(Object obj:root.elements()){//遍历每个bean元素

Element ele = (Element)obj;

String beanId = ele.attributeValue("id");

String beanScope = ele.attributeValue("scope");

if(beanScope == null || beanScope.equals("beanScope")){

Object bean = objPool.get(beanId);

for(Object prop:ele.elements()){

Element propElem = (Element)prop;

String propName = propElem.attributeValue("name");

String propVal = propElem.attributeValue("value");

String propRef = propElem.attributeValue("ref");

String propNameOfFirstLetter = propName.substring(0,1).toUpperCase()+propName.substring(1);

if(propVal != null && propVal.length()>0){

Method setter = bean.getClass().getMethod("set" + propNameOfFirstLetter, String.class);

setter.invoke(bean, propVal);

}

if(propRef !=null && propRef.length()>0){

Object target = objPool.get(propRef);

if(target == null){};

Method setter = null;

for(Class superInterface:target.getClass().getInterfaces()){

try{

setter = bean.getClass().getMethod("set" + propNameOfFirstLetter, superInterface);

setter.invoke(bean, propVal);

break;

}catch(NoSuchMethodException e){

continue;

}

}

if(setter == null){

setter = bean.getClass().getMethod("set" + propNameOfFirstLetter, target.getClass());

}

setter.invoke(bean, propVal);

}

}

}else{

//objPool.put(beanId, beanClazz);

}

}

}

public static void main(String[] args)throws Exception{

ApplicationContext ac = new MyXmlApplicationContextUtil("D:\\Myeclipse6.5\\webBookShop\\WebRoot\\bean.xml");

StudentAction s = (StudentAction)ac.getBean("StudentAction");

System.out.println(s);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值