SlideShare a Scribd company logo
1 of 97
Introduc)on to Spring
Ilio Catallo - info@iliocatallo.it
Outline
• What is Dependency Injec2on?
• The Spring Framework
• Wri2ng applica2ons in Spring
• References
What is Dependency Injec1on?
Components
Applica'ons typically consist of components that work together to
form what the user sees as a coherent whole
Movie rental
Assume that we need to write a component that provides a list of
movies directed by a par5cular director1
1
This use case is an adapta/on from the Mar/n Fowler's seminal example on Inversion of Control (see references)
Movie rental
Moreover, assume we are told that movies are currently being
stored in a comma delimited file
MovieService
A"er much delibera/on, we delineate the following component,
which we decide to call MovieService
public class MovieService {
private CSVMovieRepository repository;
public List<Movie> moviesDirectedBy(String directorName) { ... }
}
MovieService
Note that MovieService depends on CSVMovieRepository
public class MovieService {
private CSVMovieRepository repository;
public List<Movie> moviesDirectedBy(String directorName) { ... }
}
CSVMovieRepository
CSVMovieRepository is an addi'onal component that abstracts
the comma delimited file containing the list of movies
public class CSVMovieRepository {
public CSVMovieRepository(String filename) { ... }
public List<Movie> findAll() { ... }
}
The moviesDirectedBy use case
An immediate implementa,on of moviesDirectedBy() is
therefore as follows:
public List<Movie> moviesDirectedBy(String directorName) {
List<Movie> movies = repository.findAll();
Iterator<Movie> it = movies.iterator();
while (it.hasNext()) {
Movie movie = it.next();
if (!movie.getDirector().equals(directorName))
it.remove();
}
return movies;
}
The moviesDirectedBy use case
Alterna(vely, we can use the Stream API in order to obtain a terser
implementa(on
public List<Movie> moviesDirectedBy(String directorName) {
repository.findAll()
.stream()
.filter(m -> m.getDirector().equals(directorName))
.collect(Collectors.toList());
}
MovieService class
public class MovieService {
private CSVMovieRepository repository;
public MovieService() {
this.repository = new CSVMovieRepository("movies.csv");
}
public List<Movie> moviesDirectedBy(String directorName) {
List<Movie> movies = repository.findAll();
Iterator<Movie> it = movies.iterator();
while (it.hasNext()) {
Movie movie = it.next();
if (!movie.getDirector().equals(directorName))
it.remove();
}
return movies;
}
}
Job done?
Unit tes(ng MovieService
Assume that we now want to unit test MovieService
public class MovieServiceTest {
@Test
public void moviesDirectedByTest() {
...
}
}
Unit tes(ng MovieService
We want to ensure the correctness of moviesDirectedBy() by
checking its output against a set of controlled movies
Unit tes(ng MovieService
For instance, given the following data
Sophia Coppola, Lost in Translation
Christopher Nolan, Inception
Christopher Nolan, The Dark Knight
Unit tes(ng MovieService
We want to test if moviesDirectedBy("Sophia Coppola")
returns the expected output
@Test
public void moviesDirectedByTest() {
List<Movie> movies = service.moviesDirectedBy("Shopia Coppola");
assertTrue(movies.length == 1);
assertTrue(movies.get(0).getDirector().equals("Sophia Coppola"));
assertTrue(movies.get(0).getTitle().equals("Lost in Translation"));
}
MockMovieRepository
We can therefore devise a MockMovieRepository:
public class MockMovieRepository {
public List<Movie> findAll() {
return Arrays.asList(
new Movie("Sophia Coppola", "Lost in Translation"),
new Movie("Christopher Nolan", "Inception"),
new Movie("Christopher Nolan", "The Dark Knight")
);
}
}
Unit tes(ng MovieService
To use our test data, we need to replace CSVMovieRepository
with MockMovieRepository
Unit tes(ng MovieService
We need to unplug MovieService from CSVMovieRepository
Unit tes(ng MovieService
However, there is no way to replace CSVMovieRepository with
MockMovieRepository
public class MovieService {
private CSVMovieRepository repository;
public MovieService() {
this.repository = new CSVMovieRepository("movies.csv");
}
}
Unit tes(ng MovieService
This is because MovieService instan-ates its own dependency
public class MovieService {
private CSVMovieRepository repository;
public MovieService() {
this.repository = new CSVMovieRepository("movies.csv");
}
}
MovieService cannot be tested
Thus, we found out that our solu1on is not unit testable
public class MovieService {
private CSVMovieRepository repository;
public MovieService() {
this.repository = new CSVMovieRepository("movies.csv");
}
}
What could ever happen?
¯_( )_/¯
What could ever happen?
Variability
Apart from testability, we may face an even bigger issue with our
solu7on
Variability
Assume that we are suddenly been told that from now on movies
are going to be stored in a rela%on database
Variability
That requires changing MovieService, even though the real
change involves a different component
public class MovieService {
public SQLMovieRepository repository;
public MovieService() {
this.repository = new SQLMovieRepository(...);
}
...
}
Obtaining the movie list
This appears even more suspicious if we no2ce that the
implementa2on of moviesDirectedBy() does not change
public List<Movie> moviesDirectedBy(String directorName) {
List<Movie> movies = repository.findAll();
...
}
Obtaining the movie list
As a ma&er of fact, MovieService does not need to know how
the movies are stored
public List<Movie> moviesDirectedBy(String directorName) {
List<Movie> movies = repository.findAll();
...
}
Obtaining the movie list
There are many different ways of storing the movie list
• Database
• File system
• Remote web service
MovieRepository as an interface
We can explicitly model this variability by defining a
MovieRepository interface with a unique method
public interface MovieRepository {
List<Movie> findAll();
}
MovieRepository as an interface
Each MovieRepository implementa-on has its own way of
retrieving the data
public class SQLMovieRepository implements MovieRepository {...}
public class CSVMovieRepository implements MovieRepository {...}
public class RESTMovieRepository implements MovieRepository {...}
External dependency
Instead of le,ng MovieService look up the right
implementa5on, we provide it as an external dependency
public class MovieService {
private MovieRepository repository;
public MovieService(MovieRepository repository) {
this.repository = repository;
}
...
}
Composing objects
We can now provide different implementa2ons of
MovieRepository to MovieService
public class MovieService {
private MovieRepository repository;
public MovieService(MovieRepository repository) {
this.repository = repository;
}
...
}
Composing objects
This allows different developers to reuse the same components in
different situa5ons
public class MovieService {
private MovieRepository repository;
public MovieService(MovieRepository repository) {
this.repository = repository;
}
...
}
Dependency inversion principle
What we did in the above is a direct applica3on of the dependency
inversion principle
Dependency inversion principle
1. High-level modules should not depend on low-level modules.
Both should depend on abstrac:ons
2. Abstrac:ons should not depend upon details. Details should
depend upon abstrac:ons
Both should depend on abstrac1ons
+--------------+ +-----------------+
| | | |
| MovieService +----------> MovieRepository |
| | | |
+--------------+ +--------^--------+
|
|
|
+---------+----------+
| |
| SQLMovieRepository |
| |
+--------------------+
Details should depend upon abstrac2ons
Details should depend upon abstrac2ons
+--------------------------------------------------+
| |
| +--------------+ +-----------------+ |
| | | | | |
| | MovieService +----------> MovieRepository | |
| | | | | |
| +--------------+ +--------^--------+ |
| | |
+--------------------------------------------------+
|
+---------+----------+
| |
| SQLMovieRepository |
| |
+--------------------+
Testability
As a welcome byproduct, we gain the possibility of tes8ng
MovieService
@Test
public void moviesDirectedByTest() {
MovieService ms = new MovieService(new MockMovieRepository());
List<Movie> movies = ms.moviesDirectedBy("Sophia Coppola");
assertTrue(movies.length == 1);
assertTrue(movies.get(0).getDirector().equals("Sophia Coppola"));
assertTrue(movies.get(0).getTitle().equals("Lost in Translation"));
}
Job done?
Deciding for a MovieRepository
At a certain point we need to decide for a specific implementa-on
of MovieRepository
Factories
To this end, we write a factory class, whose responsibility is to
provide fully-constructed MovieService objects
Factories
For instance, we could devise the following simple factory2
public class MovieServiceFactory {
public MovieService create(String type) {
if (type.equals("CSV")) return new MovieService(new CSVMovieRepository(...));
if (type.equals("SQL")) ...
return null;
}
}
2
Here, we are using the so called simple factory pa/ern. Although it is in turn sub-op9mal, we decided for this
pa<ern because of the similarity of its interface with that of Spring's own factories
Factories
We can finally obtain a fully-constructed MovieService
public static void main(String[] args) {
...
MovieService service = factory.create("SQL");
List<Movie> movies = service.moviesDirectedBy("Sophia Coppola");
}
Wiring objects
Developers of the applica0on are in charge of wiring the different
components together
Wiring objects
That is, developers are in control of the assembly phase of the
applica5on components
Wiring objects
Developers would happily give up this control to someone else
Wiring objects
That is, developers would happily stop wri$ng factories
Wiring objects
We would like a 3rd
-party factory to take care of crea0ng and
wiring the different objects of the applica0on on our behalf
MovieService movieService = externFactory.getComponent("SQL");
Dependency Injec+on
Dependency injec+on (DI) is a pa*ern whereby the responsibility
of crea7ng and wiring applica7on objects is deferred to a 3rd
-party
factory
Dependency Injec+on
Namely, the 3rd
-party factory will:
• Instan'ate components
• Provide dependencies to each component
• Manage the lifecycle of each component
The Spring framework
The Spring framework
Spring is a Java framework that provides comprehensive
infrastructure support for developing Java applica6ons
Spring DI Containers
In Spring, applica-on components are instan&ated and managed
directly by the framework
Spring DI Containers
Objects live within a dependency-inversion container
+-------------------------------+
| +-------+ +-------+ |
| | | | | |
| | A +------> B | |
| | | | | |
| +---^---+ +---^---+ |
| | | |
| | | |
| +---+---+ | |
| | | | |
| | C +----------+ |
| | | |
| +-------+ |
+-------------------------------+
Spring DI container
Spring DI Containers
Such a DI container plays the role of the 3rd
-party factory
+-------------------------------+
| +-------+ +-------+ |
| | | | | |
| | A +------> B | |
| | | | | |
| +---^---+ +---^---+ |
| | | |
| | | |
| +---+---+ | |
| | | | |
| | C +----------+ |
| | | |
| +-------+ |
+-------------------------------+
Spring DI container
Spring DI Containers
It is responsible for instan&a&ng and assembling the objects
+-------------------------------+
| +-------+ +-------+ |
| | | | | |
| | A +------> B | |
| | | | | |
| +---^---+ +---^---+ |
| | | |
| | | |
| +---+---+ | |
| | | | |
| | C +----------+ |
| | | |
| +-------+ |
+-------------------------------+
Spring DI container
Spring beans
A bean is an applica)on object that is instan)ated, and otherwise
managed by a Spring DI container
+-------------------------------+
| +-------+ +-------+ |
| | | | | |
| | A +------> B | |
| | | | | |
| +---^---+ +---^---+ |
| | | |
| | | |
| +---+---+ | |
| | | | |
| | C +----------+ |
| | | |
| +-------+ |
+-------------------------------+
Spring DI container
Spring DI Containers
Spring comes with two families of containers:
• Containers that implement the BeanFactory interface
• Containers that implement the ApplicationContext
interface
BeanFactory
The BeanFactory interface provides basic support for DI
ApplicationContext
The ApplicationContext interface extends BeanFactory,
providing addi4onal func4onali4es
• Interna(onaliza(on support
• The ability to load resource file
Bean retrieving
The ApplicationContext interface models a sophis2cated
implementa2on of the factory pa*ern
T getBean(String name, Class<T> requiredType)
Bean retrieving
By using the getBean() method, developers can retrieve
instances of the applica7on beans
T getBean(String name, Class<T> requiredType)
Bean retrieving
Example of bean retrieving:
ApplicationContext c = new ClassPathXmlApplicationContext("ex1.xml");
MovieService movieService = c.getBean("SQL", MovieService.class);
Bean retrieving
ClassPathXmlApplicationContext is an implementa+on of
ApplicationContext
ApplicationContext c = new ClassPathXmlApplicationContext("ex1.xml");
MovieService movieService = c.getBean("SQL", MovieService.class);
Configura)on metadata
The container gets instruc/ons on what objects to instan/ate and
configure by reading some configura)on metadata
+ Business objects
|
|
|
|
+---------v---------+
| |
| Spring |
+-------------> Container |
| |
Configuration +---------+---------+
metadata |
(e.g., XML file) |
|
v Fully configured
system
Configura)on metadata
Through the configura.on metadata, the developer tells the Spring
container how to assemble objects
+ Business objects
|
|
|
|
+---------v---------+
| |
| Spring |
+-------------> Container |
| |
Configuration +---------+---------+
metadata |
(e.g., XML file) |
|
v Fully configured
system
Configura)on metadata
The configuring metadata can be represented in:
• XML
• Java annota,on
• Java code
Configura)on metadata
The configura-on metadata ex1.xml is loaded from the
CLASSPATH
ApplicationContext c = new ClassPathXmlApplicationContext("ex1.xml");
MovieService movieService = c.getBean("SQL", MovieService.class);
Dependencies vs. business logic
The DI Container allows decoupling the specifica(on of
dependencies from the actual program logic
Wri$ng applica$ons with Spring
POJOs
Many Java frameworks require developers to extend classes or
implement interfaces provided by the framework itself
public class MyServlet extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response);
public void doPost(HttpServletRequest request,
HttpServletResponse response);
public void init();
public void destroy();
}
POJOs
Spring beans are instead Plain Old Java Objects (POJOs)
public class MovieService {
private MovieRepository repository;
public MovieService(MovieRepository repository) {
this.repository = repository;
}
...
}
POJOs
Because of this, components in a Spring-based applica8on o9en
have no indica(on that they are being used by Spring
Movie rental
Assume that we want to take advantage of the Spring framework
for our movie rental applica.on
Movie rental
We need to instruct the Spring DI container about the dependency
between MovieService and MovieRepository
+--------------+ +-----------------+
| | | |
| MovieService +----------> MovieRepository |
| | | |
+--------------+ +--------^--------+
|
|
|
+---------+----------+
| |
| SQLMovieRepository |
| |
+--------------------+
Configuring the container
We need to configure Spring to tell it what beans it should contain,
and how to wire those beans
+---------------------------------------------------+
| +--------------+ +-----------------+ |
| | | | | |
| | MovieService +----------> MovieRepository | |
| | | | | |
| +--------------+ +--------^--------+ |
| | |
| | |
| | |
| +---------+----------+ |
| | | |
| | SQLMovieRepository | |
| | | |
| +--------------------+ |
+---------------------------------------------------+
Spring DI container
Configuring the container
For now, we will focus on tradi2onal XML configura-on
The <beans> element
The root element of the Spring configura4on file is the <beans>
element
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/
spring-beans.xsd">
<!-- CONFIGURE APPLICATION BEANS HERE -->
</beans>
The <bean> element
Each applica)on bean is associated with a <bean> element in the
XML configura)on file
<bean id="inMemoryMovieRepository”
class="it.polimi.awt.repository.InMemoryMovieRepository">
...
</bean>
The <bean> element
Every bean has one (or more) id a0ribute, and a class a0ribute
<bean id="inMemoryMovieRepository”
class="it.polimi.awt.repository.InMemoryMovieRepository">
...
</bean>
The <bean> element
Namely:
• If more iden,fiers are specified, the extra ones are considered as
aliases
• The class a8ribute could either specify the class of the bean to
be constructed or the name of a sta,c factory method
How to inject dependencies
In Spring, dependencies are injected through:
• Construc*on-based injec*on
• Se4er-based injec*on
• Arguments to a factory method
Constructor-based injec1on
Constructor-based injec1on is accomplished by the container
invoking the bean constructor
<bean id="movieService" class="it.polimi.awt.service.MovieService">
<constructor-arg ref="inMemoryMovieRepository"/>
</bean>
Constructor-based injec1on
The <constructor-arg> element allows injec0ng a dependency
through a constructor call
<bean id="movieService" class="it.polimi.awt.spring.MovieService">
<constructor-arg ref="inMemoryMovieRepository"/>
</bean>
Se#er-based injec/on
Se#er-based injec/on is accomplished by the container calling
se3er methods on the bean3
<bean id="lostInTranslation" class="it.polimi.awt.domain.Movie">
<property name="title" value="Lost in Translation"/>
<property name="director" value="Sophia Coppola"/>
</bean>
3
As we will see, Movies are domain objects, which usually do not need injec5on. Here, we are doing this for the
sole sake of presen5ng an example of se>er-based injec5on
Se#er-based injec/on
The <property> element injects the dependency by calling the
property se5er
<bean id="lostInTranslation" class="it.polimi.awt.domain.Movie">
<property name="title" value="Lost in Translation"/>
<property name="director" value="Sophia Coppola"/>
</bean>
Constructor vs. se-ers
As a rule of thumb, use constructor arguments for mandatory
dependencies and se6ers for op*onal dependencies
Spring beans vs. JavaBeans
Despite the similar name, keep in mind that a DI container is not
limited to JavaBeans4
and it can manage virtually any class
4
Recall that JavaBeans are objects with i) only a default constructor; and ii) appropriate ge<ers and se<ers
References
References
• Mar%n Fowler, Inversion of Control and Dependency Injec%on
pa;ern
• SpringSource, Spring Framework Reference
• Craig Walls, Spring in Ac%on (3rd Edi%on), Manning Publica%ons

More Related Content

Viewers also liked

JPQL/ JPA Activity 1
JPQL/ JPA Activity 1JPQL/ JPA Activity 1
JPQL/ JPA Activity 1SFI
 
JPQL/ JPA Activity 3
JPQL/ JPA  Activity 3JPQL/ JPA  Activity 3
JPQL/ JPA Activity 3SFI
 
Web Services Part 2
Web Services Part 2Web Services Part 2
Web Services Part 2patinijava
 
How to bake reactive behavior into your Java EE applications
How to bake reactive behavior into your Java EE applicationsHow to bake reactive behavior into your Java EE applications
How to bake reactive behavior into your Java EE applicationsOndrej Mihályi
 
FinelyMe-JustFit Intro
FinelyMe-JustFit IntroFinelyMe-JustFit Intro
FinelyMe-JustFit IntroCheng Ta Yeh
 
Introduction to developing modern web apps
Introduction to developing modern web appsIntroduction to developing modern web apps
Introduction to developing modern web appsFabricio Epaminondas
 
Continuous integration practices to improve the software quality
Continuous integration practices to improve the software qualityContinuous integration practices to improve the software quality
Continuous integration practices to improve the software qualityFabricio Epaminondas
 
Quickstart for continuous integration
Quickstart for continuous integrationQuickstart for continuous integration
Quickstart for continuous integrationFabricio Epaminondas
 
Java persistence api
Java persistence api Java persistence api
Java persistence api Luis Goldster
 
Spring 4. Part 1 - IoC, AOP
Spring 4. Part 1 - IoC, AOPSpring 4. Part 1 - IoC, AOP
Spring 4. Part 1 - IoC, AOPNakraynikov Oleg
 
20160523 hibernate persistence_framework_and_orm
20160523 hibernate persistence_framework_and_orm20160523 hibernate persistence_framework_and_orm
20160523 hibernate persistence_framework_and_ormKenan Sevindik
 
Cassandra for mission critical data
Cassandra for mission critical dataCassandra for mission critical data
Cassandra for mission critical dataOleksandr Semenov
 
DBM專案環境建置
DBM專案環境建置DBM專案環境建置
DBM專案環境建置Guo Albert
 

Viewers also liked (20)

JPQL/ JPA Activity 1
JPQL/ JPA Activity 1JPQL/ JPA Activity 1
JPQL/ JPA Activity 1
 
JPQL/ JPA Activity 3
JPQL/ JPA  Activity 3JPQL/ JPA  Activity 3
JPQL/ JPA Activity 3
 
Ejb5
Ejb5Ejb5
Ejb5
 
Web Services Part 2
Web Services Part 2Web Services Part 2
Web Services Part 2
 
How to bake reactive behavior into your Java EE applications
How to bake reactive behavior into your Java EE applicationsHow to bake reactive behavior into your Java EE applications
How to bake reactive behavior into your Java EE applications
 
FinelyMe-JustFit Intro
FinelyMe-JustFit IntroFinelyMe-JustFit Intro
FinelyMe-JustFit Intro
 
Introduction to developing modern web apps
Introduction to developing modern web appsIntroduction to developing modern web apps
Introduction to developing modern web apps
 
Continuous integration practices to improve the software quality
Continuous integration practices to improve the software qualityContinuous integration practices to improve the software quality
Continuous integration practices to improve the software quality
 
Quickstart for continuous integration
Quickstart for continuous integrationQuickstart for continuous integration
Quickstart for continuous integration
 
Java persistence api
Java persistence api Java persistence api
Java persistence api
 
Spring 4. Part 1 - IoC, AOP
Spring 4. Part 1 - IoC, AOPSpring 4. Part 1 - IoC, AOP
Spring 4. Part 1 - IoC, AOP
 
Gradle - Build System
Gradle - Build SystemGradle - Build System
Gradle - Build System
 
Java Persistence API
Java Persistence APIJava Persistence API
Java Persistence API
 
20160523 hibernate persistence_framework_and_orm
20160523 hibernate persistence_framework_and_orm20160523 hibernate persistence_framework_and_orm
20160523 hibernate persistence_framework_and_orm
 
Spring Boot Update
Spring Boot UpdateSpring Boot Update
Spring Boot Update
 
Cassandra for mission critical data
Cassandra for mission critical dataCassandra for mission critical data
Cassandra for mission critical data
 
Java persistence api 2.1
Java persistence api 2.1Java persistence api 2.1
Java persistence api 2.1
 
JPA For Beginner's
JPA For Beginner'sJPA For Beginner's
JPA For Beginner's
 
Second Level Cache in JPA Explained
Second Level Cache in JPA ExplainedSecond Level Cache in JPA Explained
Second Level Cache in JPA Explained
 
DBM專案環境建置
DBM專案環境建置DBM專案環境建置
DBM專案環境建置
 

Similar to Introduction To Spring

Clean code via dependency injection + guice
Clean code via dependency injection + guiceClean code via dependency injection + guice
Clean code via dependency injection + guiceJordi Gerona
 
Advanced #6 clean architecture
Advanced #6  clean architectureAdvanced #6  clean architecture
Advanced #6 clean architectureVitali Pekelis
 
Java EE 6 CDI Integrates with Spring & JSF
Java EE 6 CDI Integrates with Spring & JSFJava EE 6 CDI Integrates with Spring & JSF
Java EE 6 CDI Integrates with Spring & JSFJiayun Zhou
 
OSGi Community Event 2010 - Dependencies, dependencies, dependencies
OSGi Community Event 2010 - Dependencies, dependencies, dependenciesOSGi Community Event 2010 - Dependencies, dependencies, dependencies
OSGi Community Event 2010 - Dependencies, dependencies, dependenciesmfrancis
 
Dev309 from asgard to zuul - netflix oss-final
Dev309  from asgard to zuul - netflix oss-finalDev309  from asgard to zuul - netflix oss-final
Dev309 from asgard to zuul - netflix oss-finalRuslan Meshenberg
 
A brief overview of java frameworks
A brief overview of java frameworksA brief overview of java frameworks
A brief overview of java frameworksMD Sayem Ahmed
 
Dependencies, dependencies, dependencies
Dependencies, dependencies, dependenciesDependencies, dependencies, dependencies
Dependencies, dependencies, dependenciesMarcel Offermans
 
Dependency Injection in PHP
Dependency Injection in PHPDependency Injection in PHP
Dependency Injection in PHPKacper Gunia
 
Asynchronous Programming at Netflix
Asynchronous Programming at NetflixAsynchronous Programming at Netflix
Asynchronous Programming at NetflixC4Media
 
(DEV309) From Asgard to Zuul: How Netflix’s Proven Open Source Tools Can Help...
(DEV309) From Asgard to Zuul: How Netflix’s Proven Open Source Tools Can Help...(DEV309) From Asgard to Zuul: How Netflix’s Proven Open Source Tools Can Help...
(DEV309) From Asgard to Zuul: How Netflix’s Proven Open Source Tools Can Help...Amazon Web Services
 
How to ensure Presto scalability 
in multi use case
How to ensure Presto scalability 
in multi use case How to ensure Presto scalability 
in multi use case
How to ensure Presto scalability 
in multi use case Kai Sasaki
 
Mastering the Sling Rewriter by Justin Edelson
Mastering the Sling Rewriter by Justin EdelsonMastering the Sling Rewriter by Justin Edelson
Mastering the Sling Rewriter by Justin EdelsonAEM HUB
 
Mastering the Sling Rewriter
Mastering the Sling RewriterMastering the Sling Rewriter
Mastering the Sling RewriterJustin Edelson
 
Dev ops journey basics and real life
Dev ops journey basics and real lifeDev ops journey basics and real life
Dev ops journey basics and real life🌍 Miguel Rodrigues
 
End to-end testing from rookie to pro
End to-end testing  from rookie to proEnd to-end testing  from rookie to pro
End to-end testing from rookie to proDomenico Gemoli
 
CloudStack Meetup Santa Clara
CloudStack Meetup Santa Clara CloudStack Meetup Santa Clara
CloudStack Meetup Santa Clara NetApp
 
Developing maintainable Cordova applications
Developing maintainable Cordova applicationsDeveloping maintainable Cordova applications
Developing maintainable Cordova applicationsIvano Malavolta
 

Similar to Introduction To Spring (20)

Clean code via dependency injection + guice
Clean code via dependency injection + guiceClean code via dependency injection + guice
Clean code via dependency injection + guice
 
Advanced #6 clean architecture
Advanced #6  clean architectureAdvanced #6  clean architecture
Advanced #6 clean architecture
 
Test-driven serverless
Test-driven serverlessTest-driven serverless
Test-driven serverless
 
Java EE 6 CDI Integrates with Spring & JSF
Java EE 6 CDI Integrates with Spring & JSFJava EE 6 CDI Integrates with Spring & JSF
Java EE 6 CDI Integrates with Spring & JSF
 
OSGi Community Event 2010 - Dependencies, dependencies, dependencies
OSGi Community Event 2010 - Dependencies, dependencies, dependenciesOSGi Community Event 2010 - Dependencies, dependencies, dependencies
OSGi Community Event 2010 - Dependencies, dependencies, dependencies
 
Cqs v2
Cqs v2Cqs v2
Cqs v2
 
Dev309 from asgard to zuul - netflix oss-final
Dev309  from asgard to zuul - netflix oss-finalDev309  from asgard to zuul - netflix oss-final
Dev309 from asgard to zuul - netflix oss-final
 
A brief overview of java frameworks
A brief overview of java frameworksA brief overview of java frameworks
A brief overview of java frameworks
 
Dependencies, dependencies, dependencies
Dependencies, dependencies, dependenciesDependencies, dependencies, dependencies
Dependencies, dependencies, dependencies
 
Dependency Injection in PHP
Dependency Injection in PHPDependency Injection in PHP
Dependency Injection in PHP
 
Asynchronous Programming at Netflix
Asynchronous Programming at NetflixAsynchronous Programming at Netflix
Asynchronous Programming at Netflix
 
(DEV309) From Asgard to Zuul: How Netflix’s Proven Open Source Tools Can Help...
(DEV309) From Asgard to Zuul: How Netflix’s Proven Open Source Tools Can Help...(DEV309) From Asgard to Zuul: How Netflix’s Proven Open Source Tools Can Help...
(DEV309) From Asgard to Zuul: How Netflix’s Proven Open Source Tools Can Help...
 
How to ensure Presto scalability 
in multi use case
How to ensure Presto scalability 
in multi use case How to ensure Presto scalability 
in multi use case
How to ensure Presto scalability 
in multi use case
 
Mastering the Sling Rewriter by Justin Edelson
Mastering the Sling Rewriter by Justin EdelsonMastering the Sling Rewriter by Justin Edelson
Mastering the Sling Rewriter by Justin Edelson
 
Mastering the Sling Rewriter
Mastering the Sling RewriterMastering the Sling Rewriter
Mastering the Sling Rewriter
 
CQS
CQSCQS
CQS
 
Dev ops journey basics and real life
Dev ops journey basics and real lifeDev ops journey basics and real life
Dev ops journey basics and real life
 
End to-end testing from rookie to pro
End to-end testing  from rookie to proEnd to-end testing  from rookie to pro
End to-end testing from rookie to pro
 
CloudStack Meetup Santa Clara
CloudStack Meetup Santa Clara CloudStack Meetup Santa Clara
CloudStack Meetup Santa Clara
 
Developing maintainable Cordova applications
Developing maintainable Cordova applicationsDeveloping maintainable Cordova applications
Developing maintainable Cordova applications
 

More from Ilio Catallo

C++ Standard Template Library
C++ Standard Template LibraryC++ Standard Template Library
C++ Standard Template LibraryIlio Catallo
 
Regular types in C++
Regular types in C++Regular types in C++
Regular types in C++Ilio Catallo
 
Resource wrappers in C++
Resource wrappers in C++Resource wrappers in C++
Resource wrappers in C++Ilio Catallo
 
Memory management in C++
Memory management in C++Memory management in C++
Memory management in C++Ilio Catallo
 
Operator overloading in C++
Operator overloading in C++Operator overloading in C++
Operator overloading in C++Ilio Catallo
 
Multidimensional arrays in C++
Multidimensional arrays in C++Multidimensional arrays in C++
Multidimensional arrays in C++Ilio Catallo
 
Pointers & References in C++
Pointers & References in C++Pointers & References in C++
Pointers & References in C++Ilio Catallo
 
Spring MVC - Wiring the different layers
Spring MVC -  Wiring the different layersSpring MVC -  Wiring the different layers
Spring MVC - Wiring the different layersIlio Catallo
 
Java and Java platforms
Java and Java platformsJava and Java platforms
Java and Java platformsIlio Catallo
 
Spring MVC - Web Forms
Spring MVC  - Web FormsSpring MVC  - Web Forms
Spring MVC - Web FormsIlio Catallo
 
Spring MVC - The Basics
Spring MVC -  The BasicsSpring MVC -  The Basics
Spring MVC - The BasicsIlio Catallo
 
Web application architecture
Web application architectureWeb application architecture
Web application architectureIlio Catallo
 
Gestione della memoria in C++
Gestione della memoria in C++Gestione della memoria in C++
Gestione della memoria in C++Ilio Catallo
 
Puntatori e Riferimenti
Puntatori e RiferimentiPuntatori e Riferimenti
Puntatori e RiferimentiIlio Catallo
 
Java Persistence API
Java Persistence APIJava Persistence API
Java Persistence APIIlio Catallo
 
JSP Standard Tag Library
JSP Standard Tag LibraryJSP Standard Tag Library
JSP Standard Tag LibraryIlio Catallo
 
Internationalization in Jakarta Struts 1.3
Internationalization in Jakarta Struts 1.3Internationalization in Jakarta Struts 1.3
Internationalization in Jakarta Struts 1.3Ilio Catallo
 
Validation in Jakarta Struts 1.3
Validation in Jakarta Struts 1.3Validation in Jakarta Struts 1.3
Validation in Jakarta Struts 1.3Ilio Catallo
 

More from Ilio Catallo (20)

C++ Standard Template Library
C++ Standard Template LibraryC++ Standard Template Library
C++ Standard Template Library
 
Regular types in C++
Regular types in C++Regular types in C++
Regular types in C++
 
Resource wrappers in C++
Resource wrappers in C++Resource wrappers in C++
Resource wrappers in C++
 
Memory management in C++
Memory management in C++Memory management in C++
Memory management in C++
 
Operator overloading in C++
Operator overloading in C++Operator overloading in C++
Operator overloading in C++
 
Multidimensional arrays in C++
Multidimensional arrays in C++Multidimensional arrays in C++
Multidimensional arrays in C++
 
Arrays in C++
Arrays in C++Arrays in C++
Arrays in C++
 
Pointers & References in C++
Pointers & References in C++Pointers & References in C++
Pointers & References in C++
 
Spring MVC - Wiring the different layers
Spring MVC -  Wiring the different layersSpring MVC -  Wiring the different layers
Spring MVC - Wiring the different layers
 
Java and Java platforms
Java and Java platformsJava and Java platforms
Java and Java platforms
 
Spring MVC - Web Forms
Spring MVC  - Web FormsSpring MVC  - Web Forms
Spring MVC - Web Forms
 
Spring MVC - The Basics
Spring MVC -  The BasicsSpring MVC -  The Basics
Spring MVC - The Basics
 
Web application architecture
Web application architectureWeb application architecture
Web application architecture
 
Gestione della memoria in C++
Gestione della memoria in C++Gestione della memoria in C++
Gestione della memoria in C++
 
Array in C++
Array in C++Array in C++
Array in C++
 
Puntatori e Riferimenti
Puntatori e RiferimentiPuntatori e Riferimenti
Puntatori e Riferimenti
 
Java Persistence API
Java Persistence APIJava Persistence API
Java Persistence API
 
JSP Standard Tag Library
JSP Standard Tag LibraryJSP Standard Tag Library
JSP Standard Tag Library
 
Internationalization in Jakarta Struts 1.3
Internationalization in Jakarta Struts 1.3Internationalization in Jakarta Struts 1.3
Internationalization in Jakarta Struts 1.3
 
Validation in Jakarta Struts 1.3
Validation in Jakarta Struts 1.3Validation in Jakarta Struts 1.3
Validation in Jakarta Struts 1.3
 

Recently uploaded

Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operationalssuser3e220a
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
The Contemporary World: The Globalization of World Politics
The Contemporary World: The Globalization of World PoliticsThe Contemporary World: The Globalization of World Politics
The Contemporary World: The Globalization of World PoliticsRommel Regala
 
Presentation Activity 2. Unit 3 transv.pptx
Presentation Activity 2. Unit 3 transv.pptxPresentation Activity 2. Unit 3 transv.pptx
Presentation Activity 2. Unit 3 transv.pptxRosabel UA
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parentsnavabharathschool99
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxlancelewisportillo
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
 
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...JojoEDelaCruz
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfVanessa Camilleri
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Seán Kennedy
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 

Recently uploaded (20)

Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operational
 
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptxINCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
The Contemporary World: The Globalization of World Politics
The Contemporary World: The Globalization of World PoliticsThe Contemporary World: The Globalization of World Politics
The Contemporary World: The Globalization of World Politics
 
Presentation Activity 2. Unit 3 transv.pptx
Presentation Activity 2. Unit 3 transv.pptxPresentation Activity 2. Unit 3 transv.pptx
Presentation Activity 2. Unit 3 transv.pptx
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parents
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx
 
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdf
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 

Introduction To Spring

  • 1. Introduc)on to Spring Ilio Catallo - info@iliocatallo.it
  • 2. Outline • What is Dependency Injec2on? • The Spring Framework • Wri2ng applica2ons in Spring • References
  • 3. What is Dependency Injec1on?
  • 4. Components Applica'ons typically consist of components that work together to form what the user sees as a coherent whole
  • 5. Movie rental Assume that we need to write a component that provides a list of movies directed by a par5cular director1 1 This use case is an adapta/on from the Mar/n Fowler's seminal example on Inversion of Control (see references)
  • 6. Movie rental Moreover, assume we are told that movies are currently being stored in a comma delimited file
  • 7. MovieService A"er much delibera/on, we delineate the following component, which we decide to call MovieService public class MovieService { private CSVMovieRepository repository; public List<Movie> moviesDirectedBy(String directorName) { ... } }
  • 8. MovieService Note that MovieService depends on CSVMovieRepository public class MovieService { private CSVMovieRepository repository; public List<Movie> moviesDirectedBy(String directorName) { ... } }
  • 9. CSVMovieRepository CSVMovieRepository is an addi'onal component that abstracts the comma delimited file containing the list of movies public class CSVMovieRepository { public CSVMovieRepository(String filename) { ... } public List<Movie> findAll() { ... } }
  • 10. The moviesDirectedBy use case An immediate implementa,on of moviesDirectedBy() is therefore as follows: public List<Movie> moviesDirectedBy(String directorName) { List<Movie> movies = repository.findAll(); Iterator<Movie> it = movies.iterator(); while (it.hasNext()) { Movie movie = it.next(); if (!movie.getDirector().equals(directorName)) it.remove(); } return movies; }
  • 11. The moviesDirectedBy use case Alterna(vely, we can use the Stream API in order to obtain a terser implementa(on public List<Movie> moviesDirectedBy(String directorName) { repository.findAll() .stream() .filter(m -> m.getDirector().equals(directorName)) .collect(Collectors.toList()); }
  • 12. MovieService class public class MovieService { private CSVMovieRepository repository; public MovieService() { this.repository = new CSVMovieRepository("movies.csv"); } public List<Movie> moviesDirectedBy(String directorName) { List<Movie> movies = repository.findAll(); Iterator<Movie> it = movies.iterator(); while (it.hasNext()) { Movie movie = it.next(); if (!movie.getDirector().equals(directorName)) it.remove(); } return movies; } }
  • 14. Unit tes(ng MovieService Assume that we now want to unit test MovieService public class MovieServiceTest { @Test public void moviesDirectedByTest() { ... } }
  • 15. Unit tes(ng MovieService We want to ensure the correctness of moviesDirectedBy() by checking its output against a set of controlled movies
  • 16. Unit tes(ng MovieService For instance, given the following data Sophia Coppola, Lost in Translation Christopher Nolan, Inception Christopher Nolan, The Dark Knight
  • 17. Unit tes(ng MovieService We want to test if moviesDirectedBy("Sophia Coppola") returns the expected output @Test public void moviesDirectedByTest() { List<Movie> movies = service.moviesDirectedBy("Shopia Coppola"); assertTrue(movies.length == 1); assertTrue(movies.get(0).getDirector().equals("Sophia Coppola")); assertTrue(movies.get(0).getTitle().equals("Lost in Translation")); }
  • 18. MockMovieRepository We can therefore devise a MockMovieRepository: public class MockMovieRepository { public List<Movie> findAll() { return Arrays.asList( new Movie("Sophia Coppola", "Lost in Translation"), new Movie("Christopher Nolan", "Inception"), new Movie("Christopher Nolan", "The Dark Knight") ); } }
  • 19. Unit tes(ng MovieService To use our test data, we need to replace CSVMovieRepository with MockMovieRepository
  • 20. Unit tes(ng MovieService We need to unplug MovieService from CSVMovieRepository
  • 21. Unit tes(ng MovieService However, there is no way to replace CSVMovieRepository with MockMovieRepository public class MovieService { private CSVMovieRepository repository; public MovieService() { this.repository = new CSVMovieRepository("movies.csv"); } }
  • 22. Unit tes(ng MovieService This is because MovieService instan-ates its own dependency public class MovieService { private CSVMovieRepository repository; public MovieService() { this.repository = new CSVMovieRepository("movies.csv"); } }
  • 23. MovieService cannot be tested Thus, we found out that our solu1on is not unit testable public class MovieService { private CSVMovieRepository repository; public MovieService() { this.repository = new CSVMovieRepository("movies.csv"); } }
  • 24. What could ever happen? ¯_( )_/¯
  • 25. What could ever happen?
  • 26. Variability Apart from testability, we may face an even bigger issue with our solu7on
  • 27. Variability Assume that we are suddenly been told that from now on movies are going to be stored in a rela%on database
  • 28. Variability That requires changing MovieService, even though the real change involves a different component public class MovieService { public SQLMovieRepository repository; public MovieService() { this.repository = new SQLMovieRepository(...); } ... }
  • 29. Obtaining the movie list This appears even more suspicious if we no2ce that the implementa2on of moviesDirectedBy() does not change public List<Movie> moviesDirectedBy(String directorName) { List<Movie> movies = repository.findAll(); ... }
  • 30. Obtaining the movie list As a ma&er of fact, MovieService does not need to know how the movies are stored public List<Movie> moviesDirectedBy(String directorName) { List<Movie> movies = repository.findAll(); ... }
  • 31. Obtaining the movie list There are many different ways of storing the movie list • Database • File system • Remote web service
  • 32. MovieRepository as an interface We can explicitly model this variability by defining a MovieRepository interface with a unique method public interface MovieRepository { List<Movie> findAll(); }
  • 33. MovieRepository as an interface Each MovieRepository implementa-on has its own way of retrieving the data public class SQLMovieRepository implements MovieRepository {...} public class CSVMovieRepository implements MovieRepository {...} public class RESTMovieRepository implements MovieRepository {...}
  • 34. External dependency Instead of le,ng MovieService look up the right implementa5on, we provide it as an external dependency public class MovieService { private MovieRepository repository; public MovieService(MovieRepository repository) { this.repository = repository; } ... }
  • 35. Composing objects We can now provide different implementa2ons of MovieRepository to MovieService public class MovieService { private MovieRepository repository; public MovieService(MovieRepository repository) { this.repository = repository; } ... }
  • 36. Composing objects This allows different developers to reuse the same components in different situa5ons public class MovieService { private MovieRepository repository; public MovieService(MovieRepository repository) { this.repository = repository; } ... }
  • 37. Dependency inversion principle What we did in the above is a direct applica3on of the dependency inversion principle
  • 38. Dependency inversion principle 1. High-level modules should not depend on low-level modules. Both should depend on abstrac:ons 2. Abstrac:ons should not depend upon details. Details should depend upon abstrac:ons
  • 39. Both should depend on abstrac1ons +--------------+ +-----------------+ | | | | | MovieService +----------> MovieRepository | | | | | +--------------+ +--------^--------+ | | | +---------+----------+ | | | SQLMovieRepository | | | +--------------------+
  • 40. Details should depend upon abstrac2ons
  • 41. Details should depend upon abstrac2ons +--------------------------------------------------+ | | | +--------------+ +-----------------+ | | | | | | | | | MovieService +----------> MovieRepository | | | | | | | | | +--------------+ +--------^--------+ | | | | +--------------------------------------------------+ | +---------+----------+ | | | SQLMovieRepository | | | +--------------------+
  • 42. Testability As a welcome byproduct, we gain the possibility of tes8ng MovieService @Test public void moviesDirectedByTest() { MovieService ms = new MovieService(new MockMovieRepository()); List<Movie> movies = ms.moviesDirectedBy("Sophia Coppola"); assertTrue(movies.length == 1); assertTrue(movies.get(0).getDirector().equals("Sophia Coppola")); assertTrue(movies.get(0).getTitle().equals("Lost in Translation")); }
  • 44. Deciding for a MovieRepository At a certain point we need to decide for a specific implementa-on of MovieRepository
  • 45. Factories To this end, we write a factory class, whose responsibility is to provide fully-constructed MovieService objects
  • 46. Factories For instance, we could devise the following simple factory2 public class MovieServiceFactory { public MovieService create(String type) { if (type.equals("CSV")) return new MovieService(new CSVMovieRepository(...)); if (type.equals("SQL")) ... return null; } } 2 Here, we are using the so called simple factory pa/ern. Although it is in turn sub-op9mal, we decided for this pa<ern because of the similarity of its interface with that of Spring's own factories
  • 47. Factories We can finally obtain a fully-constructed MovieService public static void main(String[] args) { ... MovieService service = factory.create("SQL"); List<Movie> movies = service.moviesDirectedBy("Sophia Coppola"); }
  • 48. Wiring objects Developers of the applica0on are in charge of wiring the different components together
  • 49. Wiring objects That is, developers are in control of the assembly phase of the applica5on components
  • 50. Wiring objects Developers would happily give up this control to someone else
  • 51. Wiring objects That is, developers would happily stop wri$ng factories
  • 52. Wiring objects We would like a 3rd -party factory to take care of crea0ng and wiring the different objects of the applica0on on our behalf MovieService movieService = externFactory.getComponent("SQL");
  • 53. Dependency Injec+on Dependency injec+on (DI) is a pa*ern whereby the responsibility of crea7ng and wiring applica7on objects is deferred to a 3rd -party factory
  • 54. Dependency Injec+on Namely, the 3rd -party factory will: • Instan'ate components • Provide dependencies to each component • Manage the lifecycle of each component
  • 56. The Spring framework Spring is a Java framework that provides comprehensive infrastructure support for developing Java applica6ons
  • 57. Spring DI Containers In Spring, applica-on components are instan&ated and managed directly by the framework
  • 58. Spring DI Containers Objects live within a dependency-inversion container +-------------------------------+ | +-------+ +-------+ | | | | | | | | | A +------> B | | | | | | | | | +---^---+ +---^---+ | | | | | | | | | | +---+---+ | | | | | | | | | C +----------+ | | | | | | +-------+ | +-------------------------------+ Spring DI container
  • 59. Spring DI Containers Such a DI container plays the role of the 3rd -party factory +-------------------------------+ | +-------+ +-------+ | | | | | | | | | A +------> B | | | | | | | | | +---^---+ +---^---+ | | | | | | | | | | +---+---+ | | | | | | | | | C +----------+ | | | | | | +-------+ | +-------------------------------+ Spring DI container
  • 60. Spring DI Containers It is responsible for instan&a&ng and assembling the objects +-------------------------------+ | +-------+ +-------+ | | | | | | | | | A +------> B | | | | | | | | | +---^---+ +---^---+ | | | | | | | | | | +---+---+ | | | | | | | | | C +----------+ | | | | | | +-------+ | +-------------------------------+ Spring DI container
  • 61. Spring beans A bean is an applica)on object that is instan)ated, and otherwise managed by a Spring DI container +-------------------------------+ | +-------+ +-------+ | | | | | | | | | A +------> B | | | | | | | | | +---^---+ +---^---+ | | | | | | | | | | +---+---+ | | | | | | | | | C +----------+ | | | | | | +-------+ | +-------------------------------+ Spring DI container
  • 62. Spring DI Containers Spring comes with two families of containers: • Containers that implement the BeanFactory interface • Containers that implement the ApplicationContext interface
  • 63. BeanFactory The BeanFactory interface provides basic support for DI
  • 64. ApplicationContext The ApplicationContext interface extends BeanFactory, providing addi4onal func4onali4es • Interna(onaliza(on support • The ability to load resource file
  • 65. Bean retrieving The ApplicationContext interface models a sophis2cated implementa2on of the factory pa*ern T getBean(String name, Class<T> requiredType)
  • 66. Bean retrieving By using the getBean() method, developers can retrieve instances of the applica7on beans T getBean(String name, Class<T> requiredType)
  • 67. Bean retrieving Example of bean retrieving: ApplicationContext c = new ClassPathXmlApplicationContext("ex1.xml"); MovieService movieService = c.getBean("SQL", MovieService.class);
  • 68. Bean retrieving ClassPathXmlApplicationContext is an implementa+on of ApplicationContext ApplicationContext c = new ClassPathXmlApplicationContext("ex1.xml"); MovieService movieService = c.getBean("SQL", MovieService.class);
  • 69.
  • 70. Configura)on metadata The container gets instruc/ons on what objects to instan/ate and configure by reading some configura)on metadata + Business objects | | | | +---------v---------+ | | | Spring | +-------------> Container | | | Configuration +---------+---------+ metadata | (e.g., XML file) | | v Fully configured system
  • 71. Configura)on metadata Through the configura.on metadata, the developer tells the Spring container how to assemble objects + Business objects | | | | +---------v---------+ | | | Spring | +-------------> Container | | | Configuration +---------+---------+ metadata | (e.g., XML file) | | v Fully configured system
  • 72. Configura)on metadata The configuring metadata can be represented in: • XML • Java annota,on • Java code
  • 73. Configura)on metadata The configura-on metadata ex1.xml is loaded from the CLASSPATH ApplicationContext c = new ClassPathXmlApplicationContext("ex1.xml"); MovieService movieService = c.getBean("SQL", MovieService.class);
  • 74. Dependencies vs. business logic The DI Container allows decoupling the specifica(on of dependencies from the actual program logic
  • 76. POJOs Many Java frameworks require developers to extend classes or implement interfaces provided by the framework itself public class MyServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response); public void doPost(HttpServletRequest request, HttpServletResponse response); public void init(); public void destroy(); }
  • 77. POJOs Spring beans are instead Plain Old Java Objects (POJOs) public class MovieService { private MovieRepository repository; public MovieService(MovieRepository repository) { this.repository = repository; } ... }
  • 78. POJOs Because of this, components in a Spring-based applica8on o9en have no indica(on that they are being used by Spring
  • 79. Movie rental Assume that we want to take advantage of the Spring framework for our movie rental applica.on
  • 80. Movie rental We need to instruct the Spring DI container about the dependency between MovieService and MovieRepository +--------------+ +-----------------+ | | | | | MovieService +----------> MovieRepository | | | | | +--------------+ +--------^--------+ | | | +---------+----------+ | | | SQLMovieRepository | | | +--------------------+
  • 81. Configuring the container We need to configure Spring to tell it what beans it should contain, and how to wire those beans +---------------------------------------------------+ | +--------------+ +-----------------+ | | | | | | | | | MovieService +----------> MovieRepository | | | | | | | | | +--------------+ +--------^--------+ | | | | | | | | | | | +---------+----------+ | | | | | | | SQLMovieRepository | | | | | | | +--------------------+ | +---------------------------------------------------+ Spring DI container
  • 82. Configuring the container For now, we will focus on tradi2onal XML configura-on
  • 83. The <beans> element The root element of the Spring configura4on file is the <beans> element <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ spring-beans.xsd"> <!-- CONFIGURE APPLICATION BEANS HERE --> </beans>
  • 84. The <bean> element Each applica)on bean is associated with a <bean> element in the XML configura)on file <bean id="inMemoryMovieRepository” class="it.polimi.awt.repository.InMemoryMovieRepository"> ... </bean>
  • 85. The <bean> element Every bean has one (or more) id a0ribute, and a class a0ribute <bean id="inMemoryMovieRepository” class="it.polimi.awt.repository.InMemoryMovieRepository"> ... </bean>
  • 86. The <bean> element Namely: • If more iden,fiers are specified, the extra ones are considered as aliases • The class a8ribute could either specify the class of the bean to be constructed or the name of a sta,c factory method
  • 87. How to inject dependencies In Spring, dependencies are injected through: • Construc*on-based injec*on • Se4er-based injec*on • Arguments to a factory method
  • 88. Constructor-based injec1on Constructor-based injec1on is accomplished by the container invoking the bean constructor <bean id="movieService" class="it.polimi.awt.service.MovieService"> <constructor-arg ref="inMemoryMovieRepository"/> </bean>
  • 89. Constructor-based injec1on The <constructor-arg> element allows injec0ng a dependency through a constructor call <bean id="movieService" class="it.polimi.awt.spring.MovieService"> <constructor-arg ref="inMemoryMovieRepository"/> </bean>
  • 90. Se#er-based injec/on Se#er-based injec/on is accomplished by the container calling se3er methods on the bean3 <bean id="lostInTranslation" class="it.polimi.awt.domain.Movie"> <property name="title" value="Lost in Translation"/> <property name="director" value="Sophia Coppola"/> </bean> 3 As we will see, Movies are domain objects, which usually do not need injec5on. Here, we are doing this for the sole sake of presen5ng an example of se>er-based injec5on
  • 91. Se#er-based injec/on The <property> element injects the dependency by calling the property se5er <bean id="lostInTranslation" class="it.polimi.awt.domain.Movie"> <property name="title" value="Lost in Translation"/> <property name="director" value="Sophia Coppola"/> </bean>
  • 92. Constructor vs. se-ers As a rule of thumb, use constructor arguments for mandatory dependencies and se6ers for op*onal dependencies
  • 93. Spring beans vs. JavaBeans Despite the similar name, keep in mind that a DI container is not limited to JavaBeans4 and it can manage virtually any class 4 Recall that JavaBeans are objects with i) only a default constructor; and ii) appropriate ge<ers and se<ers
  • 94.
  • 95.
  • 97. References • Mar%n Fowler, Inversion of Control and Dependency Injec%on pa;ern • SpringSource, Spring Framework Reference • Craig Walls, Spring in Ac%on (3rd Edi%on), Manning Publica%ons