200 likes | 229 Views
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.
E N D
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 public class ValueConsumer { private ValueSource vs; ... public void consumeValue() { vs.getValue(); }}
The Problem public class ValueConsumer { private ValueSource vs; public ValueConsumer() { vs = new XMLValueSource(); } ...}
Dependency Injection • Constructor arguments • Properties
Heavyweight Frameworks • Components of predetermined type • Forced dependencies • Microsoft OLE • J2EE Servlets • J2EE EJBs
Lightweight Frameworks • POJOs • No forced dependencies • Easy component testing and reuse
Spring • Open source framework • Dependency injection • Lightweight • Set of snap-in services • Simplifies J2EE development
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
The BeanFactory • The container • Manages beans • Most user code unaware of factory • Factory must be instantiated
The BeanFactory InputStream in = new FileInputStream("beans.xml"); XmlBeanFactory factory = new XmlBeanFactory(in);
The BeanFactory • Spring can instantiate the factory • Web layer
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>
Bean Definition • Classname • Behavior in container • Prototype or singleton • Autowiring • Dependency checking • Initialization and destruction strategies • Constructor arguments • Property values • Collaborators
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"/>
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"/>
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"/>
The ApplicationContext • Subclass to BeanFactory • Message sources • Access to resources • Event propagation • Hierarchical contexts