Introducing the Spring framework

antlertextureSoftware and s/w Development

Jul 14, 2012 (5 years and 2 months ago)

425 views

Languages in Depth
Series: Java Programming

Prof. Dr. Bertrand Meyer

Chair of Software Engineering
Introducing the Spring
framework
Marco Piccioni

2
Languages in Depth series: Java Programming

Yet another framework?



It addresses areas that other frameworks don’t


Does not reinvent the wheel


It is both comprehensive and modular


Offers a simple programming model (based on POJOs)


Open source project since February 2003


> 2.4 million downloads
3
Languages in Depth series: Java Programming

What is it about?



Helps in writing code that is easy to unit test


Effectively organizes middle tier objects


Centralizes configuration


A light weight alternative to EJB’s (most of the time)


Eliminates Singleton proliferation


Consistent framework for data access
4
Languages in Depth series: Java Programming

The overall picture

Picture from http://static.springframework.org/spring/docs/2.0.x/reference/introduction.html
5
Languages in Depth series: Java Programming

A deceivingly simple example

public
class

HelloWorld

{

public
static

void

main
(
String
[]
aargh
)
{

System.out.println
(”
Hello

again
?!");
}
}
6
Languages in Depth series: Java Programming

Requirements happen to change…

What if we want to change the message?
What if we want to output the message differently?


To
stderr
instead of
stdout


Enclosed in
HTML tags
rather than as plain text
New requirements


Our application should support a simple, flexible
mechanism for changing the message


It should be simple to change the rendering behavior.
7
Languages in Depth series: Java Programming

Quiz



Which is the simplest way to change the message
without changing the code?


How to implement a more flexible rendedering behavior?
8
Languages in Depth series: Java Programming

Quiz solution



Which is the simplest way to change the message
without changing the code?


Using it as a command line argument


How to implement a more flexible rendedering behavior?


Separating the component that renders the message
from the component that obtains the message
9
Languages in Depth series: Java Programming

A more flexible design

<<interface>>
MessageRenderer

+setMessageProvider
()
+getMessageProvider
()
+render
()
StandardOutMessageRenderer
+setMessageProvider()
+getMessageProvider()
+render()
<<interface>>
MessageProvider
+getMessage()
HelloWorldMessageProvider
+getMessage()
10
Languages in Depth series: Java Programming

Decoupling hello world

public
class

DecoupledHelloWorld

{

public
static

void

main
(
String
[]
aargh
)
{

MessageRenderer

mr
=
new

StandardOutMessageRenderer
();

MessageProvider

mp
=
new

StandardOutMessageRenderer
();

mr.setMessageProvider
(
mp
);

mr.render
();
}
}
11
Languages in Depth series: Java Programming

Improving the design using a Factory

public
class

DecoupledHelloWorldWithFactory

{

public
static

void

main
(
String
[]
aargh
)
{

MessageRenderer

mr
=
MessageSupportFactory.getInstance
().
getMes
sageRenderer
();

MessageProvider

mp
=
MessageSupportFactory.getInstance
().
getMes
sageProvider
();

mr.setMessageProvider
(
mp
);

mr.render
();
}}
12
Languages in Depth series: Java Programming

Flexibility comes at a price



The factory will be able to create a message renderer
and a message provider by name, reading a
configuration file


We have to write lots of “glue” code


We still have to provide the
MessageRenderer
with an
instance of
MessageProvider
manually
13
Languages in Depth series: Java Programming

Spring teaser

public
class

XmlConfigHelloSpringDI

{

public
static

void

main
(
String
[]
aargh
)
{

BeanFactory

factory
=
getBeanFactory
()

MessageRenderer

mr
= (
MessageRenderer
)
factory.getBean
(“
renderer
”);

mr.render
();
}
//
see

next

page

for

getBeanFactory
()

}
14
Languages in Depth series: Java Programming

More Spring teaser

...


private
static

BeanFactory

getBeanFactory
()

throws

Exception

{

XmlBeanFactory

factory
=
new

XmlBeanFactory
(
new

FileSystemResource
(“
src
/ch/
ethz
/
inf
/java/
hello
/
msgConf.xml
”));

return

factory
;
}
...

15
Languages in Depth series: Java Programming

How Spring config file looks like

<!DOCTYPE
beans
PUBLIC
"-//SPRING/DTD BEAN//EN"
"http://www.springframework.org/
dtd
/
spring-beans.dtd
">
<
beans
>
<
bean

id=
"
renderer

class=

ch.ethz.inf.java.hello.StandardOutMessageRende
rer
"
>

<
property

name=
"
messageProvider
">
<
ref

local=
"provider"
/>

</
property
>
</
bean
>
<
bean

id=
"provider”
class=
"
ch.ethz.inf.java.hello.HelloWorldMessageProvid
er
"/>
</
bean
>
</
beans
>
16
Languages in Depth series: Java Programming

Hello Spring

Here is where the Spring Framework comes into play


Provides lots of well written plumbing code


Sets the dependencies automatically


Your application interacts with the Spring DI container
using the BeanFactory (or the more powerful
ApplicationContext) interface


We can also set values directly in the configuration file,
or using constructor args (e.g. injecting “hello world”)
17
Languages in Depth series: Java Programming

References
[1]

www.springframework.org

[2]

Rob Harrop, Jan Machacek. Pro Spring: Apress 2005
[3] Rod Johnson. Expert One-on-One J2EE Design and Development,
Wrox press 2001
[4] Craig Walls, Ryan Breidenbach. Spring in Action Manning 2005