<?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.5.xsd">
<!-- <bean/> definitions here -->
<bean id="personDao" class="com.ethan.dao.impl.PersonDaoBean"/>
<bean id="personService" class="com.ethan.service.impl.PersonServiceBean" init-method="init" destroy-method="close">
<property name="personDao" ref="personDao"/>
<property name="id" value="88"/>
</bean>
<!-- <bean id="personService2" class="com.ethan.service.factory.PersonServiceBeanFactory" factory-method="createPersonServiceBean"/>
<bean id="personServiceFactory" class="com.ethan.service.factory.PersonServiceBeanFactory"></bean>
<bean id="personService3" factory-bean="personServiceFactory" factory-method="createPersonServiceBean2"></bean> -->
</beans>
import java.util.ArrayList;
import java.util.List;
/**
* 对<bean>标签 的封装类
* @author ETHAN
*
*/
public class BeanDefinition {
private String id;
private String className;
private List<PropertyDefinition> propertys = new ArrayList<PropertyDefinition>();
public BeanDefinition(String id, String className) {
super();
this.id = id;
this.className = className;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
public List<PropertyDefinition> getPropertys() {
return propertys;
}
public void setPropertys(List<PropertyDefinition> propertys) {
this.propertys = propertys;
}
}
/**
* 属性标签 <property>封装类
* @author ETHAN
*
*/
public class PropertyDefinition {
private String name;
private String ref;
//<property name="id" value=""/>
private String value;
public PropertyDefinition(String name, String ref,String value) {
super();
this.name = name;
this.ref = ref;
this.value = value;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getRef() {
return ref;
}
public void setRef(String ref) {
this.ref = ref;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
package com.ethan.dao.impl;
import com.ethan.dao.PersonDao;
public class PersonDaoBean implements PersonDao {
/* (non-Javadoc)
* @see com.ethan.dao.impl.PersonDao#add()
*/
@Override
public void add() {
System.out.println("personDaoBean add()..........");
}
}
package com.ethan.service.impl;
import com.ethan.dao.PersonDao;
import com.ethan.service.PersonService;
public class PersonServiceBean implements PersonService {
private PersonDao personDao;
private Integer id;
public void init() {
System.out.println("初始化bean");
}
public PersonServiceBean() {
System.out.println("init...........");
}
@Override
public void save() {
// System.out.println("save()..........");
System.out.println(id);
personDao.add();
}
public void close() {
System.out.println("bean被销毁了");
}
public PersonDao getPersonDao() {
return personDao;
}
public void setPersonDao(PersonDao personDao) {
this.personDao = personDao;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
}
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
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.apache.commons.beanutils.ConvertUtils;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.XPath;
import org.dom4j.io.SAXReader;
public class EthanClassPathXMLApplicationContext {
private List<BeanDefinition> beanDefines = new ArrayList<BeanDefinition>();
private Map<String,Object> singletons = new HashMap<String, Object>();
public EthanClassPathXMLApplicationContext(String filename) throws Exception {
this.readXML(filename);
this.instanceBeans();
this.injectObject();
}
/*
*属性注入
*/
private void injectObject() {
for(BeanDefinition beanDefinition:beanDefines) {
Object bean = singletons.get(beanDefinition.getId());
if(bean!=null) {
//获得此对象的所有属性
try {
//这个对象的所有成员变量属性
PropertyDescriptor[] ps = Introspector.getBeanInfo(bean.getClass()).getPropertyDescriptors();
//标签注入的属性<property>
for(PropertyDefinition propertyDefinition:beanDefinition.getPropertys()) {
for(PropertyDescriptor properdesc:ps) {
if(propertyDefinition.getName().equals(properdesc.getName())) {
Method setter = properdesc.getWriteMethod();
if(setter!=null) {
Object propervalue = null;
if(propertyDefinition.getRef()!=null&&!"".equals(propertyDefinition.getRef().trim())) {
propervalue = singletons.get(propertyDefinition.getRef());
} else {
//基本类型属性
propervalue = ConvertUtils.convert(propertyDefinition.getValue(),properdesc.getPropertyType() );
System.out.println("id--------"+propervalue);
}
setter.setAccessible(true);
setter.invoke(bean, propervalue);
}
break;
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
private void readXML(String filename) {
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");
//创建beans/bean 查询路径 /根节点<beans> /+/beans+/+bean
XPath xsub = document.createXPath("//ns:beans/ns:bean");
xsub.setNamespaceURIs(nsMap);
//拿到docuemnt内的所有bean元素
List<Element> beans = xsub.selectNodes(document);
for(Element element:beans) {
String id = element.attributeValue("id");
String clazz = element.attributeValue("class");
BeanDefinition beanDefine = new BeanDefinition(id, clazz);
XPath propertysub = element.createXPath("ns:property");
propertysub.setNamespaceURIs(nsMap);
//这个<bean>下的所有<property>
List<Element> propertys = propertysub.selectNodes(element);
for(Element property:propertys) {
String propertyName = property.attributeValue("name");
String propertyref = property.attributeValue("ref");
//基本属性值
String properValue = property.attributeValue("value");
PropertyDefinition propertyDefinition = new PropertyDefinition(propertyName, propertyref,properValue);
beanDefine.getPropertys().add(propertyDefinition);
}
beanDefines.add(beanDefine);
}
} catch(Exception e) {
e.printStackTrace();
}
}
private void instanceBeans() throws Exception {
for(BeanDefinition beanDefinition:beanDefines) {
if(beanDefinition.getClassName()!=null&&!"".equals(beanDefinition.getClassName().trim())) {
singletons.put(beanDefinition.getId(), Class.forName(beanDefinition.getClassName()).newInstance());
}
}
}
public Object getBean(String beanName) {
return this.singletons.get(beanName);
}
}
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.ethan.service.PersonService;
import com.ethan.service.impl.PersonServiceBean;
public class SpringTest {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
// ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"beans.xml"});
/*AbstractApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"beans.xml"});
ctx.close();//关闭容器
*/ /* EthanClassPathXMLApplicationContext ctx = new EthanClassPathXMLApplicationContext("beans.xml");
PersonService ps = (PersonService) ctx.getBean("personService");
ps.save();*/
//test1();
//test3();
/* ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"beans.xml"});
PersonService ps = (PersonService) ctx.getBean("personService");
ps.save();*/
test5();
}
public static void test1() {
ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"beans.xml"});
PersonService ps = (PersonService) ctx.getBean("personService2");
ps.save();
}
public static void test2() {
ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"beans.xml"});
PersonService ps = (PersonService) ctx.getBean("personService3");
ps.save();
}
public static void test3() {
ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"beans.xml"});
PersonService ps = (PersonService) ctx.getBean("personService");
PersonService ps2 = (PersonService) ctx.getBean("personService");
System.out.println(ps==ps2);
}
public static void test4() throws Exception {
EthanClassPathXMLApplicationContext ctx = new EthanClassPathXMLApplicationContext("beans.xml");
PersonService ps = (PersonService) ctx.getBean("personService");
ps.save();
}
public static void test5() throws Exception {
EthanClassPathXMLApplicationContext ctx = new EthanClassPathXMLApplicationContext("beans.xml");
PersonService ps = (PersonService) ctx.getBean("personService");
ps.save();
}
}
spring2.5 之 编码实现 IOC和依赖注入
最新推荐文章于 2022-04-19 14:02:52 发布