spring ioc原理(看完后大家可以自己写一个spring)

时间:2025-02-06 20:29:18
public static void main(String[] args) {
		ApplicationContext context = new FileSystemXmlApplicationContext(
				"");
		Animal animal = (Animal) ("animal");
		();
	}


这段代码你一定很熟悉吧,不过还是让我们分析一下它吧,首先是

Java代码
  1. <bean id="animal" class="">   
  2.         <property name="name" value="kitty" />   
  3.     </bean>  
<bean  class="">
		<property name="name" value="kitty" />
	</bean>


他有一个类

Java代码
  1. public class Cat implements Animal {   
  2.     private String name;   
  3.     public void say() {   
  4.         ("I am " + name + "!");   
  5.     }   
  6.     public void setName(String name) {   
  7.         this.name = name;   
  8.     }   
  9. }  
public class Cat implements Animal {
	private String name;
	public void say() {
		("I am " + name + "!");
	}
	public void setName(String name) {
		 = name;
	}
}


实现了接口

Java代码
  1. public interface Animal {   
  2.     public void say();   
  3. }  
public interface Animal {
	public void say();
}


很明显上面的代码输出I am kitty!

那么到底Spring是如何做到的呢?
接下来就让我们自己写个Spring 来看看Spring 到底是怎么运行的吧!

首先,我们定义一个Bean类,这个类用来存放一个Bean拥有的属性

Java代码
  1. /* Bean Id */  
  2.     private String id;   
  3.     /* Bean Class */  
  4.     private String type;   
  5.     /* Bean Property */  
  6.     private Map<String, Object> properties = new HashMap<String, Object>();  
/* Bean Id */
	private String id;
	/* Bean Class */
	private String type;
	/* Bean Property */
	private Map<String, Object> properties = new HashMap<String, Object>();


一个Bean包括id,type,和Properties。

接下来Spring 就开始加载我们的配置文件了,将我们配置的信息保存在一个HashMap中,HashMap的key就是Bean 的 Id ,HasMap 的value是这个Bean,只有这样我们才能通过("animal")这个方法获得Animal这个类。我们都知道Spirng可以注入基本类型,而且可以注入像List,Map这样的类型,接下来就让我们以Map为例看看Spring是怎么保存的吧

Map配置可以像下面的

Java代码
  1. <bean id="test" class="Test">   
  2.         <property name="testMap">   
  3.             <map>   
  4.                 <entry key="a">   
  5.                     <value>1</value>   
  6.                 </entry>   
  7.                 <entry key="b">   
  8.                     <value>2</value>   
  9.                 </entry>   
  10.             </map>   
  11.         </property>   
  12.     </bean>  
<bean  class="Test">
		<property name="testMap">
			<map>
				<entry key="a">
					<value>1</value>
				</entry>
				<entry key="b">
					<value>2</value>
				</entry>
			</map>
		</property>
	</bean>


Spring是怎样保存上面的配置呢?,代码如下:

Java代码
  1. if (("map") != null) {   
  2.                     Map<String, Object> propertiesMap = new HashMap<String, Object>();   
  3.                     Element propertiesListMap = (Element) beanProperty   
  4.                             .elements().get(0);   
  5.                     Iterator<?> propertiesIterator = propertiesListMap   
  6.                             .elements().iterator();   
  7.                     while (()) {   
  8.                         Element vet = (Element) ();   
  9.                         if (().equals("entry")) {   
  10.                             String key = ("key");   
  11.                             Iterator<?> valuesIterator = ()   
  12.                                     .iterator();   
  13.                             while (()) {   
  14.                                 Element value = (Element) ();   
  15.                                 if (().equals("value")) {   
  16.                                     (key, ());   
  17.                                 }   
  18.                                 if (().equals("ref")) {   
  19.                                     (key, new String[] { value   
  20.                                             .attributeValue("bean") });   
  21.                                 }   
  22.                             }   
  23.                         }   
  24.                     }   
  25.                     ().put(name, propertiesMap);   
  26.                 }  
if (("map") != null) {
					Map<String, Object> propertiesMap = new HashMap<String, Object>();
					Element propertiesListMap = (Element) beanProperty
							.elements().get(0);
					Iterator<?> propertiesIterator = propertiesListMap
							.elements().iterator();
					while (()) {
						Element vet = (Element) ();
						if (().equals("entry")) {
							String key = ("key");
							Iterator<?> valuesIterator = ()
									.iterator();
							while (()) {
								Element value = (Element) ();
								if (().equals("value")) {
									(key, ());
								}
								if (().equals("ref")) {
									(key, new String[] { value
											.attributeValue("bean") });
								}
							}
						}
					}
					().put(name, propertiesMap);
				}



