Text 16 de mayo de 2009 Spring Framework Part III. Portable Service Abstractions Buenos Aires, June 2009.

Slides:



Advertisements
Similar presentations
1 juni 30, 2005 Spring JDBC. 2 juni 30, 2005 Doel Database acties Flexibel Gecontroleerd Productief.
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.
An architecture for webb applications, J2EE
Enterprise Applications & Java/J2EE Technologies Dr. Douglas C. Schmidt Professor of EECS.
© 2005, Cornell University. Rapid Application Development using the Kuali Architecture (Struts, Spring and OJB) A Case Study Bryan Hutchinson
The Spring Framework TM Rod Johnson Interface21. Spring framework goals Make J2EE easier to use Address end-to-end requirements rather than one tier Eliminate.
The Spring Framework: A brief introduction to Inversion of Control James Brundege
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.
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.
Creative Commons Attribution- NonCommercial-ShareAlike 2.5 License Sakai Programmer's Café Sakai Montreal CRIM Workshop Data Persistence and Intro to Hibernate.
Java Frameworks Indy Java Users Group January 29, 2003.
JDBC Session 5 Tonight: Data Access Patterns 1.J2EE Architecture & Design Patterns 2.The Data Access Tier 3.Data Access Patterns –DataAccessObject (DAO)
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.
Spring. Spring Overview Spring Container Concepts Spring and AOP Spring and Data Access Managing Transactions and Resources Remoting and Accessing Enterprise.
UPortal Developers MIT August 2004 Persistence Strategy for uPortal 3 Mike DeSimone the r-smart group
Agenda What is Hibernate Spring Integration Questions Overview
BEST PRACTICES - Java By Configuration Use global-forwards/results Helps to avoid duplicate jsp files and redundancy forward mapping.
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.
OpusCollege and the use of Spring and iBatis
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.
Anti Orgla, Nortal AS Spring Framework
Opus College - overview. OpusCollege - background First project: ICT Capacity Building Mozambican Higher Education Institutions Partners: RUG Groningen,
1 KC Web & Java – 29 november 2005 – Design Patterns – The Template Method AJAX! KC Web & Java 29 november 2005.
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.
Spring Database Access Ojitha Kumanayaka Copyright © Virtusa, 2007.
A Presentation By V AIBHAV S AHARAN Web-enHanced Information Management COMS E6125.
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Introduction to Spring Remoting Simplifying.
Creating competitive advantage Copyright © 2003 Enterprise Java Beans Presenter: Wickramanayake HMKSK Version:0.1 Last Updated:
Persistence – Iteration 4 Vancouver Bootcamp Aaron Zeckoski
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.
Java EE Patterns Dan Bugariu.  What is Java EE ?  What is a Pattern ?
Topic : Hibernate 1 Kaster Nurmukan. An ORM tool The problem fixed by ORM Advantage Hibernate Hibernate Basic –Hibernate sessionFactory –Hibernate Session.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 JSP Application Models.
JAVA EE 6 Best Practices for Migrating Spring to WTF ?!?
Persistence – Iteration 4 Vancouver Bootcamp Aaron Zeckoski
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
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Overview of the Spring Framework Introducing.
Topic : Hibernate 1 Kaster Nurmukan. An ORM tool Used in data layer of applications Implements JPA.
Spring Framework BT Team 25/02/ Spring A lightweight framework that addresses each tier in a Web application. Presentation layer – An MVC framework.
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Introduction to Spring JDBC Simplifying.
Spring JDBC Dima Ionut Daniel. Contents What is Spring JDBC? Overview Spring JDBC Core SQL Exceptions Database Connection Batch Operations Handling BLOB/CLOB.
Fundamentals of MyBATIS
.  A reference model developed for an important use case (Air Job) including UI widgets, controllers, services and DAOs.  After full requirement analysis.
Introduction to ORM Hibernate Hibernate vs JDBC. May 12, 2011 INTRODUCTION TO ORM ORM is a programming technique for converting data between relational.
Text 16 de mayo de 2009 Spring Framework Part III. Portable Service Abstractions and Spring MVC Buenos Aires, June 2009.
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.
J2EE Lecture 6: Spring – IoC and Dependency Injection
Persistence – Iteration 4 Vancouver Bootcamp
Chengyu Sun California State University, Los Angeles
By Ru Shen Department of Computer Science UAlbany, 2008
Intro to Spring CJUG - January 2013.
Chengyu Sun California State University, Los Angeles
Developing and testing enterprise Java applications
CS520 Web Programming Spring – Aspect Oriented Programming
Presentation transcript:

