1 BeandefinitionRegister
package com.example03.support;
import com.example03.BeanDefinition;
import org.springframework.beans.factory.BeanDefinitionStoreException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
public interface BeanDefinitionRegistry {
void registerBeanDefinition(String var1, BeanDefinition var2) throws BeanDefinitionStoreException;
void removeBeanDefinition(String var1) throws NoSuchBeanDefinitionException;
BeanDefinition getBeanDefinition(String var1) throws NoSuchBeanDefinitionException;
boolean containsBeanDefinition(String var1);
String[] getBeanDefinitionNames();
int getBeanDefinitionCount();
}
2 SimpleBeanDefinitionRegister
package com.example03;
import com.example03.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.BeanDefinitionStoreException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import java.util.HashMap;
import java.util.Map;
public class SimpleBeanDefinitionRegistry implements BeanDefinitionRegistry {
Map<String, BeanDefinition> beanDefinitionMap=new HashMap<>();
@Override
public void registerBeanDefinition(String var1, BeanDefinition var2) throws BeanDefinitionStoreException {
this.beanDefinitionMap.put(var1,var2);
}
@Override
public void removeBeanDefinition(String var1) throws NoSuchBeanDefinitionException {
this.beanDefinitionMap.remove(var1);
}
@Override
public BeanDefinition getBeanDefinition(String var1) throws NoSuchBeanDefinitionException {
return this.beanDefinitionMap.get(var1);
}
@Override
public boolean containsBeanDefinition(String var1) {
return this.beanDefinitionMap.containsKey(var1);
}
@Override
public String[] getBeanDefinitionNames() {
return beanDefinitionMap.keySet().toArray(new String[0]);
}
@Override
public int getBeanDefinitionCount() {
return beanDefinitionMap.size();
}
}
3 MutablePropertyValues
package com.example03;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Spliterator;
import java.util.function.Consumer;
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;
}
}
@Override
public Iterator iterator() {
return propertyValueList.iterator();
}
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 addPropertyValues(PropertyValue propertyValue){
for (int i = 0; i < propertyValueList.size(); i++) {
PropertyValue propertyValue1=propertyValueList.get(i);
if(propertyValue1.getName().equals(propertyValue.getName())){
propertyValueList.set(i,propertyValue);
return this;
}
}
propertyValueList.add(propertyValue);
return this;
}
public boolean contains(String propertyName){
return propertyValueList.contains(propertyName);
}
}
4 PropertyValue
package com.example03;
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;
}
}
5 BeanDefinition
package com.example03;
public class BeanDefinition {
private String id;
private String className;
private MutablePropertyValues mutablePropertyValues;
public BeanDefinition() {
this.mutablePropertyValues=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 getMutablePropertyValues() {
return mutablePropertyValues;
}
public void setMutablePropertyValues(MutablePropertyValues mutablePropertyValues) {
this.mutablePropertyValues = mutablePropertyValues;
}
@Override
public String toString() {
return "BeanDefinition{" +
"id='" + id + '\'' +
", className='" + className + '\'' +
", mutablePropertyValues=" + mutablePropertyValues +
'}';
}
}
6 BeanDefinitionReader
package com.example03.support;
public interface BeanDefinitionReader {
BeanDefinitionRegistry getRegistry();
void loadBeanDefinition(String configLocation) throws Exception;
}
7 XmlBeanDefinitionReader
package com.example03;
import com.example03.support.BeanDefinitionReader;
import com.example03.support.BeanDefinitionRegistry;
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 BeanDefinitionRegistry beanDefinitionRegistry;
public XmlBeanDefinitionReader() {
this.beanDefinitionRegistry=new SimpleBeanDefinitionRegistry();
}
@Override
public BeanDefinitionRegistry getRegistry() {
return beanDefinitionRegistry;
}
@Override
public void loadBeanDefinition(String configLocation) throws Exception {
SAXReader reader=new SAXReader();
InputStream resourceAsStream = XmlBeanDefinitionReader.class.getClassLoader().getResourceAsStream(configLocation);
Document read = reader.read(resourceAsStream);
Element rootElement = read.getRootElement();
parseElelment(rootElement);
}
public void parseElelment(Element rootElements){
List<Element> elements = rootElements.elements();
for (Element element : elements) {
String id=element.attributeValue("id");
String className=element.attributeValue("class");
BeanDefinition beanDefinition = new BeanDefinition();
beanDefinition.setId(id);
beanDefinition.setClassName(className);
MutablePropertyValues propertyValues = new MutablePropertyValues();
List<Element> elements1 = element.elements();
for (Element element1 : elements1) {
String name = element1.attributeValue("name");
String ref = element1.attributeValue("ref");
String value = element1.attributeValue("value");
PropertyValue propertyValue=new PropertyValue(name,ref,value);
propertyValues.addPropertyValues(propertyValue);
}
beanDefinition.setMutablePropertyValues(propertyValues);
System.out.println(beanDefinition);
System.out.println(beanDefinition);
beanDefinitionRegistry.registerBeanDefinition(id,beanDefinition);
}
}
public static void main(String[] args) throws Exception {
XmlBeanDefinitionReader xmlBeanDefinitionReader = new XmlBeanDefinitionReader();
xmlBeanDefinitionReader.loadBeanDefinition("config.xml");
}
}
8 ApplicationContext
package com.example03.support;
import com.example03.support.BeanFactory;
public interface ApplicationContext extends BeanFactory {
void refresh() throws Exception;
}
9 AbstractApplicationContext
package com.example03;
import com.example03.support.ApplicationContext;
import com.example03.support.BeanDefinitionReader;
import com.example03.support.BeanDefinitionRegistry;
import java.util.HashMap;
import java.util.Map;
public abstract class AbstractApplicationContext implements ApplicationContext {
protected BeanDefinitionReader beanDefinitionReader;
protected Map<String,Object> singletonObject=new HashMap<>();
protected String configLocation;
@Override
public void refresh() throws Exception {
beanDefinitionReader.loadBeanDefinition(configLocation);
finishBeanInitialization();
}
protected void finishBeanInitialization() throws Exception {
BeanDefinitionRegistry registry = beanDefinitionReader.getRegistry();
String[] beanDefinitionNames = registry.getBeanDefinitionNames();
for (String beanDefinitionName : beanDefinitionNames) {
getBean(beanDefinitionName);
}
};
}
10 ClassPathApplicationContext
package com.example03;
import com.example03.support.BeanDefinitionRegistry;
import com.example03.util.StringUtils;
import java.lang.reflect.Method;
public class ClassPathXmlApplicationContext extends AbstractApplicationContext{
public ClassPathXmlApplicationContext(String configLoaction) throws Exception {
this.configLocation=configLoaction;
this.beanDefinitionReader=new XmlBeanDefinitionReader();
this.refresh();
}
@Override
public Object getBean(String beanName) throws Exception {
Object obj = singletonObject.get(beanName);
if(obj != null){
return obj;
}
BeanDefinitionRegistry registry = beanDefinitionReader.getRegistry();
BeanDefinition beanDefinition = registry.getBeanDefinition(beanName);
String className = beanDefinition.getClassName();
Class clazz = Class.forName(className);
Object beanobj = clazz.newInstance();
MutablePropertyValues mutablePropertyValues = beanDefinition.getMutablePropertyValues();
for (PropertyValue mutablePropertyValue : mutablePropertyValues) {
String pname = mutablePropertyValue.getName();
String ref = mutablePropertyValue.getRef();
String value = mutablePropertyValue.getValue();
if(ref != null && !"".equals(ref)){
Object bean = getBean(ref);
String setterMethod = StringUtils.getSetterMethod(pname);
Method[] methods = clazz.getMethods();
for (Method method : methods) {
if(setterMethod.equals(method)){
method.invoke(beanobj,bean);
}
}
}
if (value != null && !"".equals(value)){
String setterMethod = StringUtils.getSetterMethod(value);
Method method = clazz.getMethod(setterMethod, String.class);
method.invoke(beanobj, value);
}
}
this.singletonObject.put(beanName,beanobj);
return beanobj;
}
@Override
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);
}
}
11 StringUtils
package com.example03.util;
public class StringUtils {
public StringUtils() {
}
public static String getSetterMethod(String fileName){
String methodName="set"+fileName.substring(0,1).toUpperCase()+fileName.substring(1);
return methodName;
}
}