Download Oracle 7Up Workshop - Day One - SQL

Document related concepts
no text concepts found
Transcript

Spring – Power to the POJO
Introductie tot het Spring Framework
Aino Andriessen & Lucas Jellema
KC Web & Java, donderdag 30 juni 2005

AMIS KC Web & Java - Spring Framework – Power to the POJO
1
Agenda

 Introductie Spring – History, Background
 Hoe kom je aan nieuwe objecten
 BeanFactory en Inversion of Control + Dependency Injection
 Hoe laat je bestaande objecten naar je pijpen dansen?
 Aspect Oriented Programming (AOP)






Business Tier & Architectuur
Test Driven Development
Spring Persistence & Spring DAO
Spring Remoting
Losse eindjes, conclusies, discussie
Workshop

AMIS KC Web & Java - Spring Framework – Power to the POJO
2
Where we’ve come from

 EJB as we know it…
 Resembles the original JavaBeans specification in name only.
 Heavyweight:
•
•
•
Requires application server
Difficult to unit-test
Intrusive (must implement EJB interfaces)
 Complex
•
•
Home/Remote/Local/LocalHome interfaces
Deployment descriptors
 Non-intuitive

AMIS KC Web & Java - Spring Framework – Power to the POJO
3
The future of EJB…

 Spilling the beans…




EJB 3.0 will embrace simplicity
Based on POJO/POJI (? Well, maybe)
Employ dependency injection instead of LDAP
Declarative services (transactions, security) will be
aspects
 Entity beans will be POJOs, persisted via Hibernatelike framework

AMIS KC Web & Java - Spring Framework – Power to the POJO
4
The future is NOW!

 Spring is a lightweight container framework
 Build applications based on POJO/POJI.
 Wire beans together using dependency injection.
 Declaratively apply transactions and security using
aspect.
 Integrates cleanly with Hibernate for persistence.
 EJB 3.0=Spring + Hibernate + Metadata

AMIS KC Web & Java - Spring Framework – Power to the POJO
5
Spring History

 J2EE Design and Development – by Rod
Johnson, 2002
 Introducing the i21 framework
 First release of Spring: Spring 2004
 Spring 1.2.1: June 2005
 Open Source
 Interface21 – small company with
most core committers
 Contributions from Oracle and other
parties
 Spawned many sub-projects

AMIS KC Web & Java - Spring Framework – Power to the POJO
6
Power to the POJO

IoC (Dependency Injection)

AMIS KC Web & Java - Spring Framework – Power to the POJO
7
Spring’s modules


AMIS KC Web & Java - Spring Framework – Power to the POJO
8
What’s more…

 Remoting support via RMI, JAX-RPC, and
Hessian/Burlap
 Metadata (ala, JSR-175 or Commons Attributes)
 Persistence via TopLink, Hibernate, JDO, or iBatis
support
 E-mail support
 EJB support
 JMX Support (Spring 1.1)
 JMS support
 Spring Rich Client Platform (Spring 1.1)
 Spring .Net

AMIS KC Web & Java - Spring Framework – Power to the POJO
9
Spring is HOT!


AMIS KC Web & Java - Spring Framework – Power to the POJO
10
Many books available

 J2EE without EJB
 The starting point
 Spring Live
 Pro Spring
 Spring in Action
 Professional Spring Development
 To be released, july 2005

AMIS KC Web & Java - Spring Framework – Power to the POJO
11
Core Spring
Inversion of Control & Dependency Injection


AMIS KC Web & Java - Spring Framework – Power to the POJO
12
Coupling

 Highly coupled code is…
 Hard to test
 Hard to maintain
 Exhibits “whack-a-mole” style bugs
 Uncoupled code is…
 Code that doesn’t do anything
 Coupling is somewhat necessary…
 …but should be controlled

AMIS KC Web & Java - Spring Framework – Power to the POJO
13
Dependency Injection

 The “Hollywood Principle”: Don’t call me, I’ll call
you.
 Collaborators aren’t asked for…they’re received.
 Also known as “Dependency Injection”, thanks to
Martin Fowler.

AMIS KC Web & Java - Spring Framework – Power to the POJO
14
Benefits of IoC

 Objects are more cohesive because they are no
longer responsible for obtaining their own
collaborators.
 When used with interfaces, code is very loosely
coupled.

AMIS KC Web & Java - Spring Framework – Power to the POJO
15
Elements of a Spring app

 Beans
 Not EJBs. Actually, not necessarily JavaBeans. Just
