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.
Introduction to the Spring Framework University of Kansas January 2009 This presentation and example application are available at
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.
© Internna Technologies 1 IWebMvc Features, Possibilities & Goals.
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.
Introduction to Spring Matt Wheeler. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Java Stack – Basic.
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.
Dependency Injection and Model-View-Controller. Overview Inversion of Control Model-View-Controller.
SPRING Framework Marcin Sztec.
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.
OpusCollege and the use of Spring and iBatis
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.
Dependency Injection in Action Jan Västernäs. CADEC2006, DI, Slide 2 Copyright 2006, Callista Enterprise AB Agenda (DI=Dependency Injection) Background.
Chapter 8 Script-free pages. Problem with scripting in JSP When you use scripting (declaration, scriplet, expressions) in your JSP, you actually put Java.
Text 16 de mayo de 2009 Spring Framework Part I. Dependency Injection Buenos Aires, May 2009.
Anti Orgla, Nortal AS Spring Framework
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
Chính phủ điện tử TS. Phạm Văn Tính Khoa CNTT, ĐH Nông Lâm TP.HCM
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.
Dependency Injection Frameworks Technion – Institute of Technology Author: Assaf Israel - Technion 2013 ©
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.
Text 16 de mayo de 2009 Spring Framework Part III. Portable Service Abstractions Buenos Aires, June 2009.
2006/2007 Licence Apache 2.0 Castle.Igloo. Castle Igloo Basics Pre-require Concept Scopes PageFlow Configuration Controller View Exemple Castle.Igloo.
Fundamentals of MyBATIS
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.
CS520 Web Programming Spring – Inversion of Control Chengyu Sun California State University, Los Angeles.
Spring Fling Phillip Warner. Spring Framework ● Dependency Injection (DI) Framework – “Inversion of Control” ● Facilitates Good Programming Practices.
Intermediate Spring Matt Wheeler.
J2EE Lecture 6: Spring – IoC and Dependency Injection
Structure of a web application
Internationalization
CS520 Web Programming Spring – Inversion of Control
By Ru Shen Department of Computer Science UAlbany, 2008
Intro to Spring CJUG - January 2013.
The Model Layer What is Model?
Leveraging ColdSpring To Make Better Applications
Designing For Testability
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

Overview Learn the basics of the Spring Framework 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 beans 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 Most objects cannot do much by themselves – They depend on other objects to delegate some tasks – For instance if an object wanted to insert something into a database it might depend on another data source object 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 dependency – For instance a data source might be used by many objects to persist data

Inversion of Control (IoC) the Concept Objects define dependencies by: – Constructor arguments – Setter arguments – Arguments to factory method Container injects dependencies when the bean is created Inverse of bean controlling instantiation and/or location of its dependencies

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) { 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) Simplifies component dependency and lifecycle management Eliminates need for: – Calling new or looking up dependencies Decouples code from IoC container Injection is easier – less code – easier to maintain Minimizes need for creational pattern implementation Simplifies testing

Inversion of Control (IoC) Heart of Spring Framework is the IoC container Two basic implementations of the IoC container – ApplicationContext – BeanFactory – BeanFactory is stripped down version of ApplicationContext We will exclusively focus on ApplicationContext

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

DI (setter injection) Say we have to 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 to 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

DI Collections Say our rabbit has many favorite foods public class Rabbit { private Set favoriteFoods; public void setFavoriteFoods(List 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; public Rabbit(String name) { this.name = name; } public void setName(String name) { this.name = name; } //… }

Bean Reference Examples

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

Lab 3: Dependency Injection ab_3_Dependency_Injection

Bean Scopes Where Spring starts to pay dividends Sometimes you want to be able to control the life of a bean – Bean scopes provide this ability By default all beans are singletons

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 Remember these days The new world public static synchronized MySingleton getInstance() { public static MySingleton getInstance() { if (instance == null) { instance = new MySingleton(); } return instance; }

Prototype Scope Equivalent to calling new every time a an instance of a class is needed Spring does not manage he lifecycle of prototype bean The configuration is as follows:

Web application scopes Without Spring you would have to manage bean creation and lifecycles manually

Lab 2: Bean Scopes ab_2_Bean_Scopes

Summary