【Spring框架01】手动模拟IOC容器

【Spring框架】手动模拟IOC容器

一、思维导图

在这里插入图片描述

pom 依赖

<!--      添加dom4j依赖-->
      <!-- https://mvnrepository.com/artifact/org.dom4j/dom4j -->
      <dependency>
        <groupId>org.dom4j</groupId>
        <artifactId>dom4j</artifactId>
        <version>2.1.1</version>
      </dependency>

      <!-- https://mvnrepository.com/artifact/jaxen/jaxen -->
      <dependency>
        <groupId>jaxen</groupId>
        <artifactId>jaxen</artifactId>
        <version>1.1.6</version>
      </dependency>

xml配置

<?xml version="1.0" encoding="UTF-8"?>
<!--使用的命名空间-->
<beans>
    <!-- configuration details go here -->
    <bean id="hellowService" class="com.lcySpring.service.HelloService"></bean>
    <bean id="AccountService" class="com.lcySpring.service.AccountService"></bean>
<!--    id起别名-->
</beans>

这里最好还是写全生成小绿叶子好一点,不然后面test就报错!!!

<?xml version="1.0" encoding="UTF-8"?>
<!--使用的命名空间-->
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!-- configuration details go here -->
    <!-- configuration details go here -->
    <bean id="helloService" class="com.lcySpring.service.HelloService"></bean>
    <bean id="AccountService" class="com.lcySpring.service.AccountService"></bean>
<!--    id起别名-->
</beans>

测试类

public class HelloService {
    public void test(){
        System.out.println("HelloService.......successful!!!!!");
    }

}

模拟ioc

1…application.java 接口

public interface ApplicationContext {
    public Object getBean(String id);
}

2.接口实现类

public class lcyApplicationContext implements ApplicationContext {
    private String xmlPath;
    private List<LcyBean> lcyBeans = new ArrayList<>();//存放解析结果
    private Map<String,Object> beanMap = new HashMap<>();//存放反射生成的bean
    public lcyApplicationContext(String xmlPath){
        this.xmlPath=xmlPath;
        readXml();
        initBean();
    }

    private void initBean() {
        //开始反射生成bean
        if(lcyBeans.size()>0){
            for(LcyBean lcyBean:lcyBeans){
                String id=lcyBean.getId();
                String clazz=lcyBean.getClazz();
                try {
                    beanMap.put(id,Class.forName(clazz).newInstance());
                } catch (InstantiationException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private void readXml() {
        if(null!=xmlPath&&""!=xmlPath){
            //创建解析对象
            SAXReader reader = new SAXReader();
            try {
                URL url = this.getClass().getClassLoader().getResource(xmlPath);
                Document document = reader.read(url);
                List<Node> list=document.selectNodes("//beans/bean");
                for(Iterator<Node> iter =list.iterator();iter.hasNext();)
                {
                    Element e = (Element)iter.next();
                    System.out.println(e.attributeValue("id") + e.attributeValue("class"));
                    //存入集合
                    LcyBean lcyBean= new LcyBean();
                    lcyBean.setId(e.attributeValue("id"));
                    lcyBean.setClazz(e.attributeValue("class"));
                    lcyBeans.add(lcyBean);
                }

            } catch (DocumentException e) {
                e.printStackTrace();
                System.err.println("文件找不到");
            }
        }
        else{
            System.err.println("文件为空");
        }
    }

    @Override
    public Object getBean(String id) {
        return beanMap.get(id);
    }
}

3.bean 存放xml解析的信息

public class LcyBean {
    private String id;

    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;
    }

    private String clazz;

}

单元测试

public class lcyApplicationContextTest {
    @Test
    public void test01(){
       ApplicationContext context = new lcyApplicationContext("spring.xml");
       context.getBean("hellowService");
       HelloService helloService =(HelloService) context.getBean("hellowService");
       helloService.test();

        context.getBean("AccountService");
        AccountService accountService = (AccountService) context.getBean("AccountService");
        accountService.test();
    }
}

附录(关于List 和关于List)

当前版本不再支持直接Element然后遍历取值
需要一个迭代器

List<Node> list=document.selectNodes("//beans/bean");
   for(Iterator<Node> iter =list.iterator();iter.hasNext();)
    {
          Element e = (Element)iter.next();
          System.out.println(e.attributeValue("id") + e.attributeValue("class"));
     }
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值