POJOs
 Bean wiring
 Typically an XML file.
 A bootstrap class
 A class with a main() method.
 A servlet.
 The bootstrap class uses a BeanFactory (or IoC
Container) to retrieve POJOs
 That have been ‘wired’ and ‘dependency injected’

AMIS KC Web & Java - Spring Framework – Power to the POJO
16
IoC Container in Action
Application Class needs POJOs

POJO 2
POJO 1
xml
IoC Container
POJO 3
pojo 1
pojo 2
getBean(“POJO1”)
pojo 3
Application

AMIS KC Web & Java - Spring Framework – Power to the POJO
17
Wiring beans in XML

 Root elements is <beans>
 Contains one or more <bean> elements
 id (or name) attribute to identify bean
 class attribute to specify class
The bean’s ID
<beans>
<bean id=“foo”
class=“com.habuma.foobar.Foo”>
<!-- Properties defined here -->
</bean>
</beans>

AMIS KC Web & Java - Spring Framework – Power to the POJO
The bean’s fullyqualified classname
18
Wiring a property

 Use <property> element
 name attribute specifies name of property
<beans>
<bean id=“foo”
class=“com.habuma.foobar.Foo”>
Maps to a setBar() call
<property name=“bar”>
<!-- Property value goes here -->
</property>
</bean>
</beans>

AMIS KC Web & Java - Spring Framework – Power to the POJO
19
Property values

 Strings and numbers:
<property name=“bar”><value>42</value></property>
<property name=“bar”><value>Hello</value></property>
 Null
<property name=“bar”><null/></property>
 Lists and arrays:
<property name=“bar”>
<list>
<value>ABC</value>
<value>123</value>
</list>
</property>

AMIS KC Web & Java - Spring Framework – Power to the POJO
20
Property values

 Sets:
<property name=“bar”>
<set>
<value>ABC</value>
<value>123</value>
</set>
</property>
 Maps:
<property name=“bar”>
<map>
<entry key=“key1”><value>ABC</value></entry>
<entry key=“key2”><value>123</value></entry>
</set>
</property>

AMIS KC Web & Java - Spring Framework – Power to the POJO
21
Property values

 Property sets:
<property name=“bar”>
<props>
<prop key=“prop1”>ABC</prop>
<prop key=“prop2”>123</prop>
</set>
</property>
 Other beans:
<property name=“bar”>
<ref bean=“bar”/>
</property>

AMIS KC Web & Java - Spring Framework – Power to the POJO
22
Auto-wiring

 You can auto-wire…
 “byName”: Property names are matched to bean
names
 “byType”: Property names are matched to bean types
 “constructor”: Pico-like constructor wiring. Like
“byType” except using constructor.
 “autodetect”: Uses reflection to decide whether to
use “byType” or “constructor”

AMIS KC Web & Java - Spring Framework – Power to the POJO
23
Auto-wiring

<bean id=“foo”
class=“com.habuma.foobar.Foo”
autowire=“byName”/>
<bean id=“foo”
class=“com.habuma.foobar.Foo”
autowire=“byName”>
<property name=“bar”><value>bar</value></property>
</bean>
<beans default-autowire=“byType”>
<!-- Bean definitions go here -->
</beans>

AMIS KC Web & Java - Spring Framework – Power to the POJO
24
BeanFactory vs. ApplicationContext

 A BeanFactory is the Spring container.
 Loads beans and wires beans together.
 Dispenses beans as requested.
 XmlBeanFactory is the most commonly used.
 An ApplicationContext is a BeanFactory, but adds
“framework” features such as:
 I18N messages
 Event notification

AMIS KC Web & Java - Spring Framework – Power to the POJO
25

IoC Examples

AMIS KC Web & Java - Spring Framework – Power to the POJO
26
Example of a IoC based programming


AMIS KC Web & Java - Spring Framework – Power to the POJO
27
The knight before…


AMIS KC Web & Java - Spring Framework – Power to the POJO
28
The knight after…


AMIS KC Web & Java - Spring Framework – Power to the POJO
29
Core Spring
Aspect Oriented Programming

 AOP – a programming technique that promotes
separation of concerns within a software system
 Recurring – often infrastructural – concerns can easily be
duplicated
in many objects
 Security
 Transaction
Management
 Logging
 Profiling
 AOP suggests
separation
 Concerns are applied
at compile or run-time

