Upload
govindbirajdar
View
233
Download
0
Embed Size (px)
Citation preview
8/3/2019 Using Spring and Hibernate
1/46
G53ELC
05/05/12 G53ELC 1
Using Spring and Hibernate
Dave Elliman
8/3/2019 Using Spring and Hibernate
2/46
G53ELC
05/05/12 G53ELC 2
The overall architecture
8/3/2019 Using Spring and Hibernate
3/46
G53ELC
05/05/12 G53ELC 3
The enterprise view (Java)
8/3/2019 Using Spring and Hibernate
4/46
G53ELC
05/05/12 G53ELC 4
Revision - What is
Dependency Injection?
DI is all about wiring up objects or
plumbing if you prefer
It is about ensuring loose coupling and fitswell with design patterns
Design to an interface and then inject the
actual class at run time This is what inheritence is really for
8/3/2019 Using Spring and Hibernate
5/46
G53ELC
05/05/12 G53ELC 5
A Real World Example?
Web App
Stock Quotes Authenticator
Error Handler Logger Database
This example was originally created by Jim Weirich in Ruby on his blog.
8/3/2019 Using Spring and Hibernate
6/46
G53ELC
05/05/12 G53ELC 6
Remember the old way
public class WebApp{public WebApp(){
quotes = new StockQuotes();authenticator = new Authenticator();database = new Database();logger = new Logger();
errorHandler = new ErrorHandler();}
// More code here...
}
8/3/2019 Using Spring and Hibernate
7/46
G53ELC
05/05/12 G53ELC 7
What about the child
objects?
How does the StockQuotes find the Logger? How does the Authenticator find the
database?
Suppose you want to use a TestingLoggerinstead? Or a MockDatabase?
8/3/2019 Using Spring and Hibernate
8/46
G53ELC
05/05/12 G53ELC 8
Service Locator Interface
public interface ILocator
{
TObject Get();}
8/3/2019 Using Spring and Hibernate
9/46
G53ELC
05/05/12 G53ELC 9
Service Locator Example
public class MyLocator : ILocator{
protected Dictionary dict =new Dictionary();
public MyLocator(){dict.Add(typeof(ILogger), new Logger());dict.Add(typeof(IErrorHandler),
new ErrorHandler(this));dict.Add(typeof(IQuotes), new StockQuotes(this));
dict.Add(typeof(IDatabase), new Database(this));dict.Add(typeof(IAuthenticator),
new Authenticator(this));dict.Add(typeof(WebApp), new WebApp(this));
}
}
8/3/2019 Using Spring and Hibernate
10/46
G53ELC
05/05/12 G53ELC 10
StockQuotes with Locator
public class StockQuotes{public StockQuotes(ILocator locator)
{errorHandler =
locator.Get();logger = locator.Get();
}
// More code here...}
8/3/2019 Using Spring and Hibernate
11/46
G53ELC
05/05/12 G53ELC 11
Good things
Classes are decoupled from explicit
imlementation types Easy to externalise the configuration
8/3/2019 Using Spring and Hibernate
12/46
G53ELC
05/05/12 G53ELC 12
Dependency Injection
Containers
Gets rid of the dependency on the
ILocator
Object is no longer responsible for finding
its dependencies
The container does it for you
8/3/2019 Using Spring and Hibernate
13/46
G53ELC
05/05/12 G53ELC 13
Then what?
Write your objects the way you want Setup the container
Ask the container for objects
The container creates objects for you and
fulfills dependencies
8/3/2019 Using Spring and Hibernate
14/46
G53ELC
05/05/12 G53ELC 14
Setting Up the Container
(XML)
8/3/2019 Using Spring and Hibernate
15/46
G53ELC
05/05/12 G53ELC 15
Creating Objects
[Test]public void WebAppTest(){DIContainer container = GetContainer();
IStockQuotes quotes =container.Get();IAuthenticator auth =
container.Get();
Assert.IsNotNull( quotes.Logger );Assert.IsNotNull( auth.Logger );Assert.AreSame( quotes.Logger, auth.Logger );
}
8/3/2019 Using Spring and Hibernate
16/46
G53ELC
05/05/12 G53ELC 16
Existing Frameworks
Java Pico Container
Spring Framework
HiveMind
Ruby
Rico Copland
Python PyContainer
.NET Pico.NET
Spring.NET p&p Object Builder
8/3/2019 Using Spring and Hibernate
17/46
G53ELC
05/05/12 G53ELC 17
Spring with Persistence Layers
8/3/2019 Using Spring and Hibernate
18/46
G53ELC
05/05/12 G53ELC 18
Spring can act as the
Application
8/3/2019 Using Spring and Hibernate
19/46
G53ELC
05/05/12 G53ELC 19
A persistent POJO for Hibernate
package elc; public class Tutor { private String id; /// unique identifier private String surname; private String firstname; private Tutor(); /// default constructor public Tutor(String id, String surname, String firstname) { this.id = id; this.surname = surname; this.firstname = firstname); } public String getId() {return id; } public String getSurname() {return surname; }
public String getFirstname() {return firstname; } public void setId(String id) { this.id = id; } public void setSurname(String surname) { this.surname = surname; } public void setLogin(String firstname) { this.firstname = firstname; } }
8/3/2019 Using Spring and Hibernate
20/46
G53ELC
05/05/12 G53ELC 20
The Hibernate Session
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
... some code
Session = getSessionFactory().openSession();
Transaction tx = session.beginTransaction();
Tutor dave=new Tutor("dge","Elliman","Dave");
session.save(dave); tx.commit();
session.close();
... more code
8/3/2019 Using Spring and Hibernate
21/46
8/3/2019 Using Spring and Hibernate
22/46
G53ELC
05/05/12 G53ELC 22
hibernate.cfg.xml
com.mysql.jdbc.Driver
jdbc:mysql://localhost/hibernatetutorial
root
10
true
org.hibernate.dialect.MySQLDialectupdate
8/3/2019 Using Spring and Hibernate
23/46
A I f d l
8/3/2019 Using Spring and Hibernate
24/46
G53ELC
05/05/12 G53ELC 24
An Intereface and a class to
define
public interface TutorDao {public void createTutor(final Tutor tutor);
public Tutor getTutor(final String id);
}
Also saveTutor(); deleteTutor() etc.
CRUD is now done with single lines of code. No
SQL needed
8/3/2019 Using Spring and Hibernate
25/46
G53ELC
05/05/12 G53ELC 25
Do you get the CONCEPTS?
You can look up the detail when you needit
ELC exam will test the concepts and not
the detail of xml configuration files of Javacode
You need to know that a configuration file
is needed and what it does, not the finedetail of its formatting
8/3/2019 Using Spring and Hibernate
26/46
G53ELC
05/05/12 G53ELC 26
Any Questions?
Im confused
8/3/2019 Using Spring and Hibernate
27/46
G53ELC
05/05/12 G53ELC 27
JEE and EJBs
Dave Elliman
8/3/2019 Using Spring and Hibernate
28/46
G53ELC
05/05/12 G53ELC 28
POJOs and POJIs
Plain Old Java Objects and Interfaces In the past EJBs were all rather different
and special and you could not test them
outside a container Now they are POJ you can
8/3/2019 Using Spring and Hibernate
29/46
G53ELC
05/05/12 G53ELC 29
Annotations
There has been a revolution in Server-sideJava and Tiger Jane 1.5: EJB
annotations
These are processed by a newtool called apt
You can write your own if you like
8/3/2019 Using Spring and Hibernate
30/46
8/3/2019 Using Spring and Hibernate
31/46
G53ELC
05/05/12 G53ELC 31
The Concept
Entity bean
corresponds to a setof records in a
database
Session bean
handles business flow
(one per client unless
stateless, when may
be shared)
8/3/2019 Using Spring and Hibernate
32/46
G53ELC
05/05/12 G53ELC 32
Enterprise JavaBeans
Definition from OReillys EnterpriseJavaBeans bookAn Enterprise Java Bean is a standard server-
side component model Aha! Its a standard for building
MIDDLEWARE In multi-tiered solutions
The Context in Which EJBs
8/3/2019 Using Spring and Hibernate
33/46
G53ELC
05/05/12 G53ELC 33
The Context in Which EJBs
Are Used
EJB container
EJBeanEJB Object
clientEJB server
..
JT
S
JN
DI
secu
rity
EJB Home
8/3/2019 Using Spring and Hibernate
34/46
G53ELC
05/05/12 G53ELC 34
It All Works by RMI
Client
SkeletonStub Server-
Side
Compo
nent
Client-Side Network Middle Tier
invokes
return
results
connect to
remoteobject invoke
return
resultsreturn
results
8/3/2019 Using Spring and Hibernate
35/46
G53ELC
05/05/12 G53ELC 35
A Taxonomy of EJBs
EJB
Entity Session
Bean managed Container managed stateless stateful
Differences Between
8/3/2019 Using Spring and Hibernate
36/46
G53ELC
05/05/12 G53ELC 36
Differences Between
Beans
Entity Beans Represent persistent data
Long-livedSession Beans
Interact with clients
Model business Logic
Short-lived
Enterprise JavaBeans Container
Session Bean Entity Bean
Clients RDBMS
8/3/2019 Using Spring and Hibernate
37/46
G53ELC
05/05/12 G53ELC 37
Session Bean (Stateful)
Assigned to 1 client only Keeps info about a client
ie has attributes for clients state
Assigned to a client for lifetime
Non-persistent
Short-lived (client, timeout, server crash)
8/3/2019 Using Spring and Hibernate
38/46
G53ELC
05/05/12 G53ELC 38
Session Bean (Stateless)
Many clients can access it
Implements business logic
One bean can service multiple clients (fastand efficient)
Lifetime controlled by container
No state across methods
Each instance identical upon creation
Short-lived
8/3/2019 Using Spring and Hibernate
39/46
G53ELC
05/05/12 G53ELC 39
Insight Into Session Beans
EJB Server
Client Using
stateless
Session Beans
Clients Using
only Entity Beans
8/3/2019 Using Spring and Hibernate
40/46
G53ELC
05/05/12 G53ELC 40
Entity EJBs
Bean managed persistence (BMP) developer writes JDBC code and SQL
statements
Container managed persistence (CMP) Container generates methods to read and
write to the database dropped!
Takes necessary information from theDeployment Descriptor
8/3/2019 Using Spring and Hibernate
41/46
G53ELC
05/05/12 G53ELC 41
EJB Architecture Diagram
Client
Home
InterfaceHome
Stub
Remote
InterfaceEJB
Stub
EJB Server
EJB Container
Home Interface
EJBHome
Remote Interface
EJB Object
Bean Class
8/3/2019 Using Spring and Hibernate
42/46
G53ELC
05/05/12 G53ELC 42
A Stateless Session Bean
import javax.ejb.*;
/*** A stateless session bean requesting that a* remote busines interface be generated for it.*/@Stateless
@Remotepublic class HelloWorldBean {public String sayHello() {
return "Hello World!!!";}
}
8/3/2019 Using Spring and Hibernate
43/46
G53ELC
05/05/12 G53ELC 43
A Stateful Session Bean
import javax.ejb.*;
@Stateful
public class ShoppingCartBean implements ShoppingCart {
private String customer;
public void startToShop(String customer) {this.customer = customer;}
public void addToCart(Item item) {
System.out.println("Added item to cart... ");
}
@Remove
public void finishShopping() {
System.out.println("Shopping Done... ");
}}
8/3/2019 Using Spring and Hibernate
44/46
G53ELC
05/05/12 G53ELC 44
A Entity Java Bean
import javax.ejb.*;
@Entity
public class Order {
private Long id;
private int version;
private int itemId;
private int quantity;
private Customer cust;
@Id(generate=AUTO)
public Long getId() { return id; }
public void setId(Long id) { this.id = id; }
@Version
protected int getVersion() { return version; }
protected void setVersion(int version) { this.version = version; }
@Basic
public int getItemId() { return itemId; }
public void setItemId(int itemId) { this.itemId = itemId; }
@Basic
public int getQuantity() { return quantity; }public void setQuantity(int quantity) {
this.quantity = quantity;
}
@ManyToOne
public Customer getCustomer() { return cust; }
public setCustomer(Customer cust) { this.cust = cust; }
}
8/3/2019 Using Spring and Hibernate
45/46
8/3/2019 Using Spring and Hibernate
46/46
G53ELC
End of detour into Java!