Text 16 de mayo de 2009 Spring Framework Part III. Portable Service Abstractions Buenos Aires, June 2009.

16 de mayo de 2009 Contents  Introduction  Transaction management  DAO support  Data access with JDBC  Data access with ORM  Testing with Spring  Summary

16 de mayo de 2009 Contents Introduction  Transaction management  DAO support  Data access with JDBC  Data access with ORM  Summary

16 de mayo de 2009 Overview At it’s core, Spring provides: Dependency Injection container – Effectively organize your dependencies, takes care of plumbing. – Facilitate good programming practices, as programming to interfaces. – Build flexible applications very easy to test and configure. AOP support for handling crosscutting concerns – Provides J2EE services to POJOs. – E.g.: transaction management, security, logging, auditing, etc. – Portable between applications servers (no vendor lock in). Portable Service Abstractions – Simplified APIs for many 3rd party frameworks (Hibernate, JDBC, Quartz, JMX,...). These together enable you to write powerful, scalable applications using POJOs.

16 de mayo de 2009 Portable Service Abstractions Many Java EE APIs are very low-level – Need to write lots of repetitive code – Often error-prone – Exception handling can be painful - e.g. checked exceptions Spring provides higher level APIs – Encapsulate low-level APIs – Eliminate boilerplate code – Simplified exception handling ⇒ Simpler application code

16 de mayo de 2009 Portable Service Abstractions Spring provides abstraction for: Transaction Management JDBC ORM frameworks: – Hibernate – JPA – Toplink – iBATIS JavaMail JMX RMI JMS Quartz … Allows access to these frameworks without knowing how they actually work.

16 de mayo de 2009 Contents  Introduction Transaction management  DAO support  Data access with JDBC  Data access with ORM  Summary

