Loading presentation...

Present Remotely

Send the link below via email or IM

Copy

Present to your audience

Start remote presentation

  • Invited audience members will follow you as you navigate and present
  • People invited to a presentation do not need a Prezi account
  • This link expires 10 minutes after you close the presentation
  • A maximum of 30 users can follow your presentation
  • Learn more about this feature in our knowledge base article

Do you really want to delete this prezi?

Neither you, nor the coeditors you shared it with will be able to recover it again.

DeleteCancel

Make your likes visible on Facebook?

Connect your Facebook account to Prezi and let your likes appear on your timeline.
You can change this under Settings & Account at any time.

No, thanks

Spring Framework

Summary of first few chapters of Spring In Action
by

John Giannelli

on 21 October 2015

Comments (0)

Please log in to add your comment.

Report abuse

Transcript of Spring Framework

Spring Framework
Simplifying Development
Eliminate boilerplate code
Injecting Dependencies
Dependency Injection (DI) is central to Spring

Applying Aspects
Aspect Oriented Programming (AOP) enables you to capture functionality into reusable chunks.
Eliminate Boilerplate Code
POJOs
Many frameworks 'lock you in' by forcing you to extend their classes or APIs.
Plain Old Java Objects
This could be a spring bean:



Separation of Concerns
Examples
logging
transaction management
security
AOP In Action
A Minstrel to sing the praises of Knights.
With DI Code
Taken from
Spring In Action
4th Edition
Craig Wall
Manning Publications

Traditional Method
Any non-trivial program is made of many interacting classes.

Each object is responsible for its own references.

This leads to
highly-coupled
and
hard-to-test
code.
Highly Coupled
Example (from Spring In Action)
Tightly Coupled to RescueDamselQuest
What if the knight was required to go on a SlayDragonQuest?

Dependency Injection

Quest is injected

Testing is easier ...

Testing is Easier
Create mockQuest

Inject Quest
Spring DI
(Wiring)
The act of creating associations between objects is called wiring.
Wiring Via XML
Create SlayDragonQuest
Inject quest bean
Wiring Via Java Config
Seeing It Work
Load Spring context (via XML)
Get bean from context
Use bean
These are
cross-cutting concerns

because they can span multiple
components of the system.
The Minstrel
Getting the Minstrel To Sing
Traditional

Should a knight manage his own minstrel?
Traditional vs AOP Methods
AOP
Reference to a Minstrel
Convert Minstrel into an Aspect

(using XML configuration - can use Java instead)
Declare minstrel
Define pointcut
Declare before advice
Declare after advice

(POJO)
The Knight knows nothing about Minstrel!
method found by poincut expression
before advice inserted here
after advice inserted here
Typical (bad) Example
Using Templates
Spring Container

AnnotationConfigApplicationContext
- load from Java based config

AnnotationConfigWebApplicationContext
- load a web based context

ClassPathXmlApplicationContext

- load a context from XML files on classpath

FileSystemXmlApplicationContext
- load a context from XML files on filesystem

XmlWebApplicationContext

- load from web application
Different Types of Context ...
A Bean's Life
Wiring Beans
Wiring Beans Automatically
Automatic Wiring
Make Bean Discoverable
Enable Component Scanning
(Preferred)
Scans all
@Components
found below
package soundsystem
Enable Component Scanning
In XML
scan everything below
package soundsystem
Test It!
Tests that cd has
been autowired
Load Config
classes Annotated
as @Component
Going Further ...
Java Dependency Injection specification (JSR-330)
@Component -> @Named
@Autowire -> @Inject
JUnit utility that allows us to check console output
Wiring With Java
Singletons ...
Config ...
Test ...
cd1
and
cd2
are the SAME object
player1
and
player2
are DIFFERENT objects
Can you explain why?
Wiring With XML
Note: Prefer Java to XML. One major advantage is compile time validation. See the book for more reasons!
See book or spring docs for more details.
Injecting External Values
Declare a property source ...

,,, then retrieve values

