1 / 20

Inversion of Control

Inversion of Control. User interfaces UI framework contains main loop Applications implement event handlers Plug-in lookup Dependency Injection Service Locator. The Problem. public interface ValueSource { public int getValue(); }. The Problem.

Download Presentation

Inversion of Control

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

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…

More Related