16 de mayo de 2009 Interceptors Interception around advive is the most fundamental advice type in Spring. AOP Alliance compliant. The advice itself is represented by a bean, and must implement one of the advice interfaces (Spring 1.2 AOP support). public class GreetingsInterceptor implements MethodInterceptor { public Object invoke(MethodInvocation invocation) throws Throwable { System.out.println("**** HI from INTERCEPTOR !!! ****"); Object rval = invocation.proceed(); System.out.println("**** GOODBYE from INTERCEPTOR !!! ****"); return rval; }

16 de mayo de 2009 Interceptors An advisor is like a small self-contained aspect that has a single piece of advice. It’s defined with the tag. This is the new xml for the interceptor aspect:

16 de mayo de 2009 Interceptors Console output: **** HI from INTERCEPTOR !!! **** counter = 0 counter = 1 counter = 2 counter = 3 counter = 4 counter = 5 counter = 6 counter = 7 counter = 8 counter = 9 counter = 10 **** GOODBYE from INTERCEPTOR !!! ****

16 de mayo de 2009 Transaction Manager Spring does not directly manage transactions. Use transaction managers that delegate responsibility for transaction management to a platform-specific transaction implementation provided by either JTA or the persistence mechanism. Each transaction manager acts as a façade to a platform-specific transaction implementation. Makes it possible for you to work with a transaction in Spring with little regard to what the actual transaction implementation is. To use a transaction manager, you’ll need to declare it in your application context. For JDBC:

16 de mayo de 2009 Spring’s Declarative Transactions

16 de mayo de 2009 PlatformTransactionManager Hierarchy

16 de mayo de 2009 DataSourceTransactionManager Manages JDBC connections – Opens and closes JDBC connections – Stores connection in a ThreadLocal Manages transactions – Connection.setAutoCommit(false) – Connection.commit() – Connection.rollback()

16 de mayo de 2009 Tieing it all togheter Declare the Transaction Manager bean: Declare the Transaction Advice bean. Declare the Transaction Interceptor:

16 de mayo de 2009 Contents  Introduction  Transaction management Data access with Spring  Summary

16 de mayo de 2009 Data Access Objects Service objects delegate data access to DAOs interfaces. This makes the service objects easily testable. You could create mock implementations for the DAOs. The data access layer is accessed in a persistence technology-agnostic manner.

16 de mayo de 2009 Data Access Persistence Hierarchy The Problem JDBC force you to catch SQLExceptions. Most of them indicate a fatal condition, little can be done at runtime. Why catch them? SQLException is the exception thrown for all data problems: is to generic. Persistence frameworks like Hibernate offer many different exceptions. But they are propietary to Hibernate. You will spread them in your code. Spring solution Provides several data access exceptions, each descriptive of the problem that they’re thrown. They are not associated with any particular persistence solution. Consistent regardless of which persistence provider you use. They are all unchecked exceptions. No more (empty…) catch blocks. This leaves the decision of wheter to catch an exception to the developer.

16 de mayo de 2009 Data Access Persistence Hierarchy

16 de mayo de 2009 Templating Data Access Spring separates the fixed and variable parts of the data access process into templates and callbacks. Spring comes with several data access templates, depending the persistence mechanism (e.g. JdbcTemplate, HibernateTemplate, etc)

16 de mayo de 2009 DAO Support Classes DAO Support Classes are meant to be subclassed by your own DAO classes. You can call the getTemplate method to hava direct access to the underlying data access template. E.g. getJdbcTemplate(), get HibernateTemplate(). You have also access to the class the persistence platform uses to communicate with the database. E.g. getConnection() for the JdbcDaoSupport, getSessionFactory() for HibernateDaoSupport.

16 de mayo de 2009 JDBC Example First, declare the dataSource

16 de mayo de 2009 JDBC Example JDBC code for addBook (26 lines) Using JdbcDaoSupport (3 lines) private static final String ADD_BOOK = "insert into books (id, name, author) values (?, ?, ?)"; public void addBook(Book book) { Connection conn = null; PreparedStatement stmt = null; try { conn = dataSource.getConnection(); stmt = conn.prepareStatement(ADD_BOOK); stmt.setInt(1, book.getId()); stmt.setString(2, book.getName()); stmt.setString(3, book.getAuthor()); stmt.execute(); } catch (SQLException e) { e.printStackTrace(); } finally { try { if (stmt != null) { stmt.close(); } if (conn != null) { conn.close(); } } catch (SQLException e) { e.printStackTrace(); } private static final String ADD_BOOK = "insert into books (id, name, author) values (?, ?, ?)"; public void addBook(Book book) { getSimpleJdbcTemplate().update(ADD_BOOK, book.getId(), book.getName(), book.getAuthor()); }

16 de mayo de 2009 Spring integration with Hibernate Define a DataSource bean Define a Session Factory bean demo.entity.Contact org.hibernate.dialect.Oracle10gDialect

16 de mayo de 2009 Spring integration with Hibernate Define an Hibernate Template bean Define a Transaction Manager bean Define a Parent DAO bean for CRUD operations

16 de mayo de 2009 Spring integration with Hibernate Define an Transaction Proxy Template bean Service interface and implementation PROPAGATION_REQUIRED

16 de mayo de 2009 Spring integration with Hibernate DAO interface and implementation

16 de mayo de 2009 Contents  Introduction  Transaction management  DAO support  Data access with JDBC  Data access with ORM Summary

16 de mayo de 2009 Summary Spring framework:  Dependency injection  AOP  Service abstractions  Improved SOC  DRY code  Simpler code  Improved maintainability  Easier to develop and test  Let’s you focus on the core problem

16 de mayo de 2009 References

16 de mayo de Thank You