接口测试从零开始系列6_简简单单Spring1

上一篇 / 下一篇  2011-12-14 18:52:14 / 个人分类:接口测试

一.            控制反转和容器的思想

 

1.      面向对象设计里,讲究“接口与实现分离”

//定义接口

publicinterfaceIPingJiaDaoForSpring {

   //插入一条记录

   publicbooleaninsert(PingJia pingJia);

  

}

   //接口实现

publicclassPingJiaDaoImplForSpringimplementsIPingJiaDaoForSpring {

       @Override

       publicbooleaninsert(PingJia pingJia) {

//实现insert的具体方法

}

 

2.      在代码当中调用

publicclassStoreServiceForSpring {

   //在此处写死了实现是PingJiaDaoImplForSpring()

   privateIPingJiaDaoForSpringpingJiaDao=newPingJiaDaoImplForSpring();

   

   publicbooleanaddPingJia(PingJia pingJia) {

      returnpingJiaDao.insert(pingJia);

    }

}

3.      不同组织有不同的实现方式

privateIPingJiaDaoForSpringpingJiaDao=new其他实现();

问题:需要维护多个StoreServiceForSpring,每个各自创建和管理自己的依赖,对象之间的耦合性很高。

 

4.      使用一个容器来管理这些组件

 

publicclassContainer {

   publicstaticContainerinstance;

   privateMap<String, Object>components;

 

   publicContainer() {

      components=newHashMap<String, Object>();

      instance=this;

       IPingJiaDaoForSpring pinjiaDao =newPingJiaDaoImplForSpring();

      components.put("pinjiaDao", pinjiaDao);

 

       IStoreServiceForSpring storeService =newStoreServiceForSpring();

      components.put("storeService", storeService);

    }

 

   publicObject getComponent(String id) {

      returncomponents.get(id);

    }

}

在组件中使用容器生成组件:privateIPingJiaDaoForSpringpingJiaDao= (IPingJiaDaoForSpring) Container.instance

        .getComponent("pinjiaDao");

 

5.      使用容器后,可以用容器来生成需要的组件:

publicclassMain {

       publicstaticvoidmain(String args[]) {

           IStoreServiceForSpring storeService = (IStoreServiceForSpring) Container.instance

              .getComponent("storeservice");

           PingJia pingJia =newPingJia();

           pingJia.setPingJiaId("123456");

           storeService.addPingJia(pingJia);

    }

}

6.      使用服务定位器查找组件(主动查找)

publicclassServiceLocator {

       publicstaticContainercontainer= Container.instance;

 

       publicstaticIPingJiaDaoForSpring getPingJiaDao() {

          return(IPingJiaDaoForSpring)container.getComponent("pinjiaDao");

    }

}

  在组件中使用查找器:privateIPingJiaDaoForSpringpingJiaDao= ServiceLocator.getPingJiaDao();

 

7.      应用控制反转和依赖注入

在组件中使用查找器查找组件时,组件依旧需要知道如何查找资源,然后向容器发起请求查找资源,容器返回资源,

使用控制反转原则:容器主动将资源推送到所管理的组件中,组件只是选择一种合适的方式接受资源。

                  publicContainer() {

//components = new HashMap<String, Object>();

//instance = this;

           IPingJiaDaoForSpring pingJiaDao =newPingJiaDaoImplForSpring();

          components.put("pinjiaDao", pingJiaDao);

 

          StoreServiceForSpringstoreService =newStoreServiceForSpring();

           StoreServiceForSpring storeService1 =newStoreServiceForSpring(pingJiaDao);

           storeService.setPingJiaDao(pingJiaDao);//设置资源

          components.put("storeService", storeService);

          components.put("storeService1", storeService1);

      

    }

      依赖注入的三种方式:接口注入,setter注入,构造器注入

8.      使用配置文件来配置容器

publicContainer() {

      components=newHashMap<String, Object>();

   // instance = this;

    Properties properties =newProperties();

   try{

        properties.load(newFileInputStream("components.properties"));

       for(Map.Entryentry : properties.entrySet()) {

           String key = (String) entry.getKey();

           String value = (String) entry.getValue();

           processEntry(key, value);

        }

    }catch(Exception e) {

        e.printStackTrace();

    }

 

    IPingJiaDaoForSpring pingJiaDao =newPingJiaDaoImplForSpring();

   components.put("pinjiaDao", pingJiaDao);

 

    StoreServiceForSpring storeService =newStoreServiceForSpring();

    StoreServiceForSpring storeService1 =newStoreServiceForSpring(

           pingJiaDao);

    storeService.setPingJiaDao(pingJiaDao);

   components.put("storeService", storeService);

   components.put("storeService1", storeService1);

}

 

查看下篇:

 


TAG: Spring spring 接口测试 控制反转

 

评分:0

我来说两句

显示全部

:loveliness: :handshake :victory: :funk: :time: :kiss: :call: :hug: :lol :'( :Q :L ;P :$ :P :o :@ :D :( :)

Open Toolbar