CS520 Web Programming Spring – Inversion of Control

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)
Introduction to Spring Continued Matt Wheeler. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Java Stack.
Spring, Hibernate and Web Services 13 th September 2014.
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
Lecture 9 Concepts of Programming Languages
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.
An Introduction to Hibernate Matt Secoske
YAGDAO Yet Another DAO Hibernate Guide. yagdao   Mert Can Akkan.
Data Persistence and Object-Relational Mapping Slides by James Brucker, used with his permission 1.
Hibernatification! Roadmap for Migrating from Plain Old SQL on JDBC to JPA on Hibernate Duke Banerjee Senior Developer, DrillingInfo.com.
Rice KRAD Data Layer JPA Design Eric Westfall July 2013.
CSE446 S OFTWARE Q UALITY M ANAGEMENT Spring 2014 Yazılım ve Uyguluma Geliştirme Yöneticisi Orhan Başar Evren.
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.
UPortal Developers MIT August 2004 Persistence Strategy for uPortal 3 Mike DeSimone the r-smart group
Enterprise JavaBeans EJB Container Services. EJB container Enterprise JavaBeans are deployed in an EJB container within the application server EJB container.
IS-907 Java EE JPA: Simple Object-Relational Mapping.
© 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.
Hibernate 3.0. What is Hibernate Hibernate is a free, open source Java package that makes it easy to work with relational databases. Hibernate makes it.
Spring 101 Struts University Series. About Ted Husted Lead author, Struts in Action Co-Author, JUnit in Action Member, Apache Software Foundation Member,
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 ?!?
Kansas City Java User’s Group Jason W. Bedell July 12, 2006
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 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.
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.
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.
Chengyu Sun California State University, Los Angeles
CS520 Web Programming Declarative Security (II)
J2EE Lecture 6: Spring – IoC and Dependency Injection
Structure of a web application
Unit-2 Beans and Containers.
Sakai WebApp Structure
Intro to Spring CJUG - January 2013.
Lecture 9 Concepts of Programming Languages
CS5220 Advanced Topics in Web Programming Spring – Web MVC
Introduction to Spring Framework and Dependency Injection
CS2011 Introduction to Programming I Objects and Classes
CS2011 Introduction to Programming I Arrays (II)
CS5220 Advanced Topics in Web Programming Spring – Web MVC
Tuesday Brown Bag Inversion of Control with Howard Abrams
CS5220 Advanced Topics in Web Programming Angular – Services
Leveraging ColdSpring To Make Better Applications
CS4961 Software Design Laboratory Understand Aquila Backend
Chengyu Sun California State University, Los Angeles
CS5220 Advanced Topics in Web Programming Angular – TypeScript
Chengyu Sun California State University, Los Angeles
Chengyu Sun California State University, Los Angeles
Lecture 9 Concepts of Programming Languages
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 Model class Interface Implementation Usage in application code

Model Class public class User { private Integer id; private String username, password; private boolean enabled; }

DAO Interface public interface UserDao { User getUser( Integer id ); List<User> getUsers(); }

DAO Implementation Implement UserDao using JPA public class UserDaoImpl implements UserDao { private EntityManager entityManger; public User getUser( 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<User> users = userDao.getUsers(); … … }

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

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 1. UserDaoJpaImpl userDao = new UserDaoJpaImpl(); 2. UserDao userDao = new UserDaoJpaImpl(); 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 instantiates UserDao

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

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 objects like DAO, i.e. that “control” is taken way from the application code 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 <beans> <bean id="msgBean“ class=“cs520.spring.hello.Message"> <property name="content" value="Hello World!" /> </bean> </beans> The string “Hello World” is injected to the bean msgBean

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

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

Dependency Injection Objects that can be injected Methods of injection 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 <property>, “name”, “value” Class type property <property>, “name”, “ref” (to another <bean>) Collection type property <list>/<set>/<map>/<props>, <value>/<ref>/<entry>/<prop> Constructor arguments <constructor-arg>, “index”, same as other properties

Some Bean Configuration Examples <property name=“foo”> <map> <entry key=“key1”> <value>bar1</value> </entry> <entry key=“key2”> <ref bean=“bar2” /> </map> </property> <property name=“foo”> <set> <value>bar1</value> <ref bean=“bar2” /> </set> </property> <property name=“foo”> <props> <prop key=“key1”>bar1</prop> <prop key=“key2”>bar2</prop> </props> </property>

Wiring – The Stack Example (I) StackTest

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

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

Annotation-based Configuration Activate annotation processing with <context:annotation-config /> Automatically scan for Spring bean with <context:component-scan /> Mark a class to be a Spring bean with @Component Enable auto wiring with @Autowired

XML Namespace … <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:annotation-config /> <context:component-scan base-package="cs520.spring.stack" /> </beans>

… XML Namespace <?xml version="1.0" encoding="UTF-8"?> <beans:beans xmlns=" http://www.springframework.org/schema/context " xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <annotation-config /> <component-scan base-package="cs520.spring.stack" /> </bean:beans>

<context:annotation-config> Activate the processing of a number of annotations, e.g. @Autowired @Qualifier @Resource @PersistenceContext

<context:component-scan> Scan all the classes under base-package and its sub-packages

Component Scanning @Component for regular bean classes @Repository for DAO classes @Controller for controller classes @Service for service classes

Auto Wiring Auto wire types For individual bean For all beans byName, byType, autodetect, constructor For individual bean <bean autowire=“autowire type”/> For all beans <beans default-autowrire=“autowire type”>

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

Advantages of IoC True separation of different components of an application Centralized bean dependency management Singleton objects improve performance Singleton vs. Prototype

Further Readings Spring in Action Chapter 1-3 Spring Framework Reference Documentation http://static.springsource.org/spring/docs/current/spring-framework-reference/html/ Chapter 5 The IoC Container