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();
}
}
}
}
}
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();
}
}
}
}
}
相关文章推荐
- Spring——容器,注入(配置和注解两种方式)
- SpringIOC容器创建对象及依赖注入的几种方式
- 【Spring 基础篇二】IoC容器的两种注入方式
- 【SSH快速进阶】——Spring IOC容器注入的几种方式
- Spring注入方式四:通过扫描类路径来把类对象注入Spring容器
- Spring中Ioc容器的注入方式
- Spring——容器,注入(配置和注解两种方式)
- 【Spring】【IOC】【Spring容器注入Bean对象的四种方式】【Spring中注入bean对象的注解】
- Spring中接口注入的三种方式
- spring的三种注入方式
- Spring的 IOC 依赖注入方式
- Spring中接口注入的三种方式
- Spring 容器创建的3种方式
- spring 构造注入方式,以及Ajax初接触
- spring注入方式注意:local/bean区别
- 传智播客 Spring中bean的实例化方式与IOC容器总结
- Spring2.5 IoC之bean的四种注入方式(理论篇)
- 普通java类如何取得注入spring Ioc容器的对象
- Spring--初始化IOC容器的几种方式
- spring 依赖注入之设值注入方式