CS520 Web Programming Spring – Inversion of Control Chengyu Sun California State University, Los Angeles.

Slides:



Advertisements
Similar presentations
Spring Auto wiring Auto wiring Resolves the beans that need to be injected by inspecting the elements in ApplicationContext.
Advertisements

Persistence Jim Briggs 1. 2 Database connectivity: JDBC Java Database Connectivity An API for connecting Java programs (applications, applets and servlets)
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.
JBoss Seam: Contextual Components Jason Bechtel
© 2005, Cornell University. Rapid Application Development using the Kuali Architecture (Struts, Spring and OJB) A Case Study Bryan Hutchinson
Structure of a web application1 Dr Jim Briggs. MVC Structure of a web application2.
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.
Using Third-Party Frameworks in Building Blocks™ David Ashman Principal Architect, Product Development.
An Introduction to Hibernate Matt Secoske
Data Persistence and Object-Relational Mapping Slides by James Brucker, used with his permission 1.
UNIT-V The MVC architecture and Struts Framework.
Spring Roo CS476 Aleksey Bukin Peter Lew. What is Roo? Productivity tool Allows for easy creation of Enterprise Java applications Runs alongside existing.
Hibernatification! Roadmap for Migrating from Plain Old SQL on JDBC to JPA on Hibernate Duke Banerjee Senior Developer, DrillingInfo.com.
CSE446 S OFTWARE Q UALITY M ANAGEMENT Spring 2014 Yazılım ve Uyguluma Geliştirme Yöneticisi Orhan Başar Evren.
SPRING Framework Marcin Sztec.
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.
Java Beans.
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.
April 05 Prof. Ismael H. F. Santos - 1 Modulo II Spring-MVC Controllers Prof. Ismael H F Santos.
The Spring Framework Training Get to know Spring Framework Rohit Prabhakar
© D. Wong  Indexes  JDBC  JDBC in J2EE (Java 2 Enterprise Edition)
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.
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,
Ch 2 – Application Assembly and Deployment COSC 617 Jeff Schmitt September 14, 2006.
Topic : Hibernate 1 Kaster Nurmukan. An ORM tool The problem fixed by ORM Advantage Hibernate Hibernate Basic –Hibernate sessionFactory –Hibernate Session.
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 ©
JAVA EE 6 Best Practices for Migrating Spring to WTF ?!?
Dependency Injection JAVA EE - Spring Framework. Inner Beans As you know Java inner classes are defined within the scope of other classes, similarly,
Kansas City Java User’s Group Jason W. Bedell July 12, 2006
Topic : Hibernate 1 Kaster Nurmukan. An ORM tool Used in data layer of applications Implements JPA.
CS422 Principles of Database Systems Object-Relational Mapping (ORM) Chengyu Sun California State University, Los Angeles.
CS520 Web Programming Object-Relational Mapping with Hibernate and JPA (I) Chengyu Sun California State University, Los Angeles.
CS 440 Database Management Systems Stored procedures & OR mapping 1.
Audit API : Hints and Tricks Mehdi BELMEKKI, Consultancy Team Alfresco.
Creative Commons Attribution- NonCommercial-ShareAlike 2.5 License Sakai Programmer's Café Sakai Montreal CRIM Workshop Introduction to Spring Framework.
CS520 Web Programming Spring – Web MVC Chengyu Sun California State University, Los Angeles.
CS520 Web Programming Spring – Web MVC Chengyu Sun California State University, Los Angeles.
Spring Fling Phillip Warner. Spring Framework ● Dependency Injection (DI) Framework – “Inversion of Control” ● Facilitates Good Programming Practices.
Chengyu Sun California State University, Los Angeles
J2EE Lecture 6: Spring – IoC and Dependency Injection
Structure of a web application
CS520 Web Programming Spring – Inversion of Control
Chengyu Sun California State University, Los Angeles
Sakai WebApp Structure
Intro to Spring CJUG - January 2013.
Chengyu Sun California State University, Los Angeles
CS5220 Advanced Topics in Web Programming Spring – Web MVC
CS2011 Introduction to Programming I Objects and Classes
CS2011 Introduction to Programming I Arrays (II)
CS5220 Advanced Topics in Web Programming Spring – Web MVC
CS5220 Advanced Topics in Web Programming Angular – Services
CS520 Web Programming Spring – Aspect Oriented Programming
Leveraging ColdSpring To Make Better Applications
CS4961 Software Design Laboratory Understand Aquila Backend
Chengyu Sun California State University, Los Angeles
CS3220 Web and Internet Programming About Data Models
CS5220 Advanced Topics in Web Programming Angular – Services
Chengyu Sun California State University, Los Angeles
Chengyu Sun California State University, Los Angeles
Presentation transcript:

