Bean实例化过程
- 工厂类通过构造器行参传递要解析的xml配置文件
- 解析配置文件,得到对应Bean标签的id和class属性值,生成MyBean对象集合
- 实例化Bean对象,便利MyBean对象集合,通过class全类名反射实例,将实例保存到hashmap中
- getBean方法,通过id获取从hashmap中获取实例化的Bean对象
引入依赖包
- dom4j 用于解析xml文件
- jaxen 用于xpath解析
pom.xml1 2 3 4 5 6 7 8 9 10 11
| <dependency> <groupId>dom4j</groupId> <artifactId>dom4j</artifactId> <version>1.6.1</version> </dependency>
<dependency> <groupId>jaxen</groupId> <artifactId>jaxen</artifactId> <version>1.2.0</version> </dependency>
|
创建dao类和service类,用来做测试
1 2 3 4 5 6 7
| package org.example.dao;
public class UserDao { public void test(){ System.out.println("UserDao ..."); } }
|
1 2 3 4 5 6 7
| package org.example.service;
public class UserService { public void test(){ System.out.println("UserService ..."); } }
|
创建bean配置文件
src/main
目录创建resources
目录,并将 resources
目录标记为资源目录,resources
目录下创建 Bean 配置文件 spring.xml
spring.xml1 2 3 4 5
| <?xml version="1.0" encoding="UTF-8" ?> <beans> <bean id="userDao" class="org.example.dao.UserDao" /> <bean id="userService" class="org.example.service.UserService" /> </beans>
|
创建工厂接口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| package org.example.factory;
public interface MyFactory {
public Object getBean(String id); }
|
创建 Bean 对象类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| package org.example.factory;
public class MyBean { private String id; private String clazz;
public MyBean() { }
public MyBean(String id, String clazz) { this.id = id; this.clazz = clazz; }
public String getId() { return id; }
public void setId(String id) { this.id = id; }
public String getClazz() { return clazz; } }
|
创建接口工厂实现类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
| package org.example.factory;
import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.XPath; import org.dom4j.io.SAXReader;
import java.net.URL; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map;
public class MyClassPathXmlApplication implements MyFactory{ private Map<String, Object> beanMap = new HashMap<String, Object>(); private List<MyBean> beanList;
public MyClassPathXmlApplication(String fileName) { parseXml(fileName);
instanceBean(); }
private void parseXml(String fileName){ SAXReader reader = new SAXReader(); URL url = this.getClass().getClassLoader().getResource(fileName); try { Document document = reader.read(url); XPath xpath = document.createXPath("beans/bean"); List<Element> elementList = xpath.selectNodes(document); if(elementList != null && elementList.size() > 0){ this.beanList = new ArrayList(); for(Element element : elementList){ String id = element.attributeValue("id"); String clazz = element.attributeValue("class"); MyBean myBean = new MyBean(id, clazz); beanList.add(myBean); } } }catch(Exception e){ e.printStackTrace(); } }
private void instanceBean(){ if(beanList != null && beanList.size() > 0){ try { for(MyBean myBean : beanList){ String id = myBean.getId(); String clazz = myBean.getClazz();
Object obj = Class.forName(clazz).newInstance();
beanMap.put(id, obj); } } catch (Exception e){ e.printStackTrace(); } } }
@Override public Object getBean(String id) { return beanMap.get(id); } }
|
测试运行
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| package org.example;
import org.example.dao.UserDao; import org.example.factory.MyClassPathXmlApplication; import org.example.factory.MyFactory; import org.example.service.UserService;
public class Starter { public static void main(String[] args) { MyFactory factory = new MyClassPathXmlApplication("spring.xml"); UserService userService = (UserService)factory.getBean("userService"); userService.test();
UserDao userDao = (UserDao) factory.getBean("userDao"); userDao.test(); } }
|