Dependency Injection in Action Jan Västernäs. CADEC2006, DI, Slide 2 Copyright 2006, Callista Enterprise AB Agenda (DI=Dependency Injection) Background.

Slides:



Advertisements
Similar presentations
JSP and web applications
Advertisements

Apache Struts Technology
Spring, Hibernate and Web Services 13 th September 2014.
Introduction to Spring Matt Wheeler. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Java Stack – Basic.
An architecture for webb applications, J2EE
Enterprise Applications & Java/J2EE Technologies Dr. Douglas C. Schmidt Professor of EECS.
Pragmatic Application Building: Step by Step Jay Sissom Principal Systems Analyst Indiana University
The Spring Framework: A brief introduction to Inversion of Control James Brundege
Last Update: 5/15/061 Spring J2EE Framework Ram A. Rao May 15 th ‘05.
Intro to Spring CJUG - January What is Spring? “The Spring framework provides central transaction control of various objects.” This means that any.
UNIT-V The MVC architecture and Struts Framework.
Lecture 2 - Struts ENTERPRISE JAVA. 2 Contents  Servlet Deployment  Servlet Filters  Model View Controllers  Struts  Dependency Injection.
Java Frameworks Indy Java Users Group January 29, 2003.
NetArchive Suite Workshop 2011 Technical Track - Code refactoring with the Spring Framework.
Spring Framework. Spring Overview Spring is an open source layered Java/J2EE application framework Created by Rod Johnson Based on book “Expert one-on-one.
Introduction to the Spring Framework By: Nigusse A. Duguma Kansas State university Department of Computer Science Nov 20, 2007.
Spring Overview, Application demo -Midhila Paineni 09/23/2011 Spring Overview, Application demo9/8/20151.
MAVEN-BLUEMARTINI Yannick Robin. What is maven-bluemartini?  maven-bluemartini is Maven archetypes for Blue Martini projects  Open source project on.
The Spring Framework Training Get to know Spring Framework Rohit Prabhakar
Introduction to the Spring Framework Rajesh. Spring Mission Statement J2EE should be easier to use OO design is more important than any implementation.
Ing. Ignacio Roqueta ARTech GeneXus and J2EE.
SelfDiagnose “who is to blame” ernest micklei, April 2007.
© D. Wong  Indexes  JDBC  JDBC in J2EE (Java 2 Enterprise Edition)
Entity Java Beans Jorg Janke Open Source ERP & CRM.
EJB Framework.  As we know, EJB is the center of the J2EE architecture that provides a sturdy framework for building enterprise applications. The major.
TDDD05 EJB Lab (Part of slides reused from Mikhail’s) Lu Li
Spring core v3.x Prepared by: Nhan Le. History v3.0 Spring Expression Language Java based bean metadata v3.1 Cache Abstraction Bean Definition Profile.
Message-Driven Beans and EJB Security Lesson 4B / Slide 1 of 37 J2EE Server Components Objectives In this lesson, you will learn about: Identify features.
|Tecnologie Web L-A Anno Accademico Laboratorio di Tecnologie Web Introduzione ad Eclipse e Tomcat
Text 16 de mayo de 2009 Spring Framework Part I. Dependency Injection Buenos Aires, May 2009.
PicoContainer Presented by: Jim O’Hara Ed Kausmeyer Jingming Zhang.
Opus College - overview. OpusCollege - background First project: ICT Capacity Building Mozambican Higher Education Institutions Partners: RUG Groningen,
Spring Framework. Spring Overview Spring is an open source layered Java/J2EE application framework Created by Rod Johnson Based on book “Expert one-on-one.
Introduction to Spring Matt Wheeler. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Java Stack – Basic.
Creative Commons Attribution- NonCommercial-ShareAlike 2.5 License Sakai Programmer's Café Sakai Montreal CRIM Workshop Introduction to Spring Framework,
Introduction to Web Dimitar Nenchev Ivan Nakov
Ch 2 – Application Assembly and Deployment COSC 617 Jeff Schmitt September 14, 2006.
Copyright© , The Seasar Foundation and the others. All rights reserved. 1 Configuration files must Die!!! Yasuo Higa The Seasar Foundation/Chief.
JBoss at Work Databases and JBoss Chapter 4 Jeff Schmitt October 26, 2006.
1 Architectural Overview For application developers, assembling enterprise beans requires little or no expertise in the complex system-level issues that.
A Presentation By V AIBHAV S AHARAN Web-enHanced Information Management COMS E6125.
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Introduction to Spring Remoting Simplifying.
Web dialogs as drop-in components Håkan Dahl, Callista Enterprise AB Killing the monolithic webapp.
Text 16 de mayo de 2009 Spring Framework Part III. Portable Service Abstractions Buenos Aires, June 2009.
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Spring MVC Essentials Getting started.
Spring and DWR Frameworks for Rich Web Enterprise Application Thomas Wiradikusuma Presentation to the 20 th.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 JSP Application Models.
1 Spring Framework April, 2012 Lam Ho Lam To. © 2010 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 2 1.Spring Overview 2.Framework.
1 Copyright © 2004, Oracle. All rights reserved. Oracle Application Development Framework.
JAVA EE 6 Best Practices for Migrating Spring to WTF ?!?
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Introduction to Data Access with Spring.
Kansas City Java User’s Group Jason W. Bedell July 12, 2006
Introduction to EJB. What is an EJB ?  An enterprise java bean is a server-side component that encapsulates the business logic of an application. By.
Java Programming: Advanced Topics 1 Enterprise JavaBeans Chapter 14.
Text 16 de mayo de 2009 Spring Framework Part III. Portable Service Abstractions Buenos Aires, June 2009.
Singleton Academy, Pune. Course syllabus Singleton Academy Pune – Course Syllabus1.
Developing with the Framework Zach A. Thomas, Texas State University
Text 16 de mayo de 2009 Spring Framework Part III. Portable Service Abstractions and Spring MVC Buenos Aires, June 2009.
Creative Commons Attribution- NonCommercial-ShareAlike 2.5 License Sakai Programmer's Café Sakai Montreal CRIM Workshop Introduction to Spring Framework.
Enterprise Java Beans. Contents  Understanding EJBs  Practice Section.
J2EE Lecture 6: Spring – IoC and Dependency Injection
Structure of a web application
EJB (Enterprise Java Beans)
Magento Technical Guidelines Eugene Shakhsuvarov, Software Magento
Introduction to J2EE Architecture
Intro to Spring CJUG - January 2013.
Understanding and Designing with EJB
JavaServer Faces: The Fundamentals
Developing and testing enterprise Java applications
Enterprise Java Beans.
Presentation transcript:

Dependency Injection in Action Jan Västernäs

CADEC2006, DI, Slide 2 Copyright 2006, Callista Enterprise AB Agenda (DI=Dependency Injection) Background What is DI ? What is the DI value promise ? Experiences –Using Spring DI container Does DI live up to the promise ? Future & Others

CADEC2006, DI, Slide 3 Copyright 2006, Callista Enterprise AB Background Using spring framework for one year Limited by policy to some parts only Large industrial project My role : Application architect Architecture & method team Mix of very experienced developers and new-to-java developers

CADEC2006, DI, Slide 4 Copyright 2006, Callista Enterprise AB Spring usage Dependency Injection –480 spring beans –680 SQL statements as beans/properties JDBC support –JDBC Template –Exception Hierarchy JMS Support –JMS Template Using ”factory beans” to intercept calls for –Out-of-container transaction/connection support –Declarative transaction demarcation Test classes

CADEC2006, DI, Slide 5 Copyright 2006, Callista Enterprise AB What is Dependency Injection ? Dont call us - we’ll call you Referenced object are declared References are injected into an object –By calling setter method Need some kind of runtime support ( DI container ) Enables call interceptors to be used Opposite of ”Dependency Lookup”

CADEC2006, DI, Slide 6 Copyright 2006, Callista Enterprise AB Dependency Injection public void setDao(CustomerDao dao) { this.dao = dao; }

CADEC2006, DI, Slide 7 Copyright 2006, Callista Enterprise AB What is the DI value promise ? No energy waisted nor code needed to find other objects –No JNDI, singletons, factories etc Unit Test easier by mocking dependencies –May even make it possible without workarounds ! Non-intrusive –In our project : Business code independant/unaware of technical infrastructure (including DI container). ”Lighter” than EJB

