Introduction to Spring Matt Wheeler. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Java Stack – Basic.

Slides:



Advertisements
Similar presentations
Introduction to Spring Continued Matt Wheeler. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Java Stack.
Advertisements

Intermediate Spring Matt Wheeler. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Java Stack – Basic.
Spring, Hibernate and Web Services 13 th September 2014.
Functional Testing with the Java Stack Test Runner
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.
Intermediate Spring Matt Wheeler. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Java Stack – Basic.
JBoss Seam: Contextual Components Jason Bechtel
Structure of a web application1 Dr Jim Briggs. MVC Structure of a web application2.
Introduction to Spring Continued Matt Wheeler. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Java Stack.
LDS Account and the Java Stack. Disclaimer This is a training NOT a presentation. – Be prepared to learn and participate in labs Please ask questions.
Address - #22, 1 st Floor, Station View Road, Kodambakkam, Chennai JTech Soft Solutions Website:
1 November 21st 2009 Shaun Abram An Introduction to Spring.
The Spring Framework: A brief introduction to Inversion of Control James Brundege
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.
Dependency Injection and Model-View-Controller. Overview Inversion of Control Model-View-Controller.
Intermediate Spring Matt Wheeler. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Java Stack – Basic.
Intermediate Spring Matt Wheeler. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Java Stack – Basic.
Intermediate Spring Matt Wheeler. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Java Stack – Basic.
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.
Introduction to the Spring Framework Rajesh. Spring Mission Statement J2EE should be easier to use OO design is more important than any implementation.
Internationalization and the Java Stack Part 1 Matt Wheeler.
Copyright © 2012 Accenture All Rights Reserved.Copyright © 2012 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are.
Internationalization and the Java Stack Matt Wheeler.
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Understanding the Bean Lifecycle An.
Introduction to Spring (part 2) Matt Wheeler. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Java Stack.
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.
Chapter 8 Script-free pages. Problem with scripting in JSP When you use scripting (declaration, scriplet, expressions) in your JSP, you actually put Java.
Internationalization in the Java Stack Matt Wheeler.
Internationalization in the Java Stack Matt Wheeler.
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
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Introduction to the Java Stack Michael Youngstrom.
Struts 2 introduction. Struts 2 framework Struts 2 A full-featured web application framework for the Java EE platform The Java Servlet API exposes the.
Intermediate Spring Matt Wheeler. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Java Stack – Basic.
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.
Kansas City Java User’s Group Jason W. Bedell July 12, 2006
1 Java Server Pages A Java Server Page is a file consisting of HTML or XML markup into which special tags and code blocks are inserted When the page is.
2006/2007 Licence Apache 2.0 Castle.Igloo. Castle Igloo Basics Pre-require Concept Scopes PageFlow Configuration Controller View Exemple Castle.Igloo.
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Overview of the Spring Framework Introducing.
Fundamentals of MyBATIS
YG - CS Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally.
Internationalization and the Java Stack Part 2 Matt Wheeler.
Singleton Academy, Pune. Course syllabus Singleton Academy Pune – Course Syllabus1.
Introduction to Inversion Of Control (IOC). IOC Definition (based on Wikipedia)  Consider the way in which an object obtains references to its dependencies.
Creative Commons Attribution- NonCommercial-ShareAlike 2.5 License Sakai Programmer's Café Sakai Montreal CRIM Workshop Introduction to Spring Framework.
Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++
CS520 Web Programming Spring – Web MVC Chengyu Sun California State University, Los Angeles.
CS520 Web Programming Spring – Inversion of Control Chengyu Sun California State University, Los Angeles.
Intermediate Spring Matt Wheeler.
J2EE Lecture 6: Spring – IoC and Dependency Injection
Structure of a web application
Internationalization
Spring Boot Introduction
CS520 Web Programming Spring – Inversion of Control
By Ru Shen Department of Computer Science UAlbany, 2008
Intro to Spring CJUG - January 2013.
Building an Integrable XBRL Portal Daniel Hamm German Central Bank
The Model Layer What is Model?
Leveraging ColdSpring To Make Better Applications
Testing your DAO’s with Java
Presentation transcript:

Introduction to Spring Matt Wheeler

Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Java Stack – Basic Java and XML skills – Installed LdsTech IDE (or other equivalent – good luck there ;)

Overview Become familiar with Spring documentation Learn basics of the Spring architecture Learn about bean definition and creation Learn about the application context Inversion of Control (IoC) Dependency Injection (DI) Learn about bean scopes

Goals of the Spring Framework Simplify Java EE development Solve problems not addressed by Java EE Provide simple integration for best of breed technologies Provide modular/pluggable architecture – Use what you want – don’t use what you don’t

Explore the Spring Ecosystem Main Page: Documentation: Forum: Jira: spa

Spring Framework Modified from

Data Access Integration Data Access Integration layer includes: – JDBC (abstraction layer over native JDBC) – ORM (integration support for JPA, Hibernate, …) – Transaction support (declarative and programmatic)

Web Basic web integration features – File upload – Initialization of IoC container using servlet listeners – Contains Spring’s model view controller (MVC) implementation

Test Test module provides integration with test frameworks – JUnit – TestNG Provides ability to load test specific ApplicationContexts Also provides helpful mock objects

Core Container Core and Beans modules provide framework fundamentals – Inversion of Control (IoC) and Dependency Injection (DI) – BeanFactory provides factory pattern implementation for creating Java objects – Allows decoupling of configuration and dependencies from actual code