CS520 Web Programming Spring – Inversion of Control Chengyu Sun California State University, Los Angeles

Background Originally developed by Rod Johnson Addresses many problems of EJB One of the most popular Java web development frameworks Books Expert One-on-One: J2EE Design and Development (2002) Expert One-on-One: J2EE Development without EJB (2004) Professional Java Development with the Spring Framework (2005)

Spring Framework

The Need for IoC The DAO Example The Data Access Object (DAO) pattern UserDao Example  Interface  Implementation  Usage in application code

Data Access Object (DAO) A Java EE design pattern controller user model view DAO Persistent Data Store Application code

DAO Interface public interface UserDao { User getUserById( Integer id ); List getAllUsers(); }

DAO Implementation Implement UserDao using JPA public class UserDaoImpl implements UserDao { private EntityManager entityManger; public User getUserById( Integer id ) { return entityManager.find(User.class, id ); } }

DAO Usage in Application Code UserController public class UserController { UserDao userDao; public String users( ModelMap models ) { List users = userDao.getAllUsers(); … … }

Advantages of DAO Provide a data access API that is Independent of persistent storage types, e.g. relational DB, OODB, XML flat files etc. Independent of persistent storage implementations, e.g. MySQL, PostgreSQL, Oracle etc. Independent of data access implementations, e.g. JDBC, Hibernate, etc.

Instantiate a UserDao Object in Application Code UserDaoJpaImpl userDao = new UserDaoJpaImpl(); 1. UserDao userDao = new UserDaoJpaImpl(); 2. Which one is better??

Problem Caused by Object Instantiation What if we decide to use JDBC instead of Hibernate/JPA, i.e. replace UserDaoJpaImpl with UserDaoJdbcImpl The application is not really independent of the data access method Switching to a different UserDao implementation affects all the code that uses UserDao

Another Way to Instantiate UserDao No more dependency on a specific implementation of the DAO But who will call the setter? UserDao userDao;... public void setUserDao( UserDao userDao) { this.userDao = userDao; }

Inversion of Control (IoC) A framework like Spring is responsible for instantiating the objects and pass them to application code A.K.A. IoC container, bean container Inversion of Control (IoC) The application code is no longer responsible for instantiate an interface with a specific implementation A.K.A. Dependency Injection

Example: Hello World Message is a Java object (or bean) managed by the Spring container Created by the container Property is set by the container

Bean Configuration File The string “Hello World” is injected to the bean msgBean <bean id="msgBean“ class=“cs520.spring.hello.Message">

Understand Bean Container … Without a bean container Application Code JVM new Message() Message object

… Understand Bean Container With a bean container Application Code JVM new Message() Message object getBean(“msgBean”) <bean id="msgBean“ class="cs520.spring.hello.Message"> <property name="message“ value="Hello World!" /> setMessage(“Hello World”); Bean Container

Dependency Injection Objects that can be injected Simple types: strings and numbers Collection types: list, set, and maps Other beans Methods of injection via Setters via Constructors

Dependency Injection Example DjBean Fields of simple types Fields of collection types Fields of class types

Quick Summary of Bean Configuration Bean, “id”, “class” Simple type property, “name”, “value” Class type property, “name”, “ref” (to another ) Collection type property / / /, / / / Constructor arguments, “index”, same as other properties

Some Bean Configuration Examples bar1 bar1 bar1 bar2

Wiring – The Stack Example (I) StackTest Stack1

Wiring – The Stack Example (II) StackTest Stack2 ArrayList LinkedList

Wiring – The Stack Example (III) StackTest Stack2 ArrayList LinkedList

Annotation-based Configuration Activate annotation processing with Automatically scan for Spring bean with Mark a class to be a Spring bean Enable auto wiring

XML Namespace … <beans xmlns=" xmlns:xsi=" xmlns:context=" xsi:schemaLocation="

… XML Namespace <beans:beans xmlns=" " xmlns:xsi=" xmlns:beans=" xsi:schemaLocation="

Activate the processing of a number of

Component for regular bean for DAO for controller for service classes

Auto Wiring Auto wire types byName, byType, constructor, autodetect For individual bean For all beans

@Autowired The property does not need a setter Auto wired by type To auto wire by name

Example: springmvc Step 2. Spring MVC Step 3. Hibernate

Advantages of IoC Separate application code from service implementation Centralized dependency management with a bean configuration file Singleton objects improve performance Singleton vs. Prototype

Further Readings Spring in Action (3 rd Ed) Chapter 1-3 Spring Framework Reference Documentation rent/spring-framework-reference/html/ rent/spring-framework-reference/html/ Chapter 5 The IoC Container