3. Factory model-simulate spring bean Factory beanFactory

Source: Internet
Author: User

1. today, we write a project to simulate the bean Factory of spring. In spring, we can configure the class to applicationContext. in the xml file, you can then retrieve the objects of this class from beanFactoy. here we need to use xml parsing Technology in java. There are four commonly used parsing technologies, here we use jdom parsing. First, we need to download the jdom jar file at www.jdom.org on the jdom official website, introduce all the jar packages under lib and jdom. jar core jar package introduction is OK.

Run the code below.

Vehicle Interface

 
  1. package com.soukenan.spring.factory;  
  2.  
  3. public interface Vehicle {  
  4.     public void run();  
  5. }  

Two classes that implement this interface

Automobile

 
  1. Package com. soukenan. spring. factory;
  2.  
  3. Public class Car implements Vehicle {
  4.  
  5. @ Override
  6. Public void run (){
  7. System. out. println ("I have four wheel drives. I can run very fast ,,,,,,,");
  8. }
  9.  
  10. }

Train

 
  1. Package com. soukenan. spring. factory;
  2.  
  3. Public class Train implements Vehicle {
  4.  
  5. @ Override
  6. Public void run (){
  7. System. out. println ("I Have A Lot Of freelogs. I can run very fast ,,,,,,");
  8. }
  9.  
  10. }

Spring configuration file

 
  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <beans > 
  3.       
  4.     <bean id="car" class="com.soukenan.spring.factory.Car"></bean> 
  5.     <bean id="train" class="com.soukenan.spring.factory.Train"></bean> 
  6.       
  7. </beans> 

BeanFactory Interface

 
  1. package com.soukenan.spring.factory;  
  2.  
  3. public interface BeanFactory {  
  4.     public Object getBean(String id);  
  5. }  

ClassPathXmlApplicationContext class that implements this interface

 
  1. package com.soukenan.spring.factory;  
  2.  
  3. import java.io.IOException;  
  4. import java.util.HashMap;  
  5. import java.util.List;  
  6. import java.util.Map;  
  7.  
  8. import org.jdom2.Document;  
  9. import org.jdom2.Element;  
  10. import org.jdom2.input.SAXBuilder;  
  11. import org.jdom2.xpath.XPath;  
  12.  
  13. public class ClassPathXmlApplicationContext implements BeanFactory {  
  14.       
  15.     public Map<String,Object> container = new HashMap<String,Object>();  
  16.       
  17.       
  18.     public ClassPathXmlApplicationContext(String path) throws Exception{  
  19.                 SAXBuilder sb = new SAXBuilder();  
  20.                 Document doc = sb.build(this.getClass().getClassLoader().getResourceAsStream(path));  
  21.                 Element root = doc.getRootElement();  
  22.                 List list = XPath.selectNodes(root, "/beans/bean");  
  23.                 for (int i = 0; i < list.size(); i++) {   
  24.                     Element bean = (Element) list.get(i);  
  25.                     String id = bean.getAttributeValue("id");  
  26.                     String className = bean.getAttributeValue("class");  
  27.                     Object o = Class.forName(className).newInstance();  
  28.                     container.put(id, o);  
  29.                 }  
  30.     }  
  31.     @Override 
  32.     public Object getBean(String id) {  
  33.         return this.container.get(id);  
  34.     }  
  35.  
  36. }  

Directory of each file

650) this. width = 650; "border =" 0 "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/115GR933-0.jpg "/>

Test main class

 
  1. package com.soukenan.spring.factory;  
  2.  
  3. import java.io.IOException;  
  4. import java.util.Properties;  
  5.  
  6. public class Main {  
  7.     public static void main(String[] args) throws IOException, InstantiationException, IllegalAccessException, Exception{  
  8.  
  9.           
  10.         BeanFactory bf = new ClassPathXmlApplicationContext("applicationContext.xml");  
  11.         Vehicle v = (Vehicle) bf.getBean("car");  
  12.         v.run();  
  13.     }  
  14. }  

Check whether the usage is exactly the same as that of spring? Amazing.

If you have any questions, please reply to me!

 

This article is from the "Kenan_ITBlog" blog, please be sure to keep this source http://soukenan.blog.51cto.com/5130995/1062462

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.