整体代码架构

1.定义配置类
1.1 查看xml
<?xml version="1.0" encoding="UTF-8"?>
<beans>
<bean id="userDao" class="com.it.dao.impl.UserDaoImpl">
<property name="username" value="zhangsan"></property>
<property name="password" value="123456"></property>
</bean>
<bean id="userService" class="com.it.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"></property>
</bean>
<bean id="student" class="com.it.domain.Student">
<property name="name" value="wangwu"></property>
</bean>
</beans>
1.2 PropertyValue
package com.spring.ioc.framework.beans;
public class PropertyValue {
private String name;
private String ref;
private String value;
public PropertyValue() {
}
public PropertyValue(String name, String ref, String value) {
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;
}
}
1.3 MutablePropertyValues
package com.spring.ioc.framework.beans;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class MutablePropertyValues implements Iterable<PropertyValue> {
private final List<PropertyValue> propertyValueList;
public MutablePropertyValues() {
this.propertyValueList = new ArrayList<PropertyValue>();
}
public MutablePropertyValues(List<PropertyValue> propertyValueList) {
if(propertyValueList == null) {
this.propertyValueList = new ArrayList<PropertyValue>();
} else {
this.propertyValueList = propertyValueList;
}
}
public PropertyValue[] getPropertyValues() {
return propertyValueList.toArray(new PropertyValue[0]);
}
public PropertyValue getPropertyValue(String propertyName) {
for (PropertyValue propertyValue : propertyValueList) {
if (propertyValue.getName().equals(propertyName)) {
return propertyValue;
}
}
return null;
}
public boolean isEmpty() {
return propertyValueList.isEmpty();
}
public MutablePropertyValues addPropertyValue(PropertyValue pv) {
for (int i = 0; i < propertyValueList.size(); i++) {
PropertyValue currentPv = propertyValueList.get(i);
if(currentPv.getName().equals(pv.getName())) {
propertyValueList.set(i,pv);
return this;
}
}
this.propertyValueList.add(pv);
return this;
}
public boolean contains(String propertyName) {
return getPropertyValue(propertyName) != null;
}
public Iterator<PropertyValue> iterator() {
return propertyValueList.iterator();
}
}
1.4 BeanDefinition
一个完整的bean配置
package com.spring.ioc.framework.beans;
public class BeanDefinition {
private String id;
private String className;
private MutablePropertyValues propertyValues;
public BeanDefinition() {
propertyValues = new MutablePropertyValues();
}
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 MutablePropertyValues getPropertyValues() {
return propertyValues;
}
public void setPropertyValues(MutablePropertyValues propertyValues) {
this.propertyValues = propertyValues;
}
}
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = applicationContext.getBean("userService", UserService.class);
2. 定义注册表相关类
将全部读取到的BeanDefinition存储起来
2.1 BeanDefinitionRegistry
package com.spring.ioc.framework.beans.factory.support;
import com.spring.ioc.framework.beans.BeanDefinition;
public interface BeanDefinitionRegistry {
void registerBeanDefinition(String beanName, BeanDefinition beanDefinition);
void removeBeanDefinition(String beanName) throws Exception;
BeanDefinition getBeanDefinition(String beanName) throws Exception;
boolean containsBeanDefinition(String beanName);
int getBeanDefinitionCount();
String[] getBeanDefinitionNames();
}
2.2 SimpleBeanDefinitionRegistry
package com.spring.ioc.framework.beans.factory.support;
import com.spring.ioc.framework.beans.BeanDefinition;
import java.util.HashMap;
import java.util.Map;
public class SimpleBeanDefinitionRegistry implements BeanDefinitionRegistry {
private final Map<String, BeanDefinition> beanDefinitionMap = new HashMap<String, BeanDefinition>();
public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition) {
beanDefinitionMap.put(beanName,beanDefinition);
}
public void removeBeanDefinition(String beanName) throws Exception {
beanDefinitionMap.remove(beanName);
}
public BeanDefinition getBeanDefinition(String beanName) throws Exception {
return beanDefinitionMap.get(beanName);
}
public boolean containsBeanDefinition(String beanName) {
return beanDefinitionMap.containsKey(beanName);
}
public int getBeanDefinitionCount() {
return beanDefinitionMap.size();
}
public String[] getBeanDefinitionNames() {
return beanDefinitionMap.keySet().toArray(new String[0]);
}
}
3.定义具体读取配置的类
3.1 BeanDefinitionReader
package com.spring.ioc.framework.beans.factory.support;
public interface BeanDefinitionReader {
BeanDefinitionRegistry getRegistry();
void loadBeanDefinitions(String configLocation) throws Exception;
}
package com.spring.ioc.framework.beans.factory.xml;
import com.spring.ioc.framework.beans.BeanDefinition;
import com.spring.ioc.framework.beans.MutablePropertyValues;
import com.spring.ioc.framework.beans.PropertyValue;
import com.spring.ioc.framework.beans.factory.support.BeanDefinitionReader;
import com.spring.ioc.framework.beans.factory.support.BeanDefinitionRegistry;
import com.spring.ioc.framework.beans.factory.support.SimpleBeanDefinitionRegistry;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import java.io.InputStream;
import java.util.List;
public class XmlBeanDefinitionReader implements BeanDefinitionReader {
private final BeanDefinitionRegistry registry;
public XmlBeanDefinitionReader() {
registry = new SimpleBeanDefinitionRegistry();
}
public BeanDefinitionRegistry getRegistry() {
return registry;
}
public void loadBeanDefinitions(String configLocation) throws Exception {
SAXReader reader = new SAXReader();
InputStream is = XmlBeanDefinitionReader.class.getClassLoader().getResourceAsStream(configLocation);
Document document = reader.read(is);
Element rootElement = document.getRootElement();
List<Element> beanElements = rootElement.elements("bean");
for (Element beanElement : beanElements) {
String id = beanElement.attributeValue("id");
String className = beanElement.attributeValue("class");
BeanDefinition beanDefinition = new BeanDefinition();
beanDefinition.setId(id);
beanDefinition.setClassName(className);
MutablePropertyValues mutablePropertyValues = new MutablePropertyValues();
List<Element> propertyElements = beanElement.elements("property");
for (Element propertyElement : propertyElements) {
String name = propertyElement.attributeValue("name");
String ref = propertyElement.attributeValue("ref");
String value = propertyElement.attributeValue("value");
PropertyValue propertyValue = new PropertyValue(name,ref,value);
mutablePropertyValues.addPropertyValue(propertyValue);
}
beanDefinition.setPropertyValues(mutablePropertyValues);
registry.registerBeanDefinition(id,beanDefinition);
}
}
}
4.IOC容器类(创建bean操作)
4.1 BeanFactory接口
package com.spring.ioc.framework.beans.factory;
public interface BeanFactory {
Object getBean(String name) throws Exception;
<T> T getBean(String name, Class<? extends T> clazz) throws Exception;
}
4.2 ApplicationContext接口,继承BeanFactory
package com.spring.ioc.framework.context;
import com.spring.ioc.framework.beans.factory.BeanFactory;
public interface ApplicationContext extends BeanFactory {
void refresh() throws IllegalStateException, Exception;
}
4.3 抽象方法 AbstractApplicationContext实现ApplicationContext的refresh()方法
package com.spring.ioc.framework.context.support;
import com.spring.ioc.framework.beans.BeanDefinition;
import com.spring.ioc.framework.beans.factory.support.BeanDefinitionReader;
import com.spring.ioc.framework.beans.factory.support.BeanDefinitionRegistry;
import com.spring.ioc.framework.context.ApplicationContext;
import java.util.HashMap;
import java.util.Map;
public abstract class AbstractApplicationContext implements ApplicationContext {
protected BeanDefinitionReader beanDefinitionReader;
protected Map<String, Object> singletonObjects = new HashMap<String, Object>();
protected String configLocation;
public void refresh() throws IllegalStateException, Exception {
beanDefinitionReader.loadBeanDefinitions(configLocation);
finishBeanInitialization();
}
private void finishBeanInitialization() throws Exception {
BeanDefinitionRegistry registry = beanDefinitionReader.getRegistry();
String[] beanNames = registry.getBeanDefinitionNames();
for (String beanName : beanNames) {
BeanDefinition beanDefinition = registry.getBeanDefinition(beanName);
getBean(beanName);
}
}
}
4.4 实现类ClassPathXmlApplicationContext,反射创建bean,继承AbstractApplicationContext,实现最顶层的BeanFactory
package com.spring.ioc.framework.context.support;
import com.spring.ioc.framework.beans.BeanDefinition;
import com.spring.ioc.framework.beans.MutablePropertyValues;
import com.spring.ioc.framework.beans.PropertyValue;
import com.spring.ioc.framework.beans.factory.support.BeanDefinitionRegistry;
import com.spring.ioc.framework.beans.factory.xml.XmlBeanDefinitionReader;
import com.spring.ioc.framework.utils.StringUtils;
import java.lang.reflect.Method;
public class ClassPathXmlApplicationContext extends AbstractApplicationContext {
public ClassPathXmlApplicationContext(String configLocation) {
this.configLocation = configLocation;
beanDefinitionReader = new XmlBeanDefinitionReader();
try {
this.refresh();
} catch (Exception e) {
}
}
public Object getBean(String name) throws Exception {
Object obj = singletonObjects.get(name);
if (obj != null) {
return obj;
}
BeanDefinitionRegistry registry = beanDefinitionReader.getRegistry();
BeanDefinition beanDefinition = registry.getBeanDefinition(name);
String className = beanDefinition.getClassName();
Class<?> clazz = Class.forName(className);
Object beanObj = clazz.newInstance();
MutablePropertyValues propertyValues = beanDefinition.getPropertyValues();
for (PropertyValue propertyValue : propertyValues) {
String propertyName = propertyValue.getName();
String value = propertyValue.getValue();
String ref = propertyValue.getRef();
if (ref != null && !"".equals(ref)) {
Object bean = getBean(ref);
String methodName = StringUtils.getSetterMethodByFieldName(propertyName);
Method[] methods = clazz.getMethods();
for (Method method : methods) {
if (methodName.equals(method.getName())) {
method.invoke(beanObj, bean);
}
}
}
if (value != null && !"".equals(value)) {
String methodName = StringUtils.getSetterMethodByFieldName(propertyName);
Method method = clazz.getMethod(methodName, String.class);
method.invoke(beanObj, value);
}
}
singletonObjects.put(name, beanObj);
return beanObj;
}
public <T> T getBean(String name, Class<? extends T> clazz) throws Exception {
Object bean = getBean(name);
if (bean == null) {
return null;
}
return clazz.cast(bean);
}
}
|