Dependency injection when you only have one dependency JavaZone Johannes Brodwall, Recovering Spring User Steria Norway.

Slides:



Advertisements
Similar presentations
Creational Design Patterns. Creational DP: Abstracts the instantiation process Helps make a system independent of how objects are created, composed, represented.
Advertisements

Lightning Talk by Ted Young. What is Integration Testing?
CLASS INHERITANCE Class inheritance is about inheriting/deriving properties from another class. When inheriting a class you are inheriting the attributes.
And so to Code. Forward, Reverse, and Round-Trip Engineering Forward Engineering Reverse Engineering Round-Trip Engineering.
Johannes Brodwall Exilesoft
Do I still hate SOA? JavaZone Johannes Brodwall, Steria Organized Architect Steria Norway.
 2002 Prentice Hall. All rights reserved. Chapter 9: Servlets Outline 9.1 Introduction 9.2 Servlet Overview and Architecture Interface Servlet and.
Intermediate Spring Matt Wheeler. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Java Stack – Basic.
Advanced Java Server Pages An more detailed look at JSPs.
Unit Testing Discussion C. Unit Test ● public Method is smallest unit of code ● Input/output transformation ● Test if the method does what it claims ●
Servlets Compiled by Dr. Billy B. L. Lim. Servlets Servlets are Java programs which are invoked to service client requests on a Web server. Servlets extend.
Unit Testing Tips and Tricks: Database Interaction Louis Thomas.
1 November 21st 2009 Shaun Abram An Introduction to Spring.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 17 Animated Version Generics and Type Safety.
Intermediate Spring Matt Wheeler. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Java Stack – Basic.
Introduction to the Spring Framework Rajesh. Spring Mission Statement J2EE should be easier to use OO design is more important than any implementation.
Anti Orgla, Nortal AS Spring Framework
Web Server Programming 1. Nuts and Bolts. Premises of Course Provides general introduction, no in-depth training Assumes some HTML knowledge Assumes some.
Chapter 26 GoF Design Patterns. The Adapter Design Pattern.
CHEF II / Sakai Architecture. CHEF II Changes uPortal replaces Jetspeed –jsr 168 portlet, servlet compliant Spring replaces Turbine component framework.
Introduction to Spring Matt Wheeler. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Java Stack – Basic.
Threading Servlets Can handle multiple clients concurrently Shared resources must be synchronized or create a servlet that handles one request at a time.
Spring 101 Struts University Series. About Ted Husted Lead author, Struts in Action Co-Author, JUnit in Action Member, Apache Software Foundation Member,
Copyright 2008 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Module Name Subtitle.
Singleton and Basic UML CS340100, NTHU Yoshi. What is UML Unified Modeling Language A standardized general-purpose modeling language in the field of software.
Presentation & Business Tier Design Patterns Pearce.
Introduction to Server-Side Web Development Introduction to Server-Side Web Development Session II: Introduction to Server-Side Web Development with Servlets.
Intermediate Spring Matt Wheeler. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Java Stack – Basic.
Servlet Filters import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class TimerFilter implements Filter { /* J2EE v1.3 Filter.
Copyright © 2002 ProsoftTraining. All rights reserved. Java Servlets.
JUnit Eclipse, Java and introduction to Junit. Topics Covered  Using Eclipse IDE  Example Java Programs  Junit Introduction.
Topic : Hibernate 1 Kaster Nurmukan. An ORM tool The problem fixed by ORM Advantage Hibernate Hibernate Basic –Hibernate sessionFactory –Hibernate Session.
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Spring MVC Essentials Getting started.
Java EE - Dependency Injection -
CSI 3125, Preliminaries, page 1 SERVLET. CSI 3125, Preliminaries, page 2 SERVLET A servlet is a server-side software program, Responds oriented other.
Mark Dixon 1 11 – Java Servlets. Mark Dixon 2 Session Aims & Objectives Aims –To cover a range of web-application design techniques Objectives, by end.
Dependency Injection Frameworks Technion – Institute of Technology Author: Assaf Israel - Technion 2013 ©
The Factory Method Pattern (Creational) ©SoftMoore ConsultingSlide 1.
Session Beans Based on: Patel, Brose, Silverman, Mastering Enterprise JavaBeans 3.0.
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Introduction to Data Access with Spring.
1 More OO Design Patterns CSC 335: Object-Oriented Programming and Design.
Topic : Hibernate 1 Kaster Nurmukan. An ORM tool Used in data layer of applications Implements JPA.
Java Programming Persistent Data Types. Persistent Data Structure A persistent data structure is a data structure having an internal state that never.
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Introduction to Spring JDBC Simplifying.
1 Creational Design Patterns CSC 335: Object-Oriented Programming and Design.
Multi-purpose tests (Cool tricks with JUnit) JavaZone 2012 Johannes Brodwall, Principal Architect Steria
.  A reference model developed for an important use case (Air Job) including UI widgets, controllers, services and DAOs.  After full requirement analysis.
CS 350 – Software Design The Decorator Pattern – Chapter 17 In this chapter we expand our e-commerce case study and learn how to use the Decorator Pattern.
Dependency Injection with Guice Technion – Institute of Technology Author: Gal Lalouche - Technion 2016 ©
Test Java EE applications with Arquillian Ivan St. Ivanov.
CS 562 Advanced Java and Internet Application Computer Warehouse Web Application By Team Alpha :-  Puja Mehta (102163)  Mona Nagpure (102147)
Andres Käver, IT Kolledž public interface IPersonRepository : IDisposable { IQueryable All { get; } IQueryable AllIncluding( params Expression.
1 Creational Design Patterns CSC 335: Object-Oriented Programming and Design.
CS520 Web Programming Spring – Inversion of Control Chengyu Sun California State University, Los Angeles.
Spring Filters Spring Interceptors SoftUni Team Spring Interceptors
MPCS – Advanced java Programming
Java Servlets By: Tejashri Udavant..
Dependency Injection Andres Käver, IT College 2016/2017 Spring.
Factory pattern Unit of Work
Computer Science 209 Testing With JUnit.
In Class Assg 4 - Solution
null, true, and false are also reserved.
CS5220 Advanced Topics in Web Programming Spring – Web MVC
In Class Assg 2 - solution
CSE 432 Presentation GoF: Factory Method PH: “To Kill a Singleton”
Repository pattern Andres Käver, IT Kolledž 2016/2017 Spring.
CSE 1030: Data Structure Mark Shtern.
CS 350 – Software Design Singleton – Chapter 21
Basic servlet structure
Dependency Injection Mechanism
Presentation transcript:

Dependency injection when you only have one dependency JavaZone Johannes Brodwall, Recovering Spring User Steria Norway

Let’s get rid of dogmatic dependency injection

Does this look familiar?

Person- Controller Person- Controller- Impl Person- Service Person- ServiceImpl Person- Repository Impl Person- Repository Impl PersonDao Impl PersonDao Impl Session- Factory

Customer Invoice Order Product

No more: this.personService = new PersonServiceImpl(sessionFactory)

Instead:

private PersonService personService;

Why?

(dear God, why?!)

Testing

Multiple implementations

(Really?)

Configuration

(Often one)

Ordnung muss sein!

(Ordnung muss sein!) Hobgoblin of little minds - Ralph Waldo Emerson

Alternative

Session- Factory PersonController Service Repo sitory

public class PersonController { private PersonService public PersonController(SessionFactory sf) { this.personService = new PersonServiceImpl(sf); }

public class PersonController { private PersonService public PersonController(SessionFactory sf) { this.personService = new PersonServiceImpl(sf); } public class PersonServiceImpl implements … { private PersonRepository personRepo; public PersonServiceImpl(SessionFactory sf) { this.personRepo = new PersonRepositoryImpl(sf); }

public class PersonController { private PersonService public PersonController(SessionFactory sf) { this.personService = new PersonServiceImpl(sf); } public PersonControllerImpl(PersonService ps) { this.personService = ps; }

public class PersonController { private PersonService public PersonController(SessionFactory sf) { this.personService = new PersonServiceImpl(sf); } public PersonControllerImpl(PersonService ps) { this.personService = ps; } For Spring For mocking

SPRING! Session- Factory PersonController Service Repo sitory InvoiceController Repository FooServiceImpl ReportsController FooController

Look ma! No Spring!

Session- Factory PersonServlet Locator (singleton O_O) Locator (singleton O_O) ”Injected” by servlet

public class PersonController extends HttpServlet { private PersonService personService; public PersonController() { } public PersonController(PersonService personService) { this.personService = personService; public void init() throws ServletException { SessionFactory sf = HibernateLookup.getInstance(getServletContext()); this.personService = new PersonServiceImpl(sf); }

public class PersonController extends HttpServlet { private PersonService personService; public PersonController() { } public PersonController(PersonService personService) { this.personService = personService; public void init() throws ServletException { SessionFactory sf = HibernateLookup.getInstance(getServletContext()); this.personService = new PersonServiceImpl(sf); } For mocking Look, ma! No Spring!

Session- Factory PersonController Service Repo sitory InvoiceController Repository FooServiceImpl ReportsController FooController >

Bonus: Generic Repository

public interface Repository { T retrieve(Class type,Serializable id); Serializable save(Object object); List find(Specification spec); Transaction beginTransaction(); }

@RunWith(RepositoryTestRunner.class) public class PersonControllerTest { private Repository repository; private PersonController personController; public PersonControllerTest(Repository repo) { this.repository = repo; this.personController = new PersonController(repo); }

@Test public void should_show_person() { Person person = new Person(); Long id = (Long) repository.save(person); ModelAndView show = personController.show(id); assertEquals(person, show.getModel().get("person")); }

public class RepoTestRunner extends Suite { public RepoTestRunner(Class testClass) { super(testClass, createRunners(testClass)); } static List createRunners(Class testClass) { List runners = new ArrayList (); runners.add(testRunner(testClass, new HashMapRepository())); if (!isRunningInInfinitest()) { runners.add(testRunner(testClass, hibernateRepository())); } return runners; }

What Spring taught me

Be aware of dependencies!

Avoid differences between test and prod

Stay the heck away from frameworks!

Collapse service chains

Takk for meg!