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
- package com.soukenan.spring.factory;
-
- public interface Vehicle {
- public void run();
- }
Two classes that implement this interface
Automobile
- Package com. soukenan. spring. factory;
-
- Public class Car implements Vehicle {
-
- @ Override
- Public void run (){
- System. out. println ("I have four wheel drives. I can run very fast ,,,,,,,");
- }
-
- }
Train
- Package com. soukenan. spring. factory;
-
- Public class Train implements Vehicle {
-
- @ Override
- Public void run (){
- System. out. println ("I Have A Lot Of freelogs. I can run very fast ,,,,,,");
- }
-
- }
Spring configuration file
- <?xml version="1.0" encoding="UTF-8"?>
- <beans >
-
- <bean id="car" class="com.soukenan.spring.factory.Car"></bean>
- <bean id="train" class="com.soukenan.spring.factory.Train"></bean>
-
- </beans>
BeanFactory Interface
- package com.soukenan.spring.factory;
-
- public interface BeanFactory {
- public Object getBean(String id);
- }
ClassPathXmlApplicationContext class that implements this interface
- package com.soukenan.spring.factory;
-
- import java.io.IOException;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
-
- import org.jdom2.Document;
- import org.jdom2.Element;
- import org.jdom2.input.SAXBuilder;
- import org.jdom2.xpath.XPath;
-
- public class ClassPathXmlApplicationContext implements BeanFactory {
-
- public Map<String,Object> container = new HashMap<String,Object>();
-
-
- public ClassPathXmlApplicationContext(String path) throws Exception{
- SAXBuilder sb = new SAXBuilder();
- Document doc = sb.build(this.getClass().getClassLoader().getResourceAsStream(path));
- Element root = doc.getRootElement();
- List list = XPath.selectNodes(root, "/beans/bean");
- for (int i = 0; i < list.size(); i++) {
- Element bean = (Element) list.get(i);
- String id = bean.getAttributeValue("id");
- String className = bean.getAttributeValue("class");
- Object o = Class.forName(className).newInstance();
- container.put(id, o);
- }
- }
- @Override
- public Object getBean(String id) {
- return this.container.get(id);
- }
-
- }
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
- package com.soukenan.spring.factory;
-
- import java.io.IOException;
- import java.util.Properties;
-
- public class Main {
- public static void main(String[] args) throws IOException, InstantiationException, IllegalAccessException, Exception{
-
-
- BeanFactory bf = new ClassPathXmlApplicationContext("applicationContext.xml");
- Vehicle v = (Vehicle) bf.getBean("car");
- v.run();
- }
- }
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