By Subbu Ramanathan - Huihoodocs.huihoo.com/picocontainer/PicoContainer.pdf · By Subbu Ramanathan...

44
Dependency Injection using By Subbu Ramanathan Presented at The Great Lakes Software Symposium Chicago, IL – October 3, 2004

Transcript of By Subbu Ramanathan - Huihoodocs.huihoo.com/picocontainer/PicoContainer.pdf · By Subbu Ramanathan...

Page 1: By Subbu Ramanathan - Huihoodocs.huihoo.com/picocontainer/PicoContainer.pdf · By Subbu Ramanathan ... Comparison with Spring Comparison with Avalon EJB3 ? Conclusion. 3 Subbu Ramanathan

Dependency Injection using

BySubbu Ramanathan

Presented atThe Great Lakes Software SymposiumChicago, IL – October 3, 2004

Page 2: By Subbu Ramanathan - Huihoodocs.huihoo.com/picocontainer/PicoContainer.pdf · By Subbu Ramanathan ... Comparison with Spring Comparison with Avalon EJB3 ? Conclusion. 3 Subbu Ramanathan

Subbu Ramanathan2

Agenda

� The need for Pico Container� Dependency Injection – What & How?� Pico Container basics� Container Hierarchies� Life cycle support in Pico Container� NanoContainer� NanoWar� Comparison with Spring� Comparison with Avalon� EJB3 ?� Conclusion

Page 3: By Subbu Ramanathan - Huihoodocs.huihoo.com/picocontainer/PicoContainer.pdf · By Subbu Ramanathan ... Comparison with Spring Comparison with Avalon EJB3 ? Conclusion. 3 Subbu Ramanathan

Subbu Ramanathan3

Coupling

� Best practice - create loosely coupled components– High Level Modules should not depend upon low

level modules and vice-versa. Both should depend on abstractions.

– Abstractions should not depend on details. Details must depend on abstractions.

Page 4: By Subbu Ramanathan - Huihoodocs.huihoo.com/picocontainer/PicoContainer.pdf · By Subbu Ramanathan ... Comparison with Spring Comparison with Avalon EJB3 ? Conclusion. 3 Subbu Ramanathan

Subbu Ramanathan4

Achieve loose coupling

� This goal must be accomplished – With minimal effort– Using a least intrusive mechanism– Without adverse functional implications

Page 5: By Subbu Ramanathan - Huihoodocs.huihoo.com/picocontainer/PicoContainer.pdf · By Subbu Ramanathan ... Comparison with Spring Comparison with Avalon EJB3 ? Conclusion. 3 Subbu Ramanathan

Subbu Ramanathan5

An Example

MovieLister<<interface>>

MovieFinder

ColonMovieFinder

Page 6: By Subbu Ramanathan - Huihoodocs.huihoo.com/picocontainer/PicoContainer.pdf · By Subbu Ramanathan ... Comparison with Spring Comparison with Avalon EJB3 ? Conclusion. 3 Subbu Ramanathan

Subbu Ramanathan6

GOF Solution – Factory Pattern

� Use a Factory class to instantiate the component you need

� Only the Factory knows about the concrete class, not your application

� Pros– Helps to resolve tight coupling

� Cons– Intrusive: Components need to be requested explicitly– Need recompiling to change specific component in

application

Page 7: By Subbu Ramanathan - Huihoodocs.huihoo.com/picocontainer/PicoContainer.pdf · By Subbu Ramanathan ... Comparison with Spring Comparison with Avalon EJB3 ? Conclusion. 3 Subbu Ramanathan

Subbu Ramanathan7

Alternative - Hollywood Principal

� aka “don’t call us, we’ll call you”– Components should not be explicitly requested– Components will be provided as needed

� aka Inversion of Control

Page 8: By Subbu Ramanathan - Huihoodocs.huihoo.com/picocontainer/PicoContainer.pdf · By Subbu Ramanathan ... Comparison with Spring Comparison with Avalon EJB3 ? Conclusion. 3 Subbu Ramanathan

Subbu Ramanathan8

Dependency Injection

� When IoC is about looking up pluginimplementation, Martin Fowler calls it Dependency injection

� Dependency injection is a term used to resolve component dependencies by injecting an instantiated component to satisfy a dependency

