Enterprise Java Development using JPA, Hibernate and Spring Srini Penchikala Detroit JUG Developer Day Conference November 14, 2009
About the Speaker Enterprise Architect Writer, Speaker, Editor (InfoQ) Working with Java since 1996, JEE (2000), SOA (2006), & PowerPoint since July 2009 Current: Agile Architectures, Domain-Driven Design, Architecture Enforcement, Model Driven Development, SOA Future: Security Architecture, Role of DSL's in Architecture Enforcement 2
Goals for this Presentation What are the essential ingredients of JEE development recipe and How Spring Portfolio helps in implementing a JEE project 3
Format Interactive Demos Duration: ~120 minutes Discussion oriented tutorial Not a hands-on workshop Q & A 4
Before we start How many are currently using Spring? Level of experience 5
Agenda (1/2) Introduction Tutorial Sample Application Spring Framework Why Spring? JEE Application Architecture Domain-Driven Design Dependency Injection Aspect-oriented Programming Annotations 6
Agenda (2/2) Application Concerns Persistence Transaction Management Application Security MVC Validation Web Services (REST) Conclusions 7
Agenda Introduction Tutorial Sample Application Spring Framework Why Spring? JEE Application Architecture Domain-Driven Design Dependency Injection Aspect-oriented Programming Annotations 8
Tutorial Application (1/5) Loan processing application Demonstrates how to use JPA, Hibernate and Spring in developing enterprise Java applications Covers JEE application concerns that developers run into in real-world applications 9
Tutorial Application (2/5) Loan processing application Demonstrates how to use JPA, Hibernate and Spring in developing enterprise Java applications JEE application concerns that developers run into in real-world applications: Application Security Persistence Transaction Management Data Binding/ Data Validation Web Services (REST) 10
Tutorial Application (3/5) Tools: JDK 1.6 Maven Eclipse Testing: JUnit 4.4 Mockito Spring Test Module 11
Tutorial Application (4/5) Technologies (non-spring): JPA Hibernate JAXB Jetty Dozer HSQL JBossCache/EHCache 12
Tutorial Application (5/5) Technologies (Spring): Spring Core (IoC) Spring AOP Spring JPA Spring Security Spring Modules 13
How to build sample application Checkout the code from Google Project (https://enterprise-javaspring.googlecode.com/svn/trunk) Install JDK, Maven and Eclipse (if needed) Modify setbaseenv.bat file To compile and unit test: mvn clean compile test To run the web application: mvn package jetty:run 14
Agenda (1/2) Introduction Tutorial Sample Application Spring Framework Why Spring? JEE Application Architecture Domain-Driven Design Dependency Injection Aspect-oriented Programming Annotations 15
Spring Framework Light-weight JEE middleware framework Separation of concerns (business v. infrastructure) POJO Based Design Business domain first, infrastructure concerns second Agile Development and Testing 16
Spring Portfolio Projects Spring Core Spring AOP Spring Security Spring MVC/Spring WebFlow 17
Agenda Introduction Tutorial Sample Application Spring Framework Why Spring? JEE Application Architecture Domain-Driven Design Dependency Injection Aspect-oriented Programming Annotations 18
Why Spring (1/3) Test Driven Development Unit testing in isolation Mock objects System integration testing support
Why Spring (2/3) Agile Software Development (Scrum) Iterative Design, Development, Unit Testing and Refactoring Modular Application Architecture POJO Programming Model
Why Spring (3/3) Domain-Driven Design
Agenda Introduction Tutorial Sample Application Spring Framework Why Spring? JEE Application Architecture Domain-Driven Design Dependency Injection Aspect-oriented Programming Annotations 22
Architecture Layers User Interface Application Domain Infrastructure 23
Layered Architecture Example 24
Typical J2EE Architecture Model Application Controller HTTP Session Management Facade Transaction Management Caching Data Access Data Access Object Persistence Data Store Domain Object* Database Notes: No real Domain Layer Business Logic in Façade Layer (Stateless Session Beans) Direct Access to Data Access Objects (CRUD logic is infrastructure not business) *Domain objects are really Persistence classes with getters/setters Infrastructure concerns have overtaken/ overshadowed the domain concerns 25
The Problem Architects/Developers are thinking only about infrastructure Most of the development time is still spent on writing plumbing code instead of real business logic We have lost real OOD/OOP 26
Domain Driven Architecture Application Controller Facade Infrastructure Domain Domain HTTP Session Management Remoting Persistence Caching Transaction Management Security (Authentication & Authorization Asynchronous Messaging Data Access/ Persistence Data Access Object Data Store Database Notes: All Layers support POJO based design Controller and Façade layers are consumers of Domain Classes Business Logic only in Domain Classes No direct Access to DAO s except from Domain Classes Domain First, Infrastructure Second Infrastructure concerns are implemented via DI, AOP, Annotations 27
Application Architecture Layers
Agenda Introduction Tutorial Sample Application Spring Framework Why Spring? JEE Application Architecture Domain-Driven Design Dependency Injection Aspect-oriented Programming Annotations 29
Domain Driven Design What: Domain Driven Design (DDD) is about mapping business domain concepts into software artifacts. Why: To create better software by focusing on a model of the domain rather than the technology. 30
Advantages Promotes high cohesion and low coupling in the application code Easy to test domain components Business (domain) logic is isolated from nondomain and infrastructure code Adding/changing services does not influence the domain or other services 31
Domain Elements Domain objects: Instances of real entities which hold knowledge or activity of business domain Entities: Objects defined by their identity, which remains same throughout life of software (Customer, Account) Value Objects Immutable objects that have no identity & are used to describe an attribute of another object (Product, State lookup) (Domain) Services Objects that have no state & only have a defined behavior that s not part of any domain object (FundTransfer service, Loan Interest Calculation) 32
Other DDD Elements Module (Package): A method of organizing related concepts and tasks in order to reduce complexity. Domain object Lifecycle is managed by: Aggregate Factory Repository 33
Design Recipe Object Oriented Programming (OOP) Dependency Injection (DI) Aspect-oriented Programming (AOP) Annotations 34
Domain Elements and OOP Domain Element Entity, Value Object, Aggregate Data Transfer Object Service, Repository Encapsulation State and Behavior State only Behavior only 35
Agenda Introduction Tutorial Sample Application Spring Framework Why Spring? JEE Application Architecture Domain-Driven Design Dependency Injection Aspect-oriented Programming Annotations 36
Dependency Injection (DI) aka Inversion of Control (IoC) Based on Hollywood Principle Decouple and manage the dependencies of the components in the application DI Frameworks: Spring Google Guice Pico Container 37
DI Use Cases Service Domain Classes Transaction Manager Domain Data Access Object (DAO) DAO class Data Source JPA Entity Manager 38
Domain Object DI Domain objects not instantiated by Spring IoC container @Configurable annotation 39
DEMO 40
Agenda Introduction Tutorial Sample Application Spring Framework Why Spring? JEE Application Architecture Domain-Driven Design Dependency Injection Aspect-oriented Programming Annotations 41
Aspect-oriented Programming Allows developers to add behavior to objects in a non-obtrusive manner through use of static and dynamic crosscutting Main goal is to code cross-cutting concerns in separate modules and apply them in a declarative way 42
AOP Use Cases Spring Framework Built-In Aspects Transaction Management Security Custom Aspects Profiling Caching Rules Architecture Governance/Policy Enforcement 43
DEMO 44
Agenda Introduction Tutorial Sample Application Spring Framework Why Spring? JEE Application Architecture Domain-Driven Design Dependency Injection Aspect-oriented Programming Annotations 45
Annotations Added in Java SE 5.0 as Java Metadata facility (JSR 175) Provide a way to add metadata to program elements Defined by nearly every recent JSR standard Also include a mechanism for adding custom annotations to the Java code 46
Annotations For Domain Elements Layer Domain Element Annotation Domain Entity, Value Object @Entity (JPA), @Configurable Domain Repository @Repository Domain Service @Service Application Controller @Controller All All @Component 47
Custom Annotations Use Cases: Caching Validation Implementation options: Reflection Annotation Processing Tool (APT) Byte-code Instrumentation (Javassist) Aspects/AOP (AspectJ and Spring AOP) 48
DEMO 49
Application Architecture Layers Database Data Access Domain Controller Presentation 50
Agenda Application Concerns Persistence Transaction Management Application Security MVC Validation Web Services (REST) Conclusions 51
Application Concerns Persistence Transaction Management Application Security (AuthN & AuthZ) Caching 52
Other Application Concerns Monitoring (JMX, Spring JMX) Conversational Web Applications (Web Flow) Asynchronous Messaging (JMS, MDP) Enterprise Integration (Spring Integration) Batch Processing (Spring Batch) * Not covered in this presentation 53
Agenda Application Concerns Persistence Transaction Management Application Security MVC Validation Web Services (REST) Conclusions 54
Persistence The Spring Way JDBC (Spring JDBC Template) JPA w/ Hibernate, OpenJPA, EclipseLink (TopLink) Spring JPA Utils & Data Source DI 55
DEMO 56
Agenda Application Concerns Persistence Transaction Management Application Security MVC Validation Web Services (REST) Conclusions 57
Transaction Management The Spring Way Declarative transaction semantics Similar to EJB3 transaction model and more Spring JTA Support Support for multiple transaction managers JDBC Hibernate JTA Transactions are managed in Service classes using @Transactional annotation 58
DEMO 59
Agenda Application Concerns Persistence Transaction Management Application Security MVC Validation Web Services (REST) Conclusions 60
Application Security Spring Security Module Declarative application security support Fits in very well with Spring Core framework Authentication Authorization URL level Domain Object Service Method JDBC and LDAP Authentication Providers JSR-250 compliant 61
Authentication Code Example 62
Authorization Code Example 63
DEMO 64
Agenda Application Concerns Persistence Transaction Management Application Security MVC Validation Web Services (REST) Conclusions 65
Ideal Web Application Framework Isolation from domain layer so presentation and domain layers can evolve independently Simple and Easy Data binding between Domain, Controller and Presentation layers Support for data validation that works in all layers w/o any additional coding Support for AJAX functionality Controller methods can be exposed as RESTful Web Services w/o extra coding or configuration 66
MVC Spring MVC Annotation-based Controller Configuration (@Controller) @RequestMapping @ModelAttribute @RequestParam @SessionAttributes 67
DEMO 68
Agenda Application Concerns Persistence Transaction Management Application Security MVC Validation Web Services (REST) Conclusions 69
Data Binding and Validation Data binding using @ModelAttribute @InitBinder Example: FeeController Validation: Declaration Validation Logic Custom Validation Error Messages JSR 303 support 70
DEMO 71
Agenda Application Concerns Persistence Transaction Management Application Security MVC Validation Web Services (REST) Conclusions 72
Web Services Support Spring WS Spring 3.0 REST support Message Converters RestTemplate 73
Project Lifecycle Diagram 74
Agenda Application Concerns Persistence Transaction Management Application Security MVC Validation Web Services (REST) Conclusions 75
Conclusions Domain First, Infrastructure Second Layered Architecture & Separation of Concerns POJO s are fun to work with again Balance between the right vs. the right now Java EE 6 release focuses on development & deployment simplicity JPA 2.0, EJB 3.1, Spring 3.0 76
References (1/2) Sample Application Project Site (https://enterprise-javaspring.googlecode.com/svn/trunk) Spring Framework Reference Manual (http://static.springsource.org/spring/docs/3.0.x/springframework-reference/html/) Spring In Action 3 rd Edition (http://www.manning.com/walls4/) Domain Driven Design and Development In Practice (http://www.infoq.com/articles/ddd-in-practice) Domain-Driven Design Website (http://www.domaindrivendesign.org/) 77
References (2/2) Domain-Driven Design by Eric Evans Applying Domain-Driven Design and Patterns, Jimmy Nilsson Patterns of Enterprise Application Architecture, Martin Fowler Can DDD be Adequately Implemented Without DI and AOP (http://www.infoq.com/news/2008/02/ddd-di-aop) 78
Contact Information Domain-Driven Design and Enterprise Architecture articles on InfoQ website: http://www.infoq.com srinipenchikala@gmail.com http://srinip2007.blogspot.com @srinip 11/21/2009 Srini Penchikala 79
Q & A 80
Thank You Thank you for your attention Feedback survey Happy Holidays 81