IOC(Inverse of control):控制反转;其实就是一个装对象的容器,以前我们在controller中调用service中的方法,都要先new 一个service对象,这样不符合模式设计的六大法则中的依赖倒置原则,为了处理这个问题,可以把各层创建对象的工作让spring来完成,spring创建对象都把它放在ioc中

DI:依赖注入:其实与IOC是一回事,只是从不同的角度来看待问题的

实现IOC/DI的技术有:

          1.setter注入(最常用)

          2.构造方法注入(使用它时,要注意空构造器必须存在)

                            3.接口注入

          4.Annotation注入

模拟spring ioc

beans.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<beans>
  <!-- ioc 对象的容器 service dao controller 内的所有对象
    1:beans.xml写完
    2:解析beans.xml 生成配置文件中配置的各个对象
        了解新的解析工具
    3:完成对象间的属性注入
    4:初始化容器 完成 UserAction内的调用
  -->
  <bean id="userAction" class="com.cdsxt.action.UserAction" >
    <!-- name:对应action类内属性的值 ref:对应bean元素的id的值-->
    <property name="userDao" ref="userDao" />
  </bean>
  <bean id="userDao" class="com.cdsxt.dao.impl.UserDaoImpl" ></bean>
</beans>

Action:


public class UserAction {
  private UserDao userDao;
  public void add(){
    System.out.println("=======UserDao=======");
    userDao.add();
  }
  public static void main(String[] args) {
    UserAction userAction = (UserAction)IocContainner.get("userAction");
    System.out.println(userAction);
    UserDao userDao =(UserDao)IocContainner.get("userDao");
    System.out.println(userDao);
    userAction.add();
  }
  public UserDao getUserDao() {
    return userDao;
  }
  public void setUserDao(UserDao userDao) {
    this.userDao = userDao;
  }
}

Dao:

public interface UserDao {
  public void add();
}

public class UserDaoImpl implements UserDao{

  @Override
  public void add() {
    System.out.println("======UserDao=======");
  }
}

IOC容器:

/**
* 初始化容器
* @author Administrator
*
*/
public class IocContainner {
  private static final Map<String,Object> iocMap = new HashMap<String,Object>();
  static{
    try {
      initIocMap();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  public static void initIocMap() throws Exception{
    //Java and Xml binding
    JAXBContext jc= JAXBContext.newInstance(Beans.class);
    Unmarshaller um =jc.createUnmarshaller();
    Beans bs =(Beans)um.unmarshal(new File("src/beans.xml"));
    for(Bean b:bs.getBeanList()){
      Class c = Class.forName(b.getClassPath());
      iocMap.put(b.getId(), c.newInstance());
      System.out.println("++++++++"+b.getId());
    }
    //完成属性注入
    /*
    * 思路:遍历所有的对象,判断那个对象有属性
    * 找到有属性的, 获取name和ref 的值
    * name有了 就可以弄设定器 反射调用
    */
    for(Bean b:bs.getBeanList()){
      List<Property> proList = b.getProList();
      if(proList!=null&&proList.size()>0){
        for(Property pro:proList){
          Object obj = iocMap.get(b.getId());
          Class clazz = obj.getClass();
          String name = pro.getName();
          String ref = pro.getRef();
          System.out.println(ref);
          Field field =clazz.getDeclaredField(name);
          String setter = "set"+name.substring(0, 1).toUpperCase()+name.substring(1);
          Method setMethod = clazz.getDeclaredMethod(setter,field.getType());
          setMethod.invoke(obj,iocMap.get(ref));
          System.out.println(iocMap.get(ref)+"-----+++-----");
        }
      }
    }

  }
  public static Object get(String key){
    return iocMap.get(key);
  }
}

以下为xmlmodel

@XmlRootElement
public class Beans {
  private List<Bean> beanList;
  @XmlElement(name="bean")
  public List<Bean> getBeanList() {
    return beanList;
  }

  public void setBeanList(List<Bean> beanList) {
    this.beanList = beanList;
  }

}

public class Bean {
  private String id;
  private String classPath;
  private List<Property> proList;
  @XmlAttribute
  public String getId() {
    return id;
  }
  public void setId(String id) {
    this.id = id;
  }
  @XmlAttribute(name="class")
    public String getClassPath() {
    return classPath;
  }
  public void setClassPath(String classPath) {
    this.classPath = classPath;
  }
  @XmlElement(name="property")
  public List<Property> getProList() {
    return proList;
  }
  public void setProList(List<Property> proList) {
    this.proList = proList;
  }
}

public class Property {
  private String name;
  private String ref;
  @XmlAttribute
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  @XmlAttribute
  public String getRef() {
    return ref;
  }
  public void setRef(String ref) {
    this.ref = ref;
  }
}