ClassPathXmlApplicationContext, classpath

Source: Internet
Author: User

ClassPathXmlApplicationContext, classpath

 

How Spring reads the configuration file beans. xml:

Public class ClassPathXmlApplicationContext implements BeanFactory {private Map <String, Object> beans = new HashMap <String, Object> (); // ClassPathXmlApplicationContext (String xml) public ClassPathXmlApplicationContext () throws Exception {SAXBuilder sb = new SAXBuilder (); Document doc = sb. build (this. getClass (). getClassLoader (). getResourceAsStream ("beans. xml "); Element root = doc. getRootElement (); List list = root. getChildren ("bean"); for (int I = 0; I <list. size (); I ++) {Element element = (Element) list. get (I); String id = element. getAttributeValue ("id"); String clazz = element. getAttributeValue ("class"); // use reflection to generate Object o = Class. forName (clazz ). newInstance (); System. out. println (id); System. out. println (clazz); beans. put (id, o) ;}@ Override public Object getBean (String id) {return beans. get (id );}

<Bean id = "" class = "">

An id corresponds to a class, which is placed in the hashMap set and used as the primary key for query.

for(Element propertyElement : (List<Element>)element.getChildren("property")){                String name = propertyElement.getAttributeValue("name");                String beanInstance = propertyElement.getAttributeValue("bean");                //beanInstance ---> id--->haved bean new                Object beanObject = beans.get(beanInstance);                String methodName = "set" + name.substring(0,1).toUpperCase()+name.substring(1);                System.out.println("method Name:"+methodName);                Method m = o.getClass().getMethod(methodName, beanObject.getClass().getInterfaces()[0]);                //two differrent object ,car human                m.invoke(o, beanObject);            }

 

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.