您的位置:首页 > 编程语言 > Java开发

spring容器(注入的方式)

2014-09-05 23:50 225 查看
package spring;

import java.beans.IntrospectionException;

import java.beans.Introspector;

import java.beans.PropertyDescriptor;

import java.lang.reflect.InvocationTargetException;

import java.lang.reflect.Method;

import java.net.URL;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import org.dom4j.Document;

import org.dom4j.Element;

import org.dom4j.XPath;

import org.dom4j.io.SAXReader;

public class XmlApplicationContext {

private List<BeanDefinition> beanDefines = new ArrayList<BeanDefinition>();

private Map<String , Object> sigletons = new HashMap<String,Object>();

public XmlApplicationContext(String filename) throws IllegalAccessException, ClassNotFoundException{

this.readXML(filename);

this.instanceBean();

this.injectObject();

}

/**

* 实例化Bean方法!

* @throws ClassNotFoundException

* @throws IllegalAccessException

*/

private void instanceBean() throws IllegalAccessException, ClassNotFoundException {

// TODO Auto-generated method stub

for(BeanDefinition beanDefinition :beanDefines){

try {

///判断不等空 和判断不等于空字符串

if(beanDefinition.getClassName()!=null&&!"".equals(beanDefinition.getClassName().trim()))

sigletons.put(beanDefinition.getId(), Class.forName(beanDefinition.getClassName()).newInstance());

} catch (InstantiationException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

/**

* 用于读取配置文件beans.xml中的内容(id, class)

* @param filename

*/

private void readXML(String filename) {

// TODO Auto-generated method stub

SAXReader saxReader = new SAXReader();

Document document=null;

try{

URL xmlpath = this.getClass().getClassLoader().getResource(filename);

document = saxReader.read(xmlpath);

Map<String,String> nsMap = new HashMap<String,String>();

nsMap.put("ns","http://www.springframework.org/schema/beans");//加入命名空间

XPath xsub = document.createXPath("//ns:beans/ns:bean");//创建beans/bean查询路径

xsub.setNamespaceURIs(nsMap);//设置命名空间

List<Element> beans = xsub.selectNodes(document);//获取文档下所有bean节点

for(Element element: beans){

String id = element.attributeValue("id");//获取id属性值

String clazz = element.attributeValue("class"); //获取class属性值

BeanDefinition beanDefine = new BeanDefinition(id, clazz);

XPath propertysub = element.createXPath("//ns:property");

propertysub.setNamespaceURIs(nsMap);

List<Element> propertys = propertysub.selectNodes(element);

for(Element property:propertys){

String propertyName = property.attributeValue("name");

String propertyRef = property.attributeValue("ref");

System.out.println(propertyName+" "+propertyRef);

PropertyDefinition propertyDefinition = new PropertyDefinition(propertyName,propertyRef);

beanDefine.getProtertys().add(propertyDefinition);

}

beanDefines.add(beanDefine);

}

}catch(Exception e){

e.printStackTrace();

}

}

/**

* 获取Bean实例

* @param beanName

* @return

*/

public Object getBean(String beanName){

return this.sigletons.get(beanName);

}

/**

* 为BEAN 对象注入值

*/

private void injectObject() {

// TODO Auto-generated method stub

for(BeanDefinition beanDefine : beanDefines){

Object bean = sigletons.get(beanDefine.getId());

if(bean!=null){

try {

PropertyDescriptor[] ps = Introspector.getBeanInfo(bean.getClass()).getPropertyDescriptors();

for(PropertyDefinition propertyDefinition : beanDefine.getProtertys()){

for(PropertyDescriptor properdesc : ps){

if(propertyDefinition.getName().equals(properdesc.getName())){

Method setter = properdesc.getWriteMethod();// 获取属性的set方法!

if(setter!=null){

Object value = sigletons.get(propertyDefinition.getRef());

setter.setAccessible(true);

try {

setter.invoke(bean, value);

} catch (IllegalAccessException

| IllegalArgumentException

| InvocationTargetException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

break;

}

}

}

} catch (IntrospectionException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: