Spring dependency injection Principle Analysis, spring Injection Analysis

Source: Internet
Author: User

Spring dependency injection Principle Analysis, spring Injection Analysis

PropertyDefinition. java

1 package junit. test; 2 3 public class PropertyDefinition {4 private String name; 5 private String ref; 6 7 public PropertyDefinition (String name, String ref) {8 this. name = name; 9 this. ref = ref; 10} 11 12 public String getName () {13 return name; 14} 15 public void setName (String name) {16 this. name = name; 17} 18 public String getRef () {19 return ref; 20} 21 public void setRef (String ref) {22 this. ref = ref; 23} 24 25}PropertyDefinition

BeanDefinition. java

1 package junit. test; 2 3 import java. util. arrayList; 4 import java. util. list; 5 6 public class BeanDefinition {7 private String id; 8 private String className; 9 private List <PropertyDefinition> propertys = new ArrayList <PropertyDefinition> (); 10 11 public BeanDefinition (String id, String className) {12 this. id = id; 13 this. className = className; 14} 15 public String getId () {16 return id; 17} 18 public void setId (String id) {19 this. id = id; 20} 21 public String getClassName () {22 return className; 23} 24 public void setClassName (String className) {25 this. className = className; 26} 27 public List <PropertyDefinition> getPropertys () {28 return propertys; 29} 30 public void setPropertys (List <PropertyDefinition> propertys) {31 this. propertys = propertys; 32} 33 34}BeanDefinition

ItcastClassPathXMLApplicationContext. java

Package junit. test; import java. beans. introspectionException; import java. beans. introspector; import java. beans. propertyDescriptor; import java. lang. reflect. method; import java.net. URL; import java. util. arrayList; import java. util. hashMap; import java. util. hashSet; import java. util. list; import java. util. map; import org. dom4j. document; import org. dom4j. element; import org. dom4j. XPath; import org. dom4j. io. SAXRead Er;/*** Chuan Zhi Chuan ke edition container **/public class a {private List <BeanDefinition> beanDefines = new ArrayList <BeanDefinition> (); private Map <String, object> sigletons = new HashMap <String, Object> (); public ItcastClassPathXMLApplicationContext (String filename) {this. readXML (filename); this. instanceBeans (); this. injectObject ();}/*** is the bean Object Property injection value */private void injectObject () {for (BeanDefinition beanDefiniti On: beanDefines) {Object bean = sigletons. get (beanDefinition. getId (); if (bean! = Null) {try {PropertyDescriptor [] ps = Introspector. getBeanInfo (bean. getClass ()). getPropertyDescriptors (); for (PropertyDefinition propertyDefinition: beanDefinition. getPropertys () {for (PropertyDescriptor properdesc: ps) {if (propertyDefinition. getName (). equals (properdesc. getName () {Method setter = properdesc. getWriteMethod (); // setter method for obtaining attributes, private if (setter! = Null) {Object value = sigletons. get (propertyDefinition. getRef (); setter. setAccessible (true); setter. invoke (bean, value); // inject the referenced object into the attribute} break ;}}} catch (Exception e) {}}}/*** instantiate bean */private void instanceBeans () {for (BeanDefinition beanDefinition: beanDefines) {try {if (beanDefinition. getClassName ()! = Null &&! "". Equals (beanDefinition. getClassName (). trim () sigletons. put (beanDefinition. getId (), Class. forName (beanDefinition. getClassName ()). newInstance ();} catch (Exception e) {e. printStackTrace () ;}}/ *** read xml configuration file * @ param filename */private void readXML (String filename) {SAXReader saxReader = new SAXReader (); document document = null; try {URL xmlpath = this. getClass (). getClassLoader (). getResource (filename); document = saxReader. read (xmlpath); Map <String, String> nsMap = new HashMap <String, String> (); nsMap. put ("ns", "http://www.springframework.org/schema/beans"); // Add the namespace XPath xsub = document. createXPath ("// ns: beans/ns: bean"); // create the beans/bean query path xsub. setNamespaceURIs (nsMap); // set the namespace List <Element> beans = xsub. selectNodes (document); // obtain all bean nodes under the document for (Element element: beans) {String id = element. attributeValue ("id"); // obtain the id attribute value String clazz = element. attributeValue ("class"); // get the class attribute value BeanDefinition beanDefine = new BeanDefinition (id, clazz); XPath propertysub = element. createXPath ("ns: property"); propertysub. setNamespaceURIs (nsMap); // set the namespace List <Element> propertys = propertysub. selectNodes (element); for (Element property: propertys) {String propertyName = property. attributeValue ("name"); String propertyref = property. attributeValue ("ref"); PropertyDefinition propertyDefinition = new PropertyDefinition (propertyName, propertyref); beanDefine. getPropertys (). add (propertyDefinition);} beanDefines. add (beanDefine) ;}} catch (Exception e) {e. printStackTrace () ;}}/*** get bean instance * @ param beanName * @ return */public Object getBean (String beanName) {return this. sigletons. get (beanName );}}ItcastClassPathXMLApplicationContext

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.