CADEC2006, DI, Slide 8 Copyright 2006, Callista Enterprise AB Experiences Typical scenario Setup Configuration Bootstrapping Namespaces Programming model Integration Test Interceptors

CADEC2006, DI, Slide 9 Copyright 2006, Callista Enterprise AB Typical scenario Layered architecture Thin clients – request/response J2EE Web-apps EJB module only for MessageDriven EJB:s JDBC - generated and handwritten

CADEC2006, DI, Slide 10 Copyright 2006, Callista Enterprise AB

CADEC2006, DI, Slide 11 Copyright 2006, Callista Enterprise AB Spring setup 1 hour to get started with basic DI container functionality

CADEC2006, DI, Slide 12 Copyright 2006, Callista Enterprise AB Spring DI Configuration Overhead writing Xml config is acceptable –Use classnames as bean id and reference name –Copy-paste - nobrainer private OrderEntity orderEntity; public void setOrderEntity(OrderEntity orderEntity) { this.orderEntity = orderEntity;

CADEC2006, DI, Slide 13 Copyright 2006, Callista Enterprise AB Spring DI Configuration Debugging configuration errors is OK Plugin helps org.springframework.beans.NotWritablePropertyExc eption: Invalid property 'dao' of bean class [se.cadec.impl.OrderEntityImpl]: Bean property 'dao' is not writable or has an invalid setter method: Does the parameter type of the setter match the return type of the getter?

CADEC2006, DI, Slide 14 Copyright 2006, Callista Enterprise AB Bootstrapping – modularization Ear –Web module Bootstrapping servlet spring-config.xml –Xxx.jar spring-config.xml –Yyy.jar spring-config.xml –spring-config1.xml –spring-config2.xml –Zzz.jar spring-config.xml

CADEC2006, DI, Slide 15 Copyright 2006, Callista Enterprise AB Solution – Custom Bootstrapping A help class that finds all spring configuration files on the classpath following a naming convention The bootstrapping servlet calls this help class Each file has references to the actual files used in that jar/war with the import function

CADEC2006, DI, Slide 16 Copyright 2006, Callista Enterprise AB No namespace support All configuration in one file – no problem Each module has its own configuration file – may cause collisions Solution –componentname:beanname <bean id=“ordercomponent:orderEntryService”

CADEC2006, DI, Slide 17 Copyright 2006, Callista Enterprise AB HiveMind solution is better Unlike Spring, HiveMind has built-in namespace support, and fundamentally assumes that the graph of related services will be built from multiple locations (that is, each JAR on the classpath will be packaged with its own XML descriptor). Howard Lewis Ship, the creator of HiveMind and Tapestry explains why tapestry does not use Spring.

CADEC2006, DI, Slide 18 Copyright 2006, Callista Enterprise AB Programming model Which objects should be injected ? What are the alternatives ? –new –static –Factory –JNDI “Stateless Session” Bean obvious, Works very well with stateless singleton-style service-oriented beans. Data Transfer Objects:s - never In the middle – try to singletonize One consistent way makes it easier

CADEC2006, DI, Slide 19 Copyright 2006, Callista Enterprise AB Programming model 2 Static methods –Can not be injected with bean –Can not be mocked new –Can not be injected with beans –Can not be mocked

CADEC2006, DI, Slide 20 Copyright 2006, Callista Enterprise AB Programming model 3 private void doIt() { MyStatefulObject myobj = getANewMyStatefulObject(); myobj.init(thisandthat, somethingelse); myobj.doSomething(); myobj.saveResult(); } private MyStatefulObject getANewMyStatefulObject() { return null; }

CADEC2006, DI, Slide 21 Copyright 2006, Callista Enterprise AB Get new instance on each call Method Injection (Lookup method). Uses bytecode modification We did not use this Solution was to design service-style instead and use singleton beans Non-singleton beans has been used rarely

CADEC2006, DI, Slide 22 Copyright 2006, Callista Enterprise AB Integration The DI container must create all beans Other objects can not have injected references

CADEC2006, DI, Slide 23 Copyright 2006, Callista Enterprise AB Struts Integration DelegatingTilesRequestProcessor One tag in struts config A spring configuration file listing all actions –Action-mapping to bean Works well

CADEC2006, DI, Slide 24 Copyright 2006, Callista Enterprise AB Message Driven Beans EJB:s that is activated by incoming JMS messages Generic MDB class that –Gets the bean id from JNDI –Gets the bean instance by calling the spring applicationContext –Return to Dependency Lookup MDB bean can be injected with references

CADEC2006, DI, Slide 25 Copyright 2006, Callista Enterprise AB Succesfull usage Action, service, entity, dao, helpers, utilities –Everything is injected Properties like –SQL –Config parameters Current time Map Inheritance JNDI to find datasource Reuse action class with parameters

CADEC2006, DI, Slide 26 Copyright 2006, Callista Enterprise AB Testability – Unit test >

CADEC2006, DI, Slide 27 Copyright 2006, Callista Enterprise AB Unit Test new to create implemention class ”inject” mock objects for dependencies –By calling setters Dont neew the full environment Creating exceptional conditions easy Unit Testing and mocking is not trivial –Which test to write ? –Create a lot of dto:s –Some classes are ”perfect” for testing – some not DI makes it less painful

CADEC2006, DI, Slide 28 Copyright 2006, Callista Enterprise AB Integration Test Including –DI container –Config xml files –All dependencies as real classes –Database etc Get Class to test from DI container –beanFactory.getBean(”beanName”) –Introduces a binding to Spring –Return to Dependency Lookup

CADEC2006, DI, Slide 29 Copyright 2006, Callista Enterprise AB Interceptor usage DI enables interceptors out of the box –Requires own ”bean”-factory in non-DI setup Typically NOT application development –More complex requirements –Insert a proxy betwen the calling and called object Sample –Out-of-container transaction/connection support –Declarative transaction demarcation Implemented by using spring Factory Bean concept & explicit proxy class definition

CADEC2006, DI, Slide 30 Copyright 2006, Callista Enterprise AB What about the promises ? No energy waisted to find objects xml format is simple Unit Test easier by mocking dependencies YES ! Non-intrusive DI invisible in beans struts/junit etc integration not easy to replace ”Lighter” than EJB Absolutely

Future & Others

CADEC2006, DI, Slide 32 Copyright 2006, Callista Enterprise AB Spring vs EJB3 EJB3 has dependency injection EJB:s are POJOs Non-EJB classes can NOT be injected –Except resurces and other JNDI items –Many of our beans must be made EJB:s Injection into struts action classes ? In our case – 250 local EJB:s How does a appserver handle 250 EJB:s ? –Websphere ? JBoss/Geronimo/Glassfish etc ? –A appserver must be able to deliver services for an EJB, can it ever be as light-weight ?

CADEC2006, DI, Slide 33 Copyright 2006, Callista Enterprise AB Migration between Spring – EJB3 Least common denominator –No annotations –Setter injection –Everything in xml –Production code platform independent

CADEC2006, DI, Slide 34 Copyright 2006, Callista Enterprise AB EJB2 -> Spring/EJB3

CADEC2006, DI, Slide 35 Copyright 2006, Callista Enterprise AB Spring 2.0 Message-driven POJO:s Enhanced xml configuration AOP, AspectJ integration Others.... Are we moving away from ”simple” ??

CADEC2006, DI, Slide 36 Copyright 2006, Callista Enterprise AB DI future Dependency Injection as a concept is here to stay John Rodson “J2EE without DI” –A title we will probably not see Why bother about simplicity ? We are all professionals right ?

CADEC2006, DI, Slide 37 Copyright 2006, Callista Enterprise AB Is Java too complicated ?

CADEC2006, DI, Slide 38 Copyright 2006, Callista Enterprise AB Ruby sample syntax # Check for pre-formatted string given check=Regexp.new('^'+Regexp.escape (format).gsub(/\\#/,'.')+'$') return string if string =~ check

CADEC2006, DI, Slide 39 Copyright 2006, Callista Enterprise AB Time for Questions!