2018-09-26 14:47:23 1242瀏覽
今天扣丁學(xué)堂Java培訓(xùn)老師給大家介紹一下關(guān)于SpringIoc原理的詳解,首先Ioc是InversionofControl。翻譯過來就是控制反轉(zhuǎn),意思是對(duì)象之間的關(guān)系不再由傳統(tǒng)的程序來控制,而是由spring容器來統(tǒng)一控制這些對(duì)象創(chuàng)建、協(xié)調(diào)、銷毀,而對(duì)象只需要完成業(yè)務(wù)邏輯即可。
public static void main(String[] args) { ApplicationContext context = new FileSystemXmlApplicationContext( "applicationContext.xml"); Animal animal = (Animal) context.getBean("animal"); animal.say(); }
<bean id="animal" class="phz.springframework.test.Cat"> <property name="name" value="kitty" /> </bean>
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 interface Animal { public void say(); }
/* Bean Id */ private String id; /* Bean Class */ private String type; /* Bean Property */ private Map<String, Object> properties = new HashMap<String, Object>();
<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>
(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); }
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 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); } }
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); }
以上就是關(guān)于扣丁學(xué)堂Java開發(fā)培訓(xùn)之spring實(shí)現(xiàn)Ioc原理的詳細(xì)介紹,希望對(duì)同學(xué)們學(xué)習(xí)Java開發(fā)有所幫助??鄱W(xué)堂Java在線直播體系課程和大量的Java視頻教程供學(xué)員觀看學(xué)習(xí),想要學(xué)好Java開發(fā)技術(shù)的小伙伴快快行動(dòng)吧??鄱W(xué)堂Java技術(shù)交流群:670348138。
【關(guān)注微信公眾號(hào)獲取更多學(xué)習(xí)資料】
查看更多關(guān)于“Java開發(fā)資訊”的相關(guān)文章>>