AMIS KC Web & Java - Spring Framework – Power to the POJO
30
AOP in a nutshell

 Aspect: A modularization of a cross-cutting concern. Implemented in
Spring as Advisors or interceptors
 Joinpoint: Point during the execution of execution.
 Advice: Action taken at a particular joinpoint.
 Pointcut: A set of joinpoints specifying where advice should be
applied.
 Advisor: Fully represents an aspect, including both advice and a
pointcut.
 Introduction: Adding methods or fields to an advised class.
 Weaving: Assembling aspects into advised objects.

AMIS KC Web & Java - Spring Framework – Power to the POJO
31
Without AOP


AMIS KC Web & Java - Spring Framework – Power to the POJO
32
With AOP


AMIS KC Web & Java - Spring Framework – Power to the POJO
33
Implementing AOP

 Compile time – modify the source code during compilation
 Requires a customized Java Compiler
 For example AspectJ; Spring does not do compile time AOP
 Run time – byte injection
 Change the class when loaded, generating a subclass that
contains the aspects
 Uses CGLib library
 Run time – using the JDK 1.3 Dynamic Proxy
 Instead of getting an object instance, the application receives a
proxy object
 The proxy implements the same interface
•
•
And maybe something else as well
Besides, it can intercept and wrap method calls

AMIS KC Web & Java - Spring Framework – Power to the POJO
34
IoC Container hides AOP implementation
from POJO consumer

Aspect A
Proxy
invoke()
xml
target=
pojo1Impl
pojo1 =
proxy
Target
intercept
orNames
=>
AspectA,
AspectB
Aspect B
target
Pojo1Impl
before()
implements
IoC Container
implements
POJO 1
(interface)
getBean(“POJO1”)
Application
AspectA
AspectB

AMIS KC Web & Java - Spring Framework – Power to the POJO
35
Different types of Advice

 Before advice

Calls to advised methods are intercepted before the method is called.
 After returning advice

Calls to advised methods are intercepted after a successful return.
 After throws advice

Calls to advised methods are intercepted after an exception is thrown.
 Around advice/interception

Calls to advised methods are intercepted. Call must be explicitly made to target
method.
 Introduction advice


Allows a class (or rather its proxy) to implement additional interfaces
Calls to methods are intercepted…even when the target bean doesn’t have the
method!
•
Actually, just a special case of around advice

AMIS KC Web & Java - Spring Framework – Power to the POJO
36
Creating Advise

 Create a class that implements one or more of
the Spring AOP interfaces




MethodInterceptor
BeforeAdvice
AfterReturningAdvice
ThrowsAdvice
 Implement the interface method
 before (Method method, Object[] args)
 afterReturning(Object returnValue, Method method,
Object[] args)
 invoke(MethodInvocation invocation)

AMIS KC Web & Java - Spring Framework – Power to the POJO
37
Defining Pointcuts in Spring
(specify where to apply which Advice)

 Programmatically
 No BeanFactory required
 Can be used independently of the rest of Spring
 Declaratively
 In the bean container configuration file
(applicationContext.xml)

AMIS KC Web & Java - Spring Framework – Power to the POJO
38
Applications of AOP by Spring itself
always in conjunction with IoC/DI

 Remoting Support
 Proxy references a remote object
 Transaction Management
 Service method is wrapped in around advice that
opens and closes the transaction
 Security
 JMX
 Proxy implements the MBean interfaces for its target
object
 Mock Testing
 Tested objects are injected with Mock objects that are
dynamically created (made up)

AMIS KC Web & Java - Spring Framework – Power to the POJO
39
AOP through Proxy

Employee
Proxy

AMIS KC Web & Java - Spring Framework – Power to the POJO
EmployeeImpl
40
Future of AOP – according to Rod Johnson

 Programming Aspects and composing an
Application from Aspects will become widely
accepted
 Various orthogonal concerns can be dealt with in
parallel
 Maintaining a single – cross application concern – is
done by maintaining a single aspect
 Tool and Runtime support for AOP will further increase
 Development of IBM WebSphere & WSAD is
heavily done in an AOP fashion

AMIS KC Web & Java - Spring Framework – Power to the POJO
41
Spring’s recommended
Application Guidelines and Architecture

 Program against interfaces
 For example Service Interface, DAO Interfaces
 Typically no interfaces for Domain Classes
 No configuration “plumbing” in your classes
 Have configuration details injected
 Domain Classes are used through all tiers
 No Struts ActionForms to wrap domain classes
 Controllers use Business Service methods to create or
