這篇文章主要講解了“Spring運(yùn)作機(jī)制是什么”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“Spring運(yùn)作機(jī)制是什么”吧!

在巴馬等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場(chǎng)前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都網(wǎng)站設(shè)計(jì)、做網(wǎng)站 網(wǎng)站設(shè)計(jì)制作按需定制開發(fā),公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),成都品牌網(wǎng)站建設(shè),全網(wǎng)營(yíng)銷推廣,外貿(mào)營(yíng)銷網(wǎng)站建設(shè),巴馬網(wǎng)站建設(shè)費(fèi)用合理。
Java代碼
public static void main(String[] args) { ApplicationContext context = new FileSystemXmlApplicationContext( "applicationContext.xml"); Animal animal = (Animal) context.getBean("animal"); animal.say(); } public static void main(String[] args) { ApplicationContext context = new FileSystemXmlApplicationContext( "applicationContext.xml"); Animal animal = (Animal) context.getBean("animal"); animal.say(); }這段代碼你一定很熟悉吧,不過(guò)還是讓我們分析一下它吧,首先是applicationContext.xml
Java代碼
<bean id="animal" class="phz.springframework.test.Cat"> <property name="name"> <value>kitty</value> </property> </bean> <bean id="animal" class="phz.springframework.test.Cat"> <property name="name"> <value>kitty</value> </property> </bean>
他有一個(gè)類phz.springframework.test.Cat =
Java代碼
public class Cat implements Animal { private String name; public void say() { System.out.println("I am " + name + "!"); } public void setName(String name) { this.name = name; } } public class Cat implements Animal { private String name; public void say() { System.out.println("I am " + name + "!"); } public void setName(String name) { this.name = name; } }實(shí)現(xiàn)了phz.springframework.test.Animal接口
Java代碼
public interface Animal { public void say(); } public interface Animal { public void say(); }很明顯上面的代碼輸出I am kitty!
那么到底Spring是如何做到的呢?
接下來(lái)就讓我們自己寫個(gè)Spring 來(lái)看看Spring運(yùn)作機(jī)制吧!
首先,我們定義一個(gè)Bean類,這個(gè)類用來(lái)存放一個(gè)Bean擁有的屬性
Java代碼
/* Bean Id */ private String id; /* Bean Class */ private String type; /* Bean Property */ 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>();
一個(gè)Bean包括id,type,和Properties。
接下來(lái)Spring 就開始加載我們的配置文件了,將我們配置的信息保存在一個(gè)HashMap中,HashMap的key就是Bean 的 Id ,HasMap 的value是這個(gè)Bean,只有這樣我們才能通過(guò)context.getBean("animal")這個(gè)方法獲得Animal這個(gè)類。我們都知道Spirng可以注入基本類型,而且可以注入像List,Map這樣的類型,接下來(lái)就讓我們以Map為例看看Spring是怎么保存的吧
Map配置可以像下面的
Java代碼
<bean id="test" class="Test"> <property name="testMap"> <map> <entry key="a"> <value>1</value> </entry> <entry key="b"> <value>2</value> </entry> </map> </property> </bean> <bean id="test" class="Test"> <property name="testMap"> <map> <entry key="a"> <value>1</value> </entry> <entry key="b"> <value>2</value> </entry> </map> </property> </bean>
Spring運(yùn)作機(jī)制中是怎樣保存上面的配置呢?,代碼如下:
Java代碼
if (beanProperty.element("map") != null) { Map<String, Object> propertiesMap = new HashMap<String, Object>(); Element propertiesListMap = (Element) beanProperty .elements().get(0); Iterator<?> propertiesIterator = propertiesListMap .elements().iterator(); while (propertiesIterator.hasNext()) { Element vet = (Element) propertiesIterator.next(); if (vet.getName().equals("entry")) { String key = vet.attributeValue("key"); Iterator<?> valuesIterator = vet.elements() .iterator(); while (valuesIterator.hasNext()) { Element value = (Element) valuesIterator.next(); if (value.getName().equals("value")) { propertiesMap.put(key, value.getText()); } if (value.getName().equals("ref")) { propertiesMap.put(key, new String[] { value .attributeValue("bean") }); } } } } bean.getProperties().put(name, propertiesMap); } if (beanProperty.element("map") != null) { Map<String, Object> propertiesMap = new HashMap<String, Object>(); Element propertiesListMap = (Element) beanProperty .elements().get(0); Iterator<?> propertiesIterator = propertiesListMap .elements().iterator(); while (propertiesIterator.hasNext()) { Element vet = (Element) propertiesIterator.next(); if (vet.getName().equals("entry")) { String key = vet.attributeValue("key"); Iterator<?> valuesIterator = vet.elements() .iterator(); while (valuesIterator.hasNext()) { Element value = (Element) valuesIterator.next(); if (value.getName().equals("value")) { propertiesMap.put(key, value.getText()); } if (value.getName().equals("ref")) { propertiesMap.put(key, new String[] { value .attributeValue("bean") }); } } } } bean.getProperties().put(name, propertiesMap); }接下來(lái)就進(jìn)入最核心部分了,讓我們看看Spring 到底是怎么依賴注入的吧,其實(shí)依賴注入的思想也很簡(jiǎn)單,它是通過(guò)反射機(jī)制實(shí)現(xiàn)的,在實(shí)例化一個(gè)類時(shí),它通過(guò)反射調(diào)用類中set方法將事先保存在HashMap中的類屬性注入到類中。讓我們看看具體它是怎么做的吧。
首先實(shí)例化一個(gè)類,像這樣
Java代碼
public static Object newInstance(String className) { Class<?> cls = null; Object obj = null; try { cls = Class.forName(className); obj = cls.newInstance(); } catch (ClassNotFoundException e) { throw new RuntimeException(e); } catch (InstantiationException e) { throw new RuntimeException(e); } catch (IllegalAccessException e) { throw new RuntimeException(e); } return obj; } public static Object newInstance(String className) { Class<?> cls = null; Object obj = null; try { cls = Class.forName(className); obj = cls.newInstance(); } catch (ClassNotFoundException e) { throw new RuntimeException(e); } catch (InstantiationException e) { throw new RuntimeException(e); } catch (IllegalAccessException e) { throw new RuntimeException(e); } return obj; }接著它將這個(gè)類的依賴注入進(jìn)去,像這樣
Java代碼
public static void setProperty(Object obj, String name, String value) { Class<? extends Object> clazz = obj.getClass(); try { String methodName = returnSetMthodName(name); Method[] ms = clazz.getMethods(); for (Method m : ms) { if (m.getName().equals(methodName)) { if (m.getParameterTypes().length == 1) { Class<?> clazzParameterType = m.getParameterTypes()[0]; setFieldValue(clazzParameterType.getName(), 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); } } public static void setProperty(Object obj, String name, String value) { Class<? extends Object> clazz = obj.getClass(); try { String methodName = returnSetMthodName(name); Method[] ms = clazz.getMethods(); for (Method m : ms) { if (m.getName().equals(methodName)) { if (m.getParameterTypes().length == 1) { Class<?> clazzParameterType = m.getParameterTypes()[0]; setFieldValue(clazzParameterType.getName(), 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); } }***它將這個(gè)類的實(shí)例返回給我們,我們就可以用了。我們還是以Map為例看看它是怎么做的,我寫的代碼里面是創(chuàng)建一個(gè)HashMap并把該HashMap注入到需要注入的類中,像這樣,
Java代碼
if (value instanceof Map) { Iterator<?> entryIterator = ((Map<?, ?>) value).entrySet() .iterator(); Map<String, Object> map = new HashMap<String, Object>(); while (entryIterator.hasNext()) { Entry<?, ?> entryMap = (Entry<?, ?>) entryIterator.next(); if (entryMap.getValue() instanceof String[]) { map.put((String) entryMap.getKey(), getBean(((String[]) entryMap.getValue())[0])); } } BeanProcesser.setProperty(obj, property, map); } if (value instanceof Map) { Iterator<?> entryIterator = ((Map<?, ?>) value).entrySet() .iterator(); Map<String, Object> map = new HashMap<String, Object>(); while (entryIterator.hasNext()) { Entry<?, ?> entryMap = (Entry<?, ?>) entryIterator.next(); if (entryMap.getValue() instanceof String[]) { map.put((String) entryMap.getKey(), getBean(((String[]) entryMap.getValue())[0])); } } BeanProcesser.setProperty(obj, property, map); }感謝各位的閱讀,以上就是“Spring運(yùn)作機(jī)制是什么”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)Spring運(yùn)作機(jī)制是什么這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
本文題目:Spring運(yùn)作機(jī)制是什么
網(wǎng)站鏈接:http://chinadenli.net/article10/goihgo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶體驗(yàn)、軟件開發(fā)、品牌網(wǎng)站建設(shè)、營(yíng)銷型網(wǎng)站建設(shè)、云服務(wù)器、電子商務(wù)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)