Spring Container / Application Context Spring container / application context contains instances of objects Spring managed object instances are called beans Spring manages the creation, configuration, and destruction of these beans Beans are defined for use in the application context – The definitions are a template for creating new bean instances Bean definitions can be provided to Spring: – In xml – Using annotations – In Java code

Defining Beans ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); SomeBean someBean = context.getBean(SomeBean.class); someBean.callMethod(); Bean definition (beans.xml) Application Context

Defining beans Each bean has a unique id – If none is provided, Spring will create one – Class should contain fully qualified package and name of the object

Defining Beans ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); SomeBean someBean = context.getBean("someBean", SomeBean.class); someBean.callMethod(); Bean definition (beans.xml) Application Context

Lab 1: Bean Creation ab_1_Bean_Configuration

Dependency Injection Dependency Injection (DI) & Inversion of Control (IoC)

Object Dependencies This is where your Spring investment starts to pay dividends Most objects cannot do much by themselves – They depend on other objects to accomplish some tasks – For instance if an object wanted to insert something into a database it will need a connection to accomplish this Most objects have properties that should be configurable – For example if you have an object that times out after a certain period, you might want to be able to configure the timeout period Additionally, what if two objects need the same object dependency – For instance a data source might be used by many objects to persist data

Inversion of Control (IoC) the Concept Container injects dependencies when (or right after) the bean is created Inverse of bean controlling instantiation and/or location of its dependencies Objects instead obtain dependencies through – Constructor arguments – Setter arguments

Inversion of Control (IoC) the Concept Managing dependencies on other beans – Dependency lookup vs. Dependency Injection //dependency lookup public class Lookup { private SomeBean someBean; public SomeBean findBean(Container container) { //or new SomeBean( " some param values " ); return (SomeBean) container.getBean( " someBean " ); } //dependency injection public class Injection { private SomeBean someBean; public void setSomeBean(SomeBean someBean) { this.someBean = someBean; }

Advantages of Inversion of Control (IoC) Separates configuration from code Simplifies component dependency and lifecycle management Eliminates need for: – Calling new and managing dependencies – Looking up dependencies Decouples code from IoC container Injection is easier – less code – easier to maintain Minimizes need for creational pattern implementation Simplifies testing

Dependency Injection (DI) Two basic types of injection – Setter injection – Constructor injection

DI (setter injection) Say we have the following Rabbit class Example public class Rabbit { private String favoriteFood; public void setFavoriteFood(String favoriteFood) { this.favoriteFood = favoriteFood; } public void printFavoriteFood() { System.out.println(favoriteFood); }

DI (constructor injection) Say we have the following Rabbit class Example public class Rabbit { private String favoriteFood; public Rabbit(String favoriteFood) { this.favoriteFood = favoriteFood; } public void printFavoriteFood() { System.out.println(favoriteFood); }

DI (continued) Ability to inject many data types – Lists, Sets, Properties, Maps (most collection types) – Other beans Lets us look at a few examples: – ng-framework-reference/html/beans.html#beans- collection-elements

Constructor Injection vs. Setter Injection Which should you use? Rule of thumb – write the bean as though Spring were not managing it – If the bean will not operate without a particular member variable use constructor injection – Otherwise use setter injection – This is a change from the past Let’s look at some more examples

DI Collections Say our rabbit has many favorite foods public class Rabbit { private Set favoriteFoods; public void setFavoriteFoods(Set favoriteFoods) { this.favoriteFoods = favoriteFoods; } public void printFavoriteFood() { for (String favoriteFoods : favoriteFood) { System.out.println(favoriteFood); } lettuce carrot

DI Bean References Lets expand our rabbit concept to an entire farm And then modify our rabbit class as follows public class Farm { private List rabbits; public void setRabbits(List rabbits) { this.rabbits = rabbits; } public class Rabbit { private String name; private float weight; public Rabbit(float weight) { this.weight = weight; } public void setName(String name) { this.name = name; } //… }

Bean Reference Examples

More Examples public class Farm { private Rabbit prizeRabbit; public void setPrizeRabbit(Rabbit prizeRabbit) { this.prizeRabbit = prizeRabbit; }

Lab 2: Dependency Injection ab_2_Dependency_Injection

Bean Scopes Sometimes you want to be able to control the life of a bean – Bean scopes provide this ability For instance, you may want a new instance of a bean every time it is requested, or you may want one instance for the entire application

Available Bean Scopes ScopeDocumentation singletonCreates a single bean instance per IoC container (default) prototypeCreates a new instance every time a bean of the type is requested requestCreates a single instance per HTTP request. Only valid in a web application context. sessionCreates a single instance per HTTP session. Only valid in a web application context. globalSessionCreates a single instance per HTTP session. Only valid in a portal application.

Singleton Scope Singleton - only one instance in a given context Any time you request an instance of this bean you will get the single instance of this bean For instance (if SomeBean is a singleton): The new world context.getBean(SomeBean.class) == context.getBean(SomeBean.class) is true

Prototype Scope Equivalent to calling new every time an instance of a class is needed Spring does not manage the lifecycle of prototype beans For instance if SomeBean is prototype scope: The configuration is as follows: context.getBean(SomeBean.class) == context.getBean(SomeBean.class) is false

Lab 3: Bean Scopes ab_3_Bean_Scopes

Credit where credit is due Spring Recipies 2 nd Edition (Gary Mak, Josh Long and Daniel Rubio)

Dedication Mike Youngstrom and Bruce Campbell for their undying love and support through this process – And for reviewing and offering suggestions And for Spencer Uresk and Mike Heath for the inspiration And a special thanks to the rabbit farm who made this all possible