manipulate Domain Objects
 Practice “Test driven development”

AMIS KC Web & Java - Spring Framework – Power to the POJO
42
Spring’s recommended architecture

Presentation Tier
View Components
Generate HTML or PDF
Remote Service Exporters
Web Tier Actions
Using SOAP, RMI, JAX-RPC etc.
(Controllers)
Business Tier
Business Services Layer
Interfaces and Container Managed
Implementations
Data Tier
DAO Interface Layer
Interfaces, independent of
implementing DAO Technology
DAO Implementation layer
Retrieves, saves entities using ORM tool or JDBC
JDBC
AMIS KC Web & Java - Spring Framework – Power to the POJO

RDBMS
JDBC
O/R Mapping Layer
Persistent
Domain
Objects
43
Spring and Test Driven Development

 Agile Software Engineering methods, such as XP
 First design and develop a test based on
interfaces
 Before implementing the interfaces
 Before starting to resolve a bug
 Automated Unit Testing for every class in the
application
 At every stage of development, the test can be rerun!
 Unit Testing usually based on JUnit
 Great integration in Eclipse and JDeveloper 10.1.3
(10.1.2 is somewhat sparse)

AMIS KC Web & Java - Spring Framework – Power to the POJO
44
Spring and Test Driven Development

 Challenges include
 Container Dependencies (HttpServlet object)
 Dependencies on external objects (not a unit test)
•
Especially objects that are hard to configure, e.g. DAO Impl
 Dependencies on objects that have not yet been
implemented

AMIS KC Web & Java - Spring Framework – Power to the POJO
45
Spring support for Test Driven Development

 When all objects (Service and DAO Impl) are
Spring Beans
 They get dependency injected by the container
 During a test, instead of injecting them with real
objects
 We can inject them with Mock Objects, that will return
the values we specify when called
 The real objects do not need to exist
•
even when they do exist, using mock objects ensures we are
performing a true UNIT test

AMIS KC Web & Java - Spring Framework – Power to the POJO
46
Unit Testing HrmServiceImpl
using Mock objects

Unit Test
HrmServiceTest
(JUnit TestCase)
MockEmployeeDAOImpl
Business Tier
HrmServiceImpl
Data Tier
EmployeeDAO
Interfaces, independent of
implementing DAO Technology
EmployeeDAOImpl
(does not yet exist)

AMIS KC Web & Java - Spring Framework – Power to the POJO
Persistent
Domain
Objects
47
Properly implementing those Mock DAO
objects

 … (Aino)

AMIS KC Web & Java - Spring Framework – Power to the POJO
48
Spring and Web Applications
Struts and other Controller Frameworks

 Struts support
 Auto-Load WebContext (== BeanFactory) in session
 Make Action Classes Spring aware and have them
reference the WebContext
 Proxy Action Classes
and Dependency
Inject them
•
Register Actions
as Spring Beans
 Similar support for
 WebWork
 Tapestry

AMIS KC Web & Java - Spring Framework – Power to the POJO
49
Spring and Web Applications
Java Server Faces

 Java Server Faces
 JSF has managed-beans
 Very similar to Spring Beans
 Though no support for AOP
 And: do you want low level, persistency related
configuration details in the faces-config.xml
 JSF-Spring project offers a JSF variable resolver
 It takes bean references in faces-config.xml and tries
to resolve them in Spring context files

AMIS KC Web & Java - Spring Framework – Power to the POJO
50
Spring and Web Applications
Spring MVC Controller Framework

 Positioned to replace Struts
 Better, more intuitive, modern architecture
 Full benefits from IoC and AOP
 Works with (these are all Spring Beans)
 Controllers – that process the request, update and prepare the
Model; they also return a result, a symbolic indication of the
ModelView to proceed to
 ViewResolvers –
that decide which
View to let render
 ViewBeans – that
wrap View Components
such as JSP, Velocity
Template, FreeMarker
page

AMIS KC Web & Java - Spring Framework – Power to the POJO
51
Spring MVC Controller Framework
Support for various View technologies

 JSP – using the Spring tag-library (very small,
primarily use JSTL)
 FreeMarker
 Velocity
 Tiles
 File Download
 Excel – using Apache POI
 PDF – using iText
 XSL-T
 JasperReports

AMIS KC Web & Java - Spring Framework – Power to the POJO
52
Spring Remote
Support for distributed applications

 Spring’s number one concept concerning remote,
distributed objects (take from Martin Fowler):
 Do NOT distribute!!!
 However, in certain circumstances you probably
have to
 Cross organizational boundaries
 Rich Clients
 Remote process
accessing
back-end server

AMIS KC Web & Java - Spring Framework – Power to the POJO
53
Spring’s Remote Façade Philosphy

 Remote access
 for example supporting remote clients over RMI or
publishing a WebService
 should be regarded as an alternative presentation
layer
 no different from an standard Browser oriented HTML
interface
 On well-defined middle tier service interfaces
 that are blissfully unaware that they are exposed and
consumed remotely
 Remoting infrastructure – for example Data
Transfer Objects – should be added on top of the
well defined, OO, fully POJO based service

AMIS KC Web & Java - Spring Framework – Power to the POJO
54
Spring support for “remoting”

 Spring will
 Declaratively expose Service interfaces for remote
clients
 Declaratively expose remote Service interfaces for
local clients
 Support for these protocols:





RMI
Caucho’s Hessian and Burlap
Spring’s own HttpInvoker
EJB
SOAP (based on JAX-RPC, using AXIS or XFire)

AMIS KC Web & Java - Spring Framework – Power to the POJO
55
Exposing Service to remote clients

ClientCode
Proxy for Service
(generated by BeanFactory)
Service Interface
Spring Exporter
Service
Implementation

AMIS KC Web & Java - Spring Framework – Power to the POJO
56
Spring Exporters

 Declaratively set up in the configuration file
 Using a specific protocol
 Spring Service Exporter
 defines an End Point where the Service can be
invoked
•
typically linked to a port
 translates incoming remote calls to local calls
•
•
unmarshalling parameter values
marshalling return values and exceptions
 Spring Exporter often works with Spring Remote
Client

AMIS KC Web & Java - Spring Framework – Power to the POJO
57
Details from Server and Client side
BeanConfiguration files

 Server Side – HttpInvoker Protocol Exporter
 Client Side – HttpInvoker Protocol proxy creator

AMIS KC Web & Java - Spring Framework – Power to the POJO
58
Extra Features

 JMX – proxy Spring Beans to register them as
MBeans
 JMS
 Email
 JNDI
 Scheduling (Quartz)
 Transaction Management

AMIS KC Web & Java - Spring Framework – Power to the POJO
59
Spring and the Oracle Java Technology Stack

 Spring DAO has TopLink support
 Since Spring 2005, contributed by Oracle
 Spring based POJO business service can be registered
with ADF Binding Framework
 Ideally we can have the registration mechanism honor the
ApplicationContext origin of the Service Object
 Spring DAO for ADF BC seems pointless
 Spring MVC on ADF BC Business Service could be done
 though a lot of the natural benefits are lost
 UIX could be used with Spring MVC as View technology
 Spring Remoting can be used to publish and consume
 Somewhat overlapping with JDeveloper WebServices support
 Spring AOP could be applied to ADF BC objects
 If we can find the right hook

AMIS KC Web & Java - Spring Framework – Power to the POJO
60
Spring and AMIS

 Spring concepts:





Programming against Interfaces
Strong Domain Model
DAO Interfaces
Service methods to bundle Use Case DAO interactions
Test Driven Development
 Spring DAO and Persistence when we are dealing with
Hibernate, JDBC and maybe TopLink
 Spring Remoting, especially when WebService support in
JDeveloper falls short
 For example with RMI or EJB (if we have to)
 Spring IoC and Dependency Injection
 Very interesting concepts
 Gradually introduce within Business Service

AMIS KC Web & Java - Spring Framework – Power to the POJO
61
Spring and AMIS

 Spring AOP
 AOP has many applications
 Especially for POJO provided by Spring BeanFactory
 Let’s keep a very open mind for using (Spring) AOP
 Spring “toolbox”





Scheduling
Email
JMX
JMS
PDF, Excel, JasperReports

AMIS KC Web & Java - Spring Framework – Power to the POJO
62
Sub Projects and initiatives around Spring







Spring Security
Spring IDE (for Eclipse)
Spring Rich Client
Spring Modules & Spring WebFlow
Spring BeanDoc
Spring .NET – by Rod Johnson et. al.
 Focus on C#




JSF-Spring
XDoclet – Spring Bean Configuration generator
Aurora MVC Framework
More support for Persistency OO/R Frameworks
 EJB 3.0…

AMIS KC Web & Java - Spring Framework – Power to the POJO
63