思路:
1. 定义 Bean ⼯⼚接⼝,提供获取 bean ⽅法
2. 定义 Bean ⼯⼚接⼝实现类,解析配置⽂件,实例化Bean对象
3. 实现获取 Bean ⽅法
- package org.example.spring;
-
- /**
- * Bean属性对象:用来封装Spring配置文件中bean标签的id和class属性值
- */
- public class MyBean {
- private String id; //存放bean标签的id值
- private String clazz; //存放bean标签的class属性值
-
- 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;
- }
-
- public void setClazz(String clazz) {
- this.clazz = clazz;
- }
- }
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0modelVersion>
-
- <groupId>org.examplegroupId>
- <artifactId>spring02artifactId>
- <version>1.0-SNAPSHOTversion>
- <packaging>jarpackaging>
-
- <name>spring02name>
- <url>http://maven.apache.orgurl>
-
- <properties>
- <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
-
- properties>
-
- <dependencies>
- <dependency>
- <groupId>junitgroupId>
- <artifactId>junitartifactId>
- <version>4.12version>
- <scope>testscope>
- dependency>
-
-
- <dependency>
- <groupId>dom4jgroupId>
- <artifactId>dom4jartifactId>
- <version>1.6.1version>
- dependency>
-
-
- <dependency>
- <groupId>jaxengroupId>
- <artifactId>jaxenartifactId>
- <version>1.1.6version>
- dependency>
- dependencies>
- project>
- "1.0" encoding="utf-8" ?>
- <beans>
- <bean id="userDao" class="org.example.service.UserDao">bean>
- <bean id="userService" class="org.example.service.UserService">bean>
- beans>
- package org.example.spring;
-
- /**
- * Bean工厂接口的定义
- */
- public interface MyFactory {
- //根据id属性值获取bean对象
- public Object getBean(String id);
- }
- package org.example.spring;
-
- 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;
-
- /**
- * 模拟Spring的实现
- * 1.通过有参构造方法获得对应的配置文件
- * 2.使用dom4j解析配置文件(spring配置文件),得到一个List集合(这个List集合存放了一系列bean标签的id和class属性值)
- * 3.通过反射机制创建对应的实例化对象并存入到一个Map集合中(遍历List集合,通过获取对应的class属性值,利用反射机制实例化对象)
- * 4.根据id值获取bean对象
- */
- public class MyClassPathXmlApplicationContext implements MyFactory{
- //存放从spring配置文件中获取到的bean标签的信息
- private List
beanList; - //使用Map集合存放实例化好的bean对象
- private Map
beanMap = new HashMap<>(); -
- //1.通过有参构造方法获得对应的配置文件
- public MyClassPathXmlApplicationContext(String fileName) {
- //2.使用dom4j解析配置文件(spring配置文件),然后得到一个List集合(这个List集合存放了一系列bean标签的id和class属性值)
- this.parseXml(fileName);
- //3.通过反射机制创建对应的实例化对象并存入到一个Map集合中
- this.instanceBean();
- }
-
- /**
- * 通过反射机制创建对应的实例化对象并存入到一个Map集合中
- */
- private void instanceBean() {
- //1.判断对象集合是否为空,如果不为空则遍历集合,获取对应的id和class属性
- if (beanList != null && beanList.size()>0){
- for (MyBean bean:beanList){
- String id = bean.getId();
- String clazz = bean.getClazz();
- try {
- //2.通过反射机制根据类的全路径名实例化对象
- Object object = Class.forName(clazz).newInstance();
- //3.将对应的id和实例化好的bean对象放入Map集合中
- beanMap.put(id,object);
- }catch (Exception e){
- e.printStackTrace();
- }
- }
- }
- }
-
- /**
- * 使用dom4j解析spring配置文件,得到List集合
- * @param fileName
- */
- private void parseXml(String fileName) {
- //1.获取解析器
- SAXReader saxReader = new SAXReader();
- //2.获取配置文件的URL
- URL url = this.getClass().getClassLoader().getResource(fileName);
- try{
- //3.通过解析器解析spring配置文件
- Document document = saxReader.read(url);
- //4.通过xpath语法解析,获取beans标签下的所有bean标签
- XPath xPath = document.createXPath("beans/bean");
- //5.通过指定的解析语法解析文档对象,返回元素集合
- List
elementList = xPath.selectNodes(document); - //6.判断元素集合是否为空
- if (elementList != null && elementList.size()>0){
- //实例化List集合
- beanList = new ArrayList<>();
- //7.如果元素集合不为空,就遍历集合
- for (Element e:elementList){
- //8.获取bean标签元素的属性(id和class属性值)
- String id = e.attributeValue("id");
- String clazz = e.attributeValue("class");
- //9.获取MyBean对象,将id和class属性值设置到对象中,再将对象添加到List集合中
- MyBean myBean = new MyBean(id,clazz);
- beanList.add(myBean);
- }
- }
- }catch (Exception e){
- e.printStackTrace();
- }
- }
-
- /**
- * 通过id获取对应bean对象
- * @param id
- * @return
- */
- public Object getBean(String id) {
- Object object = beanMap.get(id);
- return object;
- }
- }
先定义用于测试的两个类:
- package org.example.dao;
-
- public class UserDao {
- public void test(){
- System.out.println("UserDao Test...");
- }
- }
-
-
- package org.example.service;
-
- public class UserService {
- public void test(){
- System.out.println("UserService Test...");
- }
- }
编写测试程序:
- package org.example;
-
- import org.example.dao.UserDao;
- import org.example.service.UserService;
- import org.example.spring.MyClassPathXmlApplicationContext;
- import org.example.spring.MyFactory;
-
- /**
- * Hello world!
- *
- */
- public class App
- {
- public static void main( String[] args )
- {
- //得到工厂的实现对象
- MyFactory factory = new MyClassPathXmlApplicationContext("spring.xml");
- //得到对应的实例化对象
- UserDao userDao = (UserDao)factory.getBean("userDao");
- userDao.test();
-
- UserService userService = (UserService)factory.getBean("userService");
- userService.test();
- }
- }