Download
slide1 n.
Skip this Video
Loading SlideShow in 5 Seconds..
Inversion of Control PowerPoint Presentation
Download Presentation
Inversion of Control

Inversion of Control

6 Vues Download Presentation
Télécharger la présentation

Inversion of Control

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Inversion of Control • User interfaces • UI framework contains main loop • Applications implement event handlers • Plug-in lookup • Dependency Injection • Service Locator

  2. The Problem public interface ValueSource { public int getValue();}

  3. The Problem public class ValueConsumer { private ValueSource vs; ... public void consumeValue() { vs.getValue(); }}

  4. The Problem public class ValueConsumer { private ValueSource vs; public ValueConsumer() { vs = new XMLValueSource(); } ...}

  5. Dependency Injection • Constructor arguments • Properties

  6. Heavyweight Frameworks • Components of predetermined type • Forced dependencies • Microsoft OLE • J2EE Servlets • J2EE EJBs

  7. Lightweight Frameworks • POJOs • No forced dependencies • Easy component testing and reuse

  8. Spring • Open source framework • Dependency injection • Lightweight • Set of snap-in services • Simplifies J2EE development

  9. How to use Spring • Write beans, omit dependency code • Write getters and setters • Configure your beans in XML • Spring instantiates your objects • Spring satisfies any dependencies

  10. The BeanFactory • The container • Manages beans • Most user code unaware of factory • Factory must be instantiated

  11. The BeanFactory InputStream in = new FileInputStream("beans.xml"); XmlBeanFactory factory = new XmlBeanFactory(in);

  12. The BeanFactory • Spring can instantiate the factory • Web layer

  13. XML Bean Configuration <?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="..." class="...">...</bean> <bean id="..." class="...">...</bean> ...</beans>

  14. Bean Definition • Classname • Behavior in container • Prototype or singleton • Autowiring • Dependency checking • Initialization and destruction strategies • Constructor arguments • Property values • Collaborators

  15. Bean Definition <bean id="exBean" class="ex.ExBean"> <property name="beanOne"> <ref bean="beanA"/> </property> <property name="beanTwo"> <ref bean="beanB"/> </property> <property name="integerProperty"> <value>1</value> </property> </bean><bean id="beanA" class="ex.BeanA"/><bean id="beanB" class="ex.BeanB"/>

  16. Constructor Arguments <bean id="exBean" class="ex.ExBean"> <constructor-arg> <ref bean="beanA"/> </constructor-arg> <constructor-arg> <ref bean="beanB"/> </constructor-arg> <constructor-arg> <value>1</value> </constructor-arg> </bean><bean id="beanA" class="ex.BeanA"/><bean id="beanB" class="ex.BeanB"/>

  17. Static Factory Method <bean id="exBean" class="ex.ExBean" factory-method="createInstance"> <constructor-arg> <ref bean="beanA"/> </constructor-arg> <constructor-arg> <ref bean="beanB"/> </constructor-arg> <constructor-arg> <value>1</value> </constructor-arg> </bean><bean id="beanA" class="ex.BeanA"/><bean id="beanB" class="ex.BeanB"/>

  18. The ApplicationContext • Subclass to BeanFactory • Message sources • Access to resources • Event propagation • Hierarchical contexts

  19. An example…