Page 9: By Subbu Ramanathan - Huihoodocs.huihoo.com/picocontainer/PicoContainer.pdf · By Subbu Ramanathan ... Comparison with Spring Comparison with Avalon EJB3 ? Conclusion. 3 Subbu Ramanathan

Subbu Ramanathan9

Forms of Dependency Injection

� Constructor Injection with Pico Container� Setter Injection with Spring� Interface Injection with Avalon (Merlin)

Page 10: By Subbu Ramanathan - Huihoodocs.huihoo.com/picocontainer/PicoContainer.pdf · By Subbu Ramanathan ... Comparison with Spring Comparison with Avalon EJB3 ? Conclusion. 3 Subbu Ramanathan

Subbu Ramanathan10

Constructor Injection

� Constructor Injection is a Dependency Injection variant where an object gets all its dependencies via the constructor.

Page 11: By Subbu Ramanathan - Huihoodocs.huihoo.com/picocontainer/PicoContainer.pdf · By Subbu Ramanathan ... Comparison with Spring Comparison with Avalon EJB3 ? Conclusion. 3 Subbu Ramanathan

Subbu Ramanathan11

Example Changed For Pico Container

� Pico Container uses a constructor to decide how to inject a finder implementation into the lister class.

� For this to work, the movie lister class needs to declare a constructor that includes everything it needs injected.

Page 12: By Subbu Ramanathan - Huihoodocs.huihoo.com/picocontainer/PicoContainer.pdf · By Subbu Ramanathan ... Comparison with Spring Comparison with Avalon EJB3 ? Conclusion. 3 Subbu Ramanathan

Subbu Ramanathan12

Example Using Pico Container

Page 13: By Subbu Ramanathan - Huihoodocs.huihoo.com/picocontainer/PicoContainer.pdf · By Subbu Ramanathan ... Comparison with Spring Comparison with Avalon EJB3 ? Conclusion. 3 Subbu Ramanathan

Subbu Ramanathan13

Constructor Injection Advantages

� It makes a strong dependency contract. Setting is atomic in a sense that either all or none of the dependencies are set and that it can occur once and only once

� Dependency is easily identifiable� A dependency may be made immutable by

making the dependency reference final

Page 14: By Subbu Ramanathan - Huihoodocs.huihoo.com/picocontainer/PicoContainer.pdf · By Subbu Ramanathan ... Comparison with Spring Comparison with Avalon EJB3 ? Conclusion. 3 Subbu Ramanathan

Subbu Ramanathan14

What is Pico Container?

� Light weight container� Not a replacement for J2EE� Non-intrusive. Components can be POJOs� Provides component’s life cycle management� Extensible� 50K jar that depends only on JDK1.3

Page 15: By Subbu Ramanathan - Huihoodocs.huihoo.com/picocontainer/PicoContainer.pdf · By Subbu Ramanathan ... Comparison with Spring Comparison with Avalon EJB3 ? Conclusion. 3 Subbu Ramanathan

Subbu Ramanathan15

Singleton vs. Pico

� Singleton provides one instance with global visibility� Singleton causes hard-wired dependency� Singletons are hard to test with Mock objects� Container hierarchies in Pico provide fine grained

control over a component’s visibility scope� A container (and its registered components) can get

access to components registered in a parent container, but not vice-versa.

Page 16: By Subbu Ramanathan - Huihoodocs.huihoo.com/picocontainer/PicoContainer.pdf · By Subbu Ramanathan ... Comparison with Spring Comparison with Avalon EJB3 ? Conclusion. 3 Subbu Ramanathan

Subbu Ramanathan16

Container Hierarchies

Page 17: By Subbu Ramanathan - Huihoodocs.huihoo.com/picocontainer/PicoContainer.pdf · By Subbu Ramanathan ... Comparison with Spring Comparison with Avalon EJB3 ? Conclusion. 3 Subbu Ramanathan

Subbu Ramanathan17

Lifecycle support in Pico

� Lifecycle involves post creation life of a component� Useful when tree of component dependencies exist� A “lifecycle method” invoked on parent container

cascades through container components & down to child containers

� A “lifecycle method” invoked on a child container does NOT cascade up through parents

� Child containers must register as a component with parent container for lifecycle support to work

Page 18: By Subbu Ramanathan - Huihoodocs.huihoo.com/picocontainer/PicoContainer.pdf · By Subbu Ramanathan ... Comparison with Spring Comparison with Avalon EJB3 ? Conclusion. 3 Subbu Ramanathan

