Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Text 16 de mayo de 2009 Spring Framework Part III. Portable Service Abstractions Buenos Aires, June 2009."— Presentation transcript:

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

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

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

4 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.

5 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

6 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.

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

8 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; }

9 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:

10 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 !!! ****

11 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:

12 16 de mayo de 2009 Spring’s Declarative Transactions

13 16 de mayo de 2009 PlatformTransactionManager Hierarchy

14 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()

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

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

17 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.

18 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.

19 16 de mayo de 2009 Data Access Persistence Hierarchy

20 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)

21 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.

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

23 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()); }

24 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

25 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

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

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

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

29 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

30 16 de mayo de 2009 References

31 16 de mayo de 2009 31 Thank You


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

Similar presentations


Ads by Google