The inversion control spring 2.x (IOC)

  Inversion of Control is to control the name suggests, in turn, saying that white is a java bean inside the property is not generated inside the injection procedure, but rather this is actually injected into the so-called dependency injection by spring xml configuration file. Consider the following codes

public class KnightOfTheRoundTable implements Knight {
  private String name;
  private Quest quest;
  
  public KnightOfTheRoundTable(String name) {
    this.name = name;
  }
  
  public Object embarkOnQuest() throws QuestFailedException {
    return quest.embark();
  }
  
  public void setQuest(Quest quest) {
    this.quest = quest;
  }
  
  public String getName() {
    return name;
  }
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
  <bean id="quest"
      class="com.springinaction.chapter01.knight.HolyGrailQuest"/>

  <bean id="knightTarget"
      class="com.springinaction.chapter01.knight.KnightOfTheRoundTable">
    <property name="name" value="hello"/>
    <property name="quest">
      <ref bean="quest"/>
    </property>
  </bean>
</beans>
public class KnightApp {
  public static void main(String[] args) throws Exception {

  Factory // = the BeanFactory the BeanFactory actually spring containers for loading the bean
  new new the XmlBeanFactory (a FileSystemResource new new ( "knight.xml"));


    Knight knight =
        (Knight) ctx.getBean("knight");

    knight.embarkOnQuest();
  }
}

  The above xml attribute gives a complete assembly process, id indicates its location in the bean class knightTarget, and refers to the property attribute. <Property name = "name" value = "hello" /> This means there is a bean name "name" attribute value injection "hello", and the following property because it is a class rather than a simple string so give property to "quest" is injected into a bean, it is in front of the start-defined name "quest" of the bean

  

  

Reproduced in: https: //my.oschina.net/secyaher/blog/274432

Guess you like

Origin blog.csdn.net/weixin_34082789/article/details/91967033