Subbu Ramanathan18

Lifecycle Interfaces

� Pico provides Startable & Disposable interfaces

� Startable two methods: start() and stop()� Disposable has one method: dispose()� It is okay if some or none of the components

implement these lifecycle interfaces

Page 19: By Subbu Ramanathan - Huihoodocs.huihoo.com/picocontainer/PicoContainer.pdf · By Subbu Ramanathan ... Comparison with Spring Comparison with Avalon EJB3 ? Conclusion. 3 Subbu Ramanathan

Subbu Ramanathan19

Example Using Lifecycle

Page 20: By Subbu Ramanathan - Huihoodocs.huihoo.com/picocontainer/PicoContainer.pdf · By Subbu Ramanathan ... Comparison with Spring Comparison with Avalon EJB3 ? Conclusion. 3 Subbu Ramanathan

Subbu Ramanathan20

Lifecycle Appropriateness

� Developing unattended services� A single point of control for multiple

cascaded operations� Transaction processing

Page 21: By Subbu Ramanathan - Huihoodocs.huihoo.com/picocontainer/PicoContainer.pdf · By Subbu Ramanathan ... Comparison with Spring Comparison with Avalon EJB3 ? Conclusion. 3 Subbu Ramanathan

Subbu Ramanathan21

Custom Lifecycle

� Instead of mandating the default lifecycle interfaces, Pico allows for development of custom Lifecycle support

� This functionality has been moved into the NanoContainer

Page 22: By Subbu Ramanathan - Huihoodocs.huihoo.com/picocontainer/PicoContainer.pdf · By Subbu Ramanathan ... Comparison with Spring Comparison with Avalon EJB3 ? Conclusion. 3 Subbu Ramanathan

Subbu Ramanathan22

Custom Lifecycle Code

������������������� �������������� ������������������������� �

��� ������������ ������

����� ������������ ����� �� �����!���"�������� ����#���$������ �!���"�����������!���%�� �����"��������&�� �� ����������������������&�� %� �����

���&���'�������� ��� ���������������� ()�*#����������������+� ��������%��*��������,�����,�

��$�-�������!��(�.����������()�*%���� �����

Page 23: By Subbu Ramanathan - Huihoodocs.huihoo.com/picocontainer/PicoContainer.pdf · By Subbu Ramanathan ... Comparison with Spring Comparison with Avalon EJB3 ? Conclusion. 3 Subbu Ramanathan

Subbu Ramanathan23

NanoContainer

� Is a script enabled front-end for Pico Container� Register components through a script� Supports

– Groovy– BeanShell– Jython– Rhino JavaScript– XML

Page 24: By Subbu Ramanathan - Huihoodocs.huihoo.com/picocontainer/PicoContainer.pdf · By Subbu Ramanathan ... Comparison with Spring Comparison with Avalon EJB3 ? Conclusion. 3 Subbu Ramanathan

Subbu Ramanathan24

Example Using NanoContainer

Page 25: By Subbu Ramanathan - Huihoodocs.huihoo.com/picocontainer/PicoContainer.pdf · By Subbu Ramanathan ... Comparison with Spring Comparison with Avalon EJB3 ? Conclusion. 3 Subbu Ramanathan

Subbu Ramanathan25

NanoWar

� NanoWar allows to embed NanoContainer within a Java Servlet container

� It is non-intrusively activated in a Web app via a Servlet container listener

� Creates PicoContainer instances at the application, session, and request levels

� These are created when web application starts, session is created, and request is created respectively

Page 26: By Subbu Ramanathan - Huihoodocs.huihoo.com/picocontainer/PicoContainer.pdf · By Subbu Ramanathan ... Comparison with Spring Comparison with Avalon EJB3 ? Conclusion. 3 Subbu Ramanathan

Subbu Ramanathan26

NanoWar – Pico Containers

Page 27: By Subbu Ramanathan - Huihoodocs.huihoo.com/picocontainer/PicoContainer.pdf · By Subbu Ramanathan ... Comparison with Spring Comparison with Avalon EJB3 ? Conclusion. 3 Subbu Ramanathan

Subbu Ramanathan27

NanoWar & WebWork

� The NanoWar – WebWork integration kit allows WebWork actions to be PicoContainercomponents

� Provides for decoupling Actions from Persistence Layer/Communication Layer

� Allows for easier unit testing of Actions outside of the web application using mock objects

Page 28: By Subbu Ramanathan - Huihoodocs.huihoo.com/picocontainer/PicoContainer.pdf · By Subbu Ramanathan ... Comparison with Spring Comparison with Avalon EJB3 ? Conclusion. 3 Subbu Ramanathan

Subbu Ramanathan28

Example Using NanoWar - WebWork

Page 29: By Subbu Ramanathan - Huihoodocs.huihoo.com/picocontainer/PicoContainer.pdf · By Subbu Ramanathan ... Comparison with Spring Comparison with Avalon EJB3 ? Conclusion. 3 Subbu Ramanathan

Subbu Ramanathan29

Nanocontainer & Persistence

� Nanocontainer-Hibernate� Nanocontainer-Jaxor

Page 30: By Subbu Ramanathan - Huihoodocs.huihoo.com/picocontainer/PicoContainer.pdf · By Subbu Ramanathan ... Comparison with Spring Comparison with Avalon EJB3 ? Conclusion. 3 Subbu Ramanathan

Subbu Ramanathan30

Pico Container & Jaxor

� Jaxor is an OR mapping tool which generates the persistence classes from XML based entity definitions

� Code generation is done using Velocity templates

� Jaxor can use Pico Container to register all concrete classes

Page 31: By Subbu Ramanathan - Huihoodocs.huihoo.com/picocontainer/PicoContainer.pdf · By Subbu Ramanathan ... Comparison with Spring Comparison with Avalon EJB3 ? Conclusion. 3 Subbu Ramanathan

Subbu Ramanathan31

Jaxor Classes

� Implementations of the interfaces that JaxorContextImpl depends on

� JaxorContainer provides lifecycle support by implementing Pico’s Stoppable

� When the Container is Stopped, the Context is ended and db operations can be committed

Page 32: By Subbu Ramanathan - Huihoodocs.huihoo.com/picocontainer/PicoContainer.pdf · By Subbu Ramanathan ... Comparison with Spring Comparison with Avalon EJB3 ? Conclusion. 3 Subbu Ramanathan

Subbu Ramanathan32

Jaxor Code

Page 33: By Subbu Ramanathan - Huihoodocs.huihoo.com/picocontainer/PicoContainer.pdf · By Subbu Ramanathan ... Comparison with Spring Comparison with Avalon EJB3 ? Conclusion. 3 Subbu Ramanathan

Subbu Ramanathan33

Other Pico Features

� Pico container also supports Setter Injection using SetterInjectionComponentAdapterclass

� But, Pico really recommends Constructor Injection for obvious reasons

Page 34: By Subbu Ramanathan - Huihoodocs.huihoo.com/picocontainer/PicoContainer.pdf · By Subbu Ramanathan ... Comparison with Spring Comparison with Avalon EJB3 ? Conclusion. 3 Subbu Ramanathan

Subbu Ramanathan34

Setter Injection using Spring

� Spring framework uses a BeanFactory that stores information about all the beans used by the application

� The BeanFactory supplies the application with the needed beans when requested

� XMLBeanFactory is a BeanFactory that reads bean definitions from an XML file

� Beans can be defined as Singletons or not� Spring can resolve bean dependencies

Page 35: By Subbu Ramanathan - Huihoodocs.huihoo.com/picocontainer/PicoContainer.pdf · By Subbu Ramanathan ... Comparison with Spring Comparison with Avalon EJB3 ? Conclusion. 3 Subbu Ramanathan

Subbu Ramanathan35

Spring Configuration

Page 36: By Subbu Ramanathan - Huihoodocs.huihoo.com/picocontainer/PicoContainer.pdf · By Subbu Ramanathan ... Comparison with Spring Comparison with Avalon EJB3 ? Conclusion. 3 Subbu Ramanathan

Subbu Ramanathan36

Spring Code

Page 37: By Subbu Ramanathan - Huihoodocs.huihoo.com/picocontainer/PicoContainer.pdf · By Subbu Ramanathan ... Comparison with Spring Comparison with Avalon EJB3 ? Conclusion. 3 Subbu Ramanathan

Subbu Ramanathan37

Spring – Constructor Injection

� Configuration XML/�������#0/�������#0/�������#0/�������#0��������" ������������" ������������" ������������" ����1�� ���#01�� ���#01�� ���#01�� ���#0���� ��%2��������!��" �������� ��%2��������!��" �������� ��%2��������!��" �������� ��%2��������!��" ����34343434

/�����������/�����������/�����������/�����������5555�� �� �� �� ����(#0614����(#0614����(#0614����(#0614/��������#0/��������#0/��������#0/��������#0�������.�������������.�������������.�������������.������3�43�43�43�4

/������������/������������/������������/������������5555�� �� �� �� 4444/�����4/�����4/�����4/�����4

� Java Code��� ���� ������� ���� ������� ���� ������� ���� ����2��������!��" ����2��������!��" ����2��������!��" ����2��������!��" ���� ��� �� �������� �� �������� �� �������� �� �����!��" ����!��" ����!��" ����!��" ���� ������� ������ ������ ������ ���2��������!��" �����-������.������2��������!��" �����-������.������2��������!��" �����-������.������2��������!��" �����-������.������ ����������������������������������������

���%�������.���������%�������.���������%�������.���������%�������.������ #���������#���������#���������#�������������

����

Page 38: By Subbu Ramanathan - Huihoodocs.huihoo.com/picocontainer/PicoContainer.pdf · By Subbu Ramanathan ... Comparison with Spring Comparison with Avalon EJB3 ? Conclusion. 3 Subbu Ramanathan

Subbu Ramanathan38

Interface Injection

� Injection is done through an interface� The interface will define the injection method

and the implementation class has to implement this interface and provide concrete implementation for the injection method.

� Avalon provides Interface Injection

Page 39: By Subbu Ramanathan - Huihoodocs.huihoo.com/picocontainer/PicoContainer.pdf · By Subbu Ramanathan ... Comparison with Spring Comparison with Avalon EJB3 ? Conclusion. 3 Subbu Ramanathan

Subbu Ramanathan39

Avalon Code

Page 40: By Subbu Ramanathan - Huihoodocs.huihoo.com/picocontainer/PicoContainer.pdf · By Subbu Ramanathan ... Comparison with Spring Comparison with Avalon EJB3 ? Conclusion. 3 Subbu Ramanathan

Subbu Ramanathan40

Dependency Injection with EJB3

7-���������� ���� ����+�8��� ��

������������-����� ���������8��

7&�*��������������������"��������8�����-����� ���������8�������%���������8#����������8��

��

��� ����������������

%%%�

"������������#����������8% ��"�������������%%%�

��

��

Page 41: By Subbu Ramanathan - Huihoodocs.huihoo.com/picocontainer/PicoContainer.pdf · By Subbu Ramanathan ... Comparison with Spring Comparison with Avalon EJB3 ? Conclusion. 3 Subbu Ramanathan

Subbu Ramanathan41

EJB3

� EJB 3 has some built-in support for dependency management using JNDI

� EJB3 relies on an application server

Page 42: By Subbu Ramanathan - Huihoodocs.huihoo.com/picocontainer/PicoContainer.pdf · By Subbu Ramanathan ... Comparison with Spring Comparison with Avalon EJB3 ? Conclusion. 3 Subbu Ramanathan

Subbu Ramanathan42

Solution Comparisons

� Really comes down to Pico Vs. Spring� Spring is more mature than Pico� Pico is much lighter than Spring. Spring is more

than just an IoC container. It is a web framework as well.

� If you would like to use Spring for other reasons as well, Spring is the choice

� If you just need only IoC (or maybe for a non-web or a Struts/WebWork app), then Pico is the choice

Page 43: By Subbu Ramanathan - Huihoodocs.huihoo.com/picocontainer/PicoContainer.pdf · By Subbu Ramanathan ... Comparison with Spring Comparison with Avalon EJB3 ? Conclusion. 3 Subbu Ramanathan

Subbu Ramanathan43

Design Considerations

� What classes to register with container?� Enforcing consistent standards� Application extensibility & reusability?� NanoContainer vs. PicoContainer� Pico Stability� Pico Support

Page 44: By Subbu Ramanathan - Huihoodocs.huihoo.com/picocontainer/PicoContainer.pdf · By Subbu Ramanathan ... Comparison with Spring Comparison with Avalon EJB3 ? Conclusion. 3 Subbu Ramanathan

Subbu Ramanathan44

Thank You

ForAttending

ThisPresentation

(Please remember to fill out the session evaluation)