接下来就进入最核心部分了,让我们看看Spring 到底是怎么依赖注入的吧,其实依赖注入的思想也很简单,它是通过反射机制实现的,在实例化一个类时,它通过反射调用类中set方法将事先保存在HashMap中的类属性注入到类中。让我们看看具体它是怎么做的吧。
首先实例化一个类,像这样

Java代码
  1. public static Object newInstance(String className) {   
  2.         Class<?> cls = null;   
  3.         Object obj = null;   
  4.         try {   
  5.             cls = (className);   
  6.             obj = ();   
  7.         } catch (ClassNotFoundException e) {   
  8.             throw new RuntimeException(e);   
  9.         } catch (InstantiationException e) {   
  10.             throw new RuntimeException(e);   
  11.         } catch (IllegalAccessException e) {   
  12.             throw new RuntimeException(e);   
  13.         }   
  14.         return obj;   
  15.     }  
public static Object newInstance(String className) {
		Class<?> cls = null;
		Object obj = null;
		try {
			cls = (className);
			obj = ();
		} catch (ClassNotFoundException e) {
			throw new RuntimeException(e);
		} catch (InstantiationException e) {
			throw new RuntimeException(e);
		} catch (IllegalAccessException e) {
			throw new RuntimeException(e);
		}
		return obj;
	}


接着它将这个类的依赖注入进去,像这样

Java代码
  1. public static void setProperty(Object obj, String name, String value) {   
  2.         Class<? extends Object> clazz = ();   
  3.         try {   
  4.             String methodName = returnSetMthodName(name);   
  5.             Method[] ms = ();   
  6.             for (Method m : ms) {   
  7.                 if (().equals(methodName)) {   
  8.                     if (().length == 1) {   
  9.                         Class<?> clazzParameterType = ()[0];   
  10.                         setFieldValue((), value, m,   
  11.                                 obj);   
  12.                         break;   
  13.                     }   
  14.                 }   
  15.             }   
  16.         } catch (SecurityException e) {   
  17.             throw new RuntimeException(e);   
  18.         } catch (IllegalArgumentException e) {   
  19.             throw new RuntimeException(e);   
  20.         } catch (IllegalAccessException e) {   
  21.             throw new RuntimeException(e);   
  22.         } catch (InvocationTargetException e) {   
  23.             throw new RuntimeException(e);   
  24.         }   
  25. }  
public static void setProperty(Object obj, String name, String value) {
		Class<? extends Object> clazz = ();
		try {
			String methodName = returnSetMthodName(name);
			Method[] ms = ();
			for (Method m : ms) {
				if (().equals(methodName)) {
					if (().length == 1) {
						Class<?> clazzParameterType = ()[0];
						setFieldValue((), value, m,
								obj);
						break;
					}
				}
			}
		} catch (SecurityException e) {
			throw new RuntimeException(e);
		} catch (IllegalArgumentException e) {
			throw new RuntimeException(e);
		} catch (IllegalAccessException e) {
			throw new RuntimeException(e);
		} catch (InvocationTargetException e) {
			throw new RuntimeException(e);
		}
}


最后它将这个类的实例返回给我们,我们就可以用了。我们还是以Map为例看看它是怎么做的,我写的代码里面是创建一个HashMap并把该HashMap注入到需要注入的类中,像这样,

Java代码
  1. if (value instanceof Map) {   
  2.                 Iterator<?> entryIterator = ((Map<?, ?>) value).entrySet()   
  3.                         .iterator();   
  4.                 Map<String, Object> map = new HashMap<String, Object>();   
  5.                 while (()) {   
  6.                     Entry<?, ?> entryMap = (Entry<?, ?>) ();   
  7.                     if (() instanceof String[]) {   
  8.                         ((String) (),   
  9.                                 getBean(((String[]) ())[0]));   
  10.                     }   
  11.                 }   
  12.                 (obj, property, map);   
  13.             }  
if (value instanceof Map) {
				Iterator<?> entryIterator = ((Map<?, ?>) value).entrySet()
						.iterator();
				Map<String, Object> map = new HashMap<String, Object>();
				while (()) {
					Entry<?, ?> entryMap = (Entry<?, ?>) ();
					if (() instanceof String[]) {
						((String) (),
								getBean(((String[]) ())[0]));
					}
				}
				(obj, property, map);
			}


好了,这样我们就可以用Spring 给我们创建的类了,是不是也不是很难啊?当然Spring能做到的远不止这些,这个示例程序仅仅提供了Spring最核心的依赖注入功能中的一部分。
本文参考了大量文章无法一一感谢,在这一起感谢,如果侵犯了你的版权深表歉意,很希望对大家有帮助!