The Spring Framework: A brief introduction to Inversion of Control James Brundege www.synaptocode.com.

Slides:



Advertisements
Similar presentations
Introduction to the Spring Framework
Advertisements

Spring, Hibernate and Web Services 13 th September 2014.
Introduction to the Spring Framework University of Kansas January 2009 This presentation and example application are available at
Introduction to Spring Matt Wheeler. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Java Stack – Basic.
Spring.Net Steinar Dragsnes steinar.dragsnes at viz.no.
© Internna Technologies 1 IWebMvc Features, Possibilities & Goals.
1 November 21st 2009 Shaun Abram An Introduction to Spring.
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.
Introduction to Spring Matt Wheeler. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Java Stack – Basic.
Using Third-Party Frameworks in Building Blocks™ David Ashman Principal Architect, Product Development.
An Introduction to Hibernate Matt Secoske
The Spring Framework A quick overview. The Spring Framework 1. Spring principles: IoC 2. Spring principles: AOP 3. A handful of services 4. A MVC framework.
Spring Framework Adam Waldal Senior Consultant. About me..  OPI is a leader in J2EE consulting with Relationships with BEA, IBM, Tibco, and many other.
UNIT-V The MVC architecture and Struts Framework.
2007 JavaOne SM Conference | Session BOF-4181 | BOF-4181 Migrating a Spring/Hibernate Application to Java Platform, Enterprise Edition (Java EE) 5 Adrian.
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.
Copyright 2006, Codeguild, Inc Spring Framework Fundamentals March, 2006 Larry Hamel Codeguild, Inc.
Spring Overview, Application demo -Midhila Paineni 09/23/2011 Spring Overview, Application demo9/8/20151.
Spring. Spring Overview Spring Container Concepts Spring and AOP Spring and Data Access Managing Transactions and Resources Remoting and Accessing Enterprise.
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.
Copyright © 2005 Finetix LLC All Rights Reserved 0 SpringFramework.Java Developer Session Solomon Duskis The Peer Frameworks Series -.Net and Java.
OpusCollege and the use of Spring and iBatis
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.
Dependency Injection in Action Jan Västernäs. CADEC2006, DI, Slide 2 Copyright 2006, Callista Enterprise AB Agenda (DI=Dependency Injection) Background.
JBoss at Work JAW Motors Application Chapter 1-3 Jeff Schmitt October 9, 2006.
Text 16 de mayo de 2009 Spring Framework Part I. Dependency Injection Buenos Aires, May 2009.
Opus College - overview. OpusCollege - background First project: ICT Capacity Building Mozambican Higher Education Institutions Partners: RUG Groningen,
DEPENDENCY INJECTION & INVERSION OF CONTROL. WHAT’S GOING TO BE COVERED Quick intro to C# for Java developers Dependency Injection Inversion of Control.
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.
Spring Data Access By, Srinivas Reddy.S
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
JBoss at Work Databases and JBoss Chapter 4 Jeff Schmitt October 26, 2006.
A Presentation By V AIBHAV S AHARAN Web-enHanced Information Management COMS E6125.
Spring Framework. About spring Spring is the most popular application development framework for enterprise Java. Millions of developers around the world.
Enterprise Java Overview Panos Konstantinidis Java Developer JUG Leader & Java Champion
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.
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.
JAVA EE 6 Best Practices for Migrating Spring to WTF ?!?
SCA and Java n SCA provides support for multiple Java-based component implementation types l Java POJOs (implementation.java) l Spring Beans (implementation.spring)
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
Give a few Reasons for using Java?
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. Overview of the Spring Framework Introducing.
Developing with the Framework Zach A. Thomas, Texas State University
.  A reference model developed for an important use case (Air Job) including UI widgets, controllers, services and DAOs.  After full requirement analysis.
Text 16 de mayo de 2009 Spring Framework Part III. Portable Service Abstractions and Spring MVC Buenos Aires, June 2009.
Google Code Libraries Dima Ionut Daniel. Contents What is Google Code? LDAPBeans Object-ldap-mapping Ldap-ODM Bug4j jOOR Rapa jongo Conclusion Bibliography.
Creative Commons Attribution- NonCommercial-ShareAlike 2.5 License Sakai Programmer's Café Sakai Montreal CRIM Workshop Introduction to Spring Framework.
Leveraging ColdSpring to build a robust Flex applications Chris Scott, Cynergy Systems.
CS520 Web Programming Spring – Inversion of Control Chengyu Sun California State University, Los Angeles.
Training What’s Spring? The leading full-stack Java/JEE application framework.
J2EE Lecture 6: Spring – IoC and Dependency Injection
A very brief introduction
CS520 Web Programming Spring – Inversion of Control
By Ru Shen Department of Computer Science UAlbany, 2008
Intro to Spring CJUG - January 2013.
Corso di “Sviluppo di applicazioni Web”
Introduction to Spring Framework and Dependency Injection
Tuesday Brown Bag Inversion of Control with Howard Abrams
Leveraging ColdSpring To Make Better Applications
Designing For Testability
Presentation transcript:

The Spring Framework: A brief introduction to Inversion of Control James Brundege

What is Spring? 2 Things: ◦ An Inversion of Control (IoC) Container ◦ Utilities that provide a consistent (and simple!) API to many other technologies (JDBC, ORM, AOP, Declarative Transactions, etc)

Guiding Principles of Spring ◦ Minimize dependencies on Spring ◦ Minimize dependencies between all layers of an application. ◦ All application code should be testable, without an application server or other complex environment ◦ Fully factor the APIs (the 90% case should be accomplished in one line of code!)

What is Inversion of Control (IoC)? (besides yet another confusing term for a simple concept) IoC is all about Object dependencies. Traditional "Pull" approach: ◦ Direct instantiation ◦ Asking a Factory for an implementation ◦ Looking up a service via JNDI "Push" approach: ◦ Something outside of the Object "pushes" its dependencies into it. The Object has no knowledge of how it gets its dependencies, it just assumes they are there. The "Push" approach is called "Dependency Injection".

The BookDemo Application: (A simple CRUD app) User Interface Service Layer Data Access Layer DB Domai n Model

Pull Example public class BookDemoServicePullImpl implements BookDemoService { public void addPublisherToBook(Book book) { BookDemoFactory factory = BookDemoFactory.getFactory(); BookDemoDao dao = factory.getBookDemoDao(); String isbn = book.getIsbn(); if (book.getPublisher() == null && isbn != null) { Publisher publisher = dao.findPublisherByIsbn(isbn); book.setPublisher(publisher); }

Push Example (Dependency Injection) public class BookDemoServiceImpl implements BookDemoService { private BookDemoDao dao; public void addPublisherToBook(Book book) { String isbn = book.getIsbn(); if (book.getPublisher() == null && isbn != null) { Publisher publisher = dao.findPublisherByIsbn(isbn); book.setPublisher(publisher); } public void setBookDemoDao(BookDemoDao dao) { this.dao = dao; } }

BookDemoService Unit Test

Definitions Dependency Injection is the act of injecting dependencies into an Object. Inversion of Control is the general style of using Dependency Injection to wire together application layers. Hence Spring is an Inversion of Control container. That is, it is a container that handles Dependency Injection for you.

Why is Dependency Injection better? 2 reasons: ◦ Loose Coupling ◦ Testability Loose Coupling is improved because you don't hard-code dependencies between layers and modules. Instead you configure them outside of the code. This makes it easy to swap in a new implementation of a service, or break off a module and reuse it elsewhere. Testability is improved because your Objects don't know or care what environment they're in as long as someone injects their dependencies. Hence you can deploy Objects into a test environment and inject Mock Objects for their dependencies with ease.

How Spring does Inversion of Control ◦ Write a configuration file in which you name concrete "beans" for the interfaces between your layers. ◦ "Wire" the application together by stating which beans are dependent on each other. ◦ Instantiate a Spring object called an ApplicationContext. This is a type of bean factory that will instantiate all your other beans and handle dependency injection.

Example Spring applicationContext.xml <bean id="bookDemoDao" class="com.bookdemo.dao.hibernate.BookDemoDaoHibernateImpl"> <bean id="bookDemoService" class="com.bookdemo.service.impl.BookDemoServiceImpl">

Bootstapping the IoC container To start an app using IoC: ◦ Create an ApplicationContext object and tell it where applicationContext.xml is. ApplicationContext appContext = new ClassPathXmlApplicationContext("classpath*:applicationContext.xml"); This just has to be done once on startup, and can be done in the main() method or whatever code bootstraps the application.

Bootstapping the IoC container The appContext holds a single copy of each bean declared in applicationContext.xml, so you could ask the Context for any bean by name: MyService service = (MyService)appContext.getBean("myService"); But don't! That is a "Pull" technique that treats the ApplicationContext like a Factory. Instead, make sure that applicationContext.xml connects every bean to every other bean that needs it. None of the beans thus have a dependency on spring.jar

Bootstapping the IoC container For web applications the situation is simpler: Web applications are bootstrapped by the web container based on the web.xml file. Hence creating an ApplicationContext on startup is as simple as a single declaration in web.xml: org.springframework.web.context.ContextLoaderListener

A Spring app with no dependencies on Spring? When bootstrapping spring from web.xml, there are only two pieces: ◦ The applicationContext.xml file ◦ A single tag within web.xml Not a single line of Java code! Therefore, not one of your custom classes has a dependency on spring.jar

The BookDemo App

What else has Spring got? Spring provides either implementations or fully-factored API wrappers over these technologies: ◦ JDBC and DAOs ◦ ORM: Hibernate, iBatis, TopLink and others ◦ Declarative Transaction Support (without a full J2EE app server) ◦ Aspect-Oriented Programming ◦ Remote calls and Web Services (Axis) ◦ EJBs

Resources Spring Website: Download Spring with sample applications: dependencies.zip?download Rod Johnson's book on Spring: Wikipedia: EasyMock: Echo2 (Rich Web Inteface framework)