Spring environment will have been populated with values
Scoping Beans
Singleton
- One Instance created for entire application (the default)
Prototype
- Another instance created each time this bean is injected
Session
- In a web app, one instance created for each session
Request
- In a web app, one instance created for each request
In Class

@Component
@Scope(configurableBeanFactory.SCOPE_PROTOTYPE)
public class Notepad (...)
@Bean
@Scope(configurableBeanFactory.SCOPE_PROTOTYPE)
public class Notepad (...)
In Java Config
In XML Config
<bean id="notepad" lass="com.myapp.Notepad"
scope="prototype" />

SpEL
PropertySourcesPlaceholderConfigurer
A few examples (see the Spring In Action book for details)
#{T(System).currentTimeMillis()}
#{artistSelector.selectArtist()?.toUpperCase()}
Worked Example
- Spring and JDBC

DAO
DAO
(Repository)

Spring's Unchecked Exceptions
DataSource
JDBC Templates
Pulling It Together
Based on Chapter 10 JDBC Example of
Spring In Action
4th Edition,
Craig Walls
GitHub location:

https://github.com/habuma/SpringInActio§nExamples/tree/master/Chapter_10
Buy book at Manning Publications:
http://www.manning.com/walls5/
Only relevant data access methods are exposed through the interface and allow clear decoupling between component parts of the system.

The role of interfaces for DAO models is well known but should be a principle used across ALL components of your system.

Design
Domain Classes (POJOs)
Spitter Class
Spittle Class
Just Plain Old Java Objects ...
Data Access Interfaces
(similar interface for Spitter)
Data Access Implementation
(and similar for Spitter)
JDBCTemplate
(described next)
Normal SQL
ROW Mapper
Note: no reference so far to DB or connection details
Interfaces
Interface Implementations
Domain Classes
JDBC (without spring) is littered with try/catch statements for SQLException.

This is a 'checked exception'.
(this is bad because )
Spring replaces this with mappings for 'unchecked exceptions'.

Making for cleaner, easier to follow and maintain code.
Some arguments against checked exceptions (there are hundreds of articles!)
http://www.mindview.net/Etc/Discussions/CheckedExceptions
http://radio-weblogs.com/0122027/stories/2003/04/01/JavasCheckedExceptionsWereAMistake.html
http://tapestryjava.blogspot.co.uk/2011/05/tragedy-of-checked-exceptions.html
http://java.dzone.com/articles/checked-exceptions-i-love-you
(but you have to go!)
Spring offers many options:
JDBC ds
JNDI lookup ds
Pooled Connections ds
JDBC sources
Some examples:
DriverManagerDataSource
SimpleDriverDataSource
SingleConnectionDataSource
XML Config Examples
Embedded DB useful for unit testing
Spring abstracts away the boilerplate data access code behind
templates
.
Three types:
All the template requires is a
DataSource
Testing It!
Autowire the Repository - everything else is then automatic!
No need to know if this data comes from DB or file or wherever ...
Tell it how to wire.
Special spring junit runner
Going Further ...
Find out about
profiles
and how to use them to configure different datasources for test, dev, and production:
Find out about
TransactionManagers
(skipped over in the example!)
References
http://www.manning.com/walls5/
http://www.tutorialspoint.com/spring/spring_jdbc_framework.htm
https://github.com/habuma/SpringInActionExamples
Service knows nothing about db.
Interface allows repository to be easily changed/mocked.
Example Accessing E01 DB
Over To David ! ......
Lightweight and minimally invasive development with POJOs
Loose coupling through DI
Declarative programming through aspects and common conventions
EJB
Struts
Tapestry
You
CAN
use spring with just ...
easier to
test
easier to
understand
is
simpler
Wiring with Java
Wiring with XML
Injecting external values
Spring Expression Language
or (
)
(JDBC Exceptions vs Spring's Data Access Exceptions)
Configure a Data Source
JDBC Templates
Pulling it all together
Example of a CRUD interface.
JdbcTemplate
NamedParameterJdbcTemplate
SimpleJdbcTemplate
Presented by JohnGiannelli
Watermark 13th Nov 2014

(no reference to spring)
Full transcript