SlideShare a Scribd company logo
Wiring the different layers
Ilio Catallo - info@iliocatallo.it
Outline
• Adding a back-end container
• Wiring the different layers
• References
Adding a back-end container
Web beans
DispatcherServlet ini$alizes its DI container from a single file
named frontcontroller-servlet.xml
+---------------------------------------------------+
| |
| |
| |
| +------------+ +------------+ |
| | +------------+ | +------------+ |
| | | +-------------+ | | +-------------+ |
| | | | | | | | | |
| +-+ | Controllers | +-+ | Special | |
| +-+ | +-+ beans | |
| +-------------+ +-------------+ |
| |
| |
| |
+---------------------------------------------------+
Spring DI Container
Web beans
Controllers and special beans are the Web-specific components
(Web beans) of our applica9on
+---------------------------------------------------+
| |
| |
| |
| +------------+ +------------+ |
| | +------------+ | +------------+ |
| | | +-------------+ | | +-------------+ |
| | | | | | | | | |
| +-+ | Controllers | +-+ | Special | |
| +-+ | +-+ beans | |
| +-------------+ +-------------+ |
| |
| |
| |
+---------------------------------------------------+
Spring DI Container
Service and persistence beans
What about all the other applica,on components (beans) that we
will need for our applica6on?
+---------------------------------------------------+ +--------------+
| | | +--------------+
| | | | +---------------+
| | | | | |
| +------------+ +------------+ | +-+ | Service beans |
| | +------------+ | +------------+ | +-+ |
| | | +-------------+ | | +-------------+ | +---------------+
| | | | | | | | | |
| +-+ | Controllers | +-+ | Special | | +--------------+
| +-+ | +-+ beans | | | +--------------+
| +-------------+ +-------------+ | | | +---------------+
| | | | | |
| | | | | Persistence |
| | +-+ | beans |
+---------------------------------------------------+ +-| |
Spring DI Container +---------------+
Service and persistence beans
Do we have also to declare service and persistence beans into
frontcontroller-servlet.xml?
+---------------------------------------------------+ +--------------+
| | | +--------------+
| | | | +---------------+
| | | | | |
| +------------+ +------------+ | +-+ | Service beans |
| | +------------+ | +------------+ | +-+ |
| | | +-------------+ | | +-------------+ | +---------------+
| | | | | | | | | |
| +-+ | Controllers | +-+ | Special | | +--------------+
| +-+ | +-+ beans | | | +--------------+
| +-------------+ +-------------+ | | | +---------------+
| | | | | |
| | | | | Persistence |
| | +-+ | beans |
+---------------------------------------------------+ +-| |
Spring DI Container +---------------+
Mul$ple configura$on files
Although not strictly required, it is a good idea to organize our
Spring configura8on across mul+ple files
Mul$ple configura$on files
We will add some addi,onal XML configura,on files
• One for the service layer
• One for the persistence layer
• One for data source configura7on
Spli%ng by architectural layers
By doing so, we separate applica&on beans (services and
persistence beans) from Web beans (controllers and special beans)
Spli%ng by architectural layers
That is, the Spring configura3on is broken out by architectural
layers
┌──────┐┌────────────────────────────────┐
│ D ││ │
│ o ││ Presentation layer │
│ m ││ │
│ a │└────────────────────────────────┘
│ i │┌────────────────────────────────┐
│ n ││ │
│ ││ Service layer │
│ m ││ │
│ o │└────────────────────────────────┘
│ d │┌────────────────────────────────┐
│ e ││ │
│ l ││ Persistence layer │
│ ││ │
└──────┘└────────────────────────────────┘
ContextLoaderListener
ContextLoaderListener is a Spring's component capable of
loading addi$onal configura6on files
Event listeners
The Java Servlet specifica1on includes the capability to track
events in a Web applica1on through event listeners
ServletContextListener
We can monitor events related to the Web app lifecycle by means
of a ServletContextListener implementa5on
public interface ServletContextListener {
public void contextInitialized(ServletContextEvent sce);
public void contextDestroyed(ServletContextEvent sce);
}
ServletContextListener
ServletContextListeners are loaded at Web container
startup, before any servlet
public interface ServletContextListener {
public void contextInitialized(ServletContextEvent sce);
public void contextDestroyed(ServletContextEvent sce);
}
ContextLoaderListener
ContextLoaderListener implements the
ServletContextListener interface
public class ContextLoaderListener implements ServletContextListener { ... }
ContextLoaderListener
As with any Web component, we need to declare
ContextLoaderListener in web.xml
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
ContextLoaderListener
The loca)ons of the addi)onal configura)on files are specified as a
context parameter named contextConfigLocation1
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:service-context.xml
classpath:persistence-context.xml
classpath:dataSource-context.xml
</param-value>
</context-param>
1
Note that we are not allowed to specify any other parameter name
ContextLoaderListener
The classpath: prefix is used to inform Spring that the files are
located in the CLASSPATH
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:service-context.xml
classpath:persistence-context.xml
classpath:dataSource-context.xml
</param-value>
</context-param>
One configura,on file per layer
Service beans are declared in service-context.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:service-context.xml <!-- Service beans -->
classpath:persistence-context.xml
classpath:dataSource-context.xml
</param-value>
</context-param>
One configura,on file per layer
Persistence beans are declared in persistence-context.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:service-context.xml
classpath:persistence-context.xml <!-- Persistence beans -->
classpath:dataSource-context.xml
</param-value>
</context-param>
One configura,on file per layer
Finally, dataSource-context.xml configures the data source
we would like to use
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:service-context.xml
classpath:persistence-context.xml
classpath:dataSource-context.xml <!-- data source config -->
</param-value>
</context-param>
How do the addi+onal configura+on files relate to
frontcontroller-servlet.xml?
Back-end container
The ContextLoaderListener bootstraps an addi$onal
container called the back-end container
+--------------------------------------+
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
+--------------------------------------+
Spring DI back-end container
Separa&ng the different areas
Service and persistence beans will live within the back-end
container...
+---------------------------------------------------+
| |
| |
| +--------------+ |
| | +--------------+ +--------------+ |
| | | +--------------- | +--------------+ |
| | | | + | | +---------------+ |
| +-+ | Service beans | | | | | |
| +-+ | | | | Persistence | |
| +---------------| +-+ | beans | |
| +-+ | |
| +---------------+ |
| |
| |
+---------------------------------------------------+
Spring DI back-end container
Separa&ng the different areas
...whereas Web beans will live in the front-end container created
by DispatcherServlet
+---------------------------------------------------+
| |
| |
| |
| +------------+ +------------+ |
| | +------------+ | +------------+ |
| | | +-------------+ | | +-------------+ |
| | | | | | | | | |
| +-+ | Controllers | +-+ | Special | |
| +-+ | +-+ beans | |
| +-------------+ +-------------+ |
| |
| |
| |
+---------------------------------------------------+
Spring DI front-end container
Separa&ng the different areas
We are effec(vely separa&ng the applica(on beans from the Web
beans by crea(ng different containers
+-------------------------+ +-------------------------+
| | | |
| | | |
| <----> |
| | | |
| | | |
+-------------------------+ +-------------------------+
Front-end container Back-end container
The back-end container is unique
Remember: the back-end container is unique
+---------------------------------------------------+
| |
| |
| +--------------+ |
| | +--------------+ +--------------+ |
| | | +--------------- | +--------------+ |
| | | | + | | +---------------+ |
| +-+ | Service beans | | | | | |
| +-+ | | | | Persistence | |
| +---------------| +-+ | beans | |
| +-+ | |
| +---------------+ |
| |
| |
+---------------------------------------------------+
Spring DI back-end container
The back-end container is unique
In other words, ContextLoaderListener loads the different
configura5on files into the same back-end container
+---------------------------------------------------+
| |
| |
| +--------------+ |
| | +--------------+ +--------------+ |
| | | +--------------- | +--------------+ |
| | | | + | | +---------------+ |
| +-+ | Service beans | | | | | |
| +-+ | | | | Persistence | |
| +---------------| +-+ | beans | |
| +-+ | |
| +---------------+ |
| |
| |
+---------------------------------------------------+
Spring DI back-end container
Parent-child rela.onship
The back-end container serves as a parent for the front-end
container
+----------------------+
| |
| Back-end container |
| |
+----------^-----------+
|
| Child of...
|
+----------+-----------+
| |
| Front-end container |
| |
+----------------------+
Parent-child rela.onship
That is why the back-end container is also called the root container
+----------------------+
| |
| Back-end container |
| |
+----------^-----------+
|
| Child of...
|
+----------+-----------+
| |
| Front-end container |
| |
+----------------------+
Parent-child rela.onship
A container can only have a single parent, but it can have mul6ple
children
+--------------+
| |
| Container A |
| |
+------^-------+
|
+--------------+-------------+
| |
+------+-------+ +-------+------+
| | | |
| Container B | | Container C |
| | | |
+--------------+ +------^-------+
|
+--------------+-------------+
| |
+------+-------+ +-------+------+
| | | |
| Container D | | Container E |
| | | |
+--------------+ +--------------+
Parent-child rela.onship
Child containers can access beans defined in the parent container
+--------------+
| |
| Container A |
| |
+------^-------+
|
+--------------+-------------+
| |
+------+-------+ +-------+------+
| | | |
| Container B | | Container C |
| | | |
+--------------+ +------^-------+
|
+--------------+-------------+
| |
+------+-------+ +-------+------+
| | | |
| Container D | | Container E |
| | | |
+--------------+ +--------------+
Parent-child rela.onship
However, parent beans cannot access beans in the child container
+--------------+
| |
| Container A |
| |
+------^-------+
|
+--------------+-------------+
| |
+------+-------+ +-------+------+
| | | |
| Container B | | Container C |
| | | |
+--------------+ +------^-------+
|
+--------------+-------------+
| |
+------+-------+ +-------+------+
| | | |
| Container D | | Container E |
| | | |
+--------------+ +--------------+
Parent-child rela.onship
Hence, shared beans and infrastructure configura2on are
• Declared in the back-end container
• Consumed in the front-end container by Web components
Wiring the different layers
Discovering beans
Once created, we have to declare which beans are going to be part
of the back-end container
+---------------------------------------------------+
| |
| |
| +--------------+ |
| | +--------------+ +--------------+ |
| | | +--------------- | +--------------+ |
| | | | + | | +---------------+ |
| +-+ | Service beans | | | | | |
| +-+ | | | | Persistence | |
| +---------------| +-+ | beans | |
| +-+ | |
| +---------------+ |
| |
| |
+---------------------------------------------------+
Spring DI back-end container
Discovering beans
We can configure Spring to automa&cally discover beans and
declare them in the back-end container
Stereotype annota+ons
To this end, four stereotype annota+ons are available
• @Component
• @Controller
• @Service
• @Repository
Stereotype annota+ons
Both @Controller, @Service and @Repository are
specializa2on of @Component
Service beans
Service beans should be annotated with the @Service annota/on
@Service
public class AccountServiceImpl implements AccountService { ... }
Service beans
Currently, @Service does not provide any addi1onal behavior
over the @Component annota1on
@Service
public class AccountServiceImpl implements AccountService { ... }
Service beans
However, this annota/on might include service layer specific
func/onality in future Spring releases
@Service
public class AccountServiceImpl implements AccountService { ... }
Service beans
Nevertheless, @Service is s,ll useful, as it helps in clearly
demarca-ng the role of the bean
@Service
public class AccountServiceImpl implements AccountService { ... }
Persistence beans
Persistence beans should be annotated with the @Repository
annota/on
@Repository
public class AccountRepositoryImpl implements AccountRepository { ... }
Persistence beans
Like @Service, the @Repository is a specializa/on of the
@Component annota/on
Persistence beans
The @Repository annota)on makes the unchecked excep+ons
thrown by the repository bean eligible for transla)on into Spring's
DataAccessExceptions
Auto-discovering beans
With all the @Service and @Repository annota,ons in place,
we are now ready to auto-discover beans
Auto-discovering beans
The auto-discovery mechanism will find all the service and
persistence beans without having to provide XML configura<on
entries for them
Auto-discovering beans
The <context:component-scan> loads into the container
those beans annotated with any @Component specializa4on
Auto-discovering beans
service-context.xml:
<beans>
<context:component-scan
base-package="it.polimi.awt.springmvc.service"/>
</beans>
Auto-discovering beans
persistence-context.xml:
<beans>
<context:component-scan
base-package="it.polimi.awt.springmvc.repository"/>
</beans>
Auto-discovering beans
No#ce that we already used the auto-discovery mechanism, since
@Controller is also a @Component specializa#on
Auto-discovering beans
frontcontroller-servlet.xml:
<context:component-scan
base-package="it.polimi.awt.springmvc.web"/>
Beans injec*on
With the back-end container in place, we are now ready to inject
instances of the service beans into the controllers
Annota&on-based wiring
The most common way of wiring beans is to use annota%ons
@Controller
@RequestMapping("/account")
public class AccountController {
private AccountService accountService;
@PleaseInjectHere
public AccountController(AccountService accountService) {
accountService = accountService;
}
}
Annota&on-based wiring
It turns out that Spring supports different annota+ons
• The @Autowired annota*on (Spring API)
• The @Inject annota*on (Dependency Injec*on API)
• The @Resource annota*on (Common Annota*ons API)
Annota&on-based wiring
It turns out that Spring supports different annota+ons
• The @Autowired annota)on (Spring API)
• The @Inject annota*on (Dependency Injec*on API)
• The @Resource annota*on (Common Annota*ons API)
@Autowired
When Spring sees something annotated with @Autowired, it will
try to perform wiring by type
@Autowired
@Autowired can be used:
• On fields
• On se*er methods
• On constructor methods
• On methods with arbitrary signature
@Autowired
@Controller
@RequestMapping("/account")
public class AccountController {
private AccountService accountService;
@Autowired
public AccountController(AccountService accountService) {
accountService = accountService;
}
}
Resolving dependencies
If no bean can be wired into the @Autowired-annotated
argument, a NoSuchBeanDefinitionException will be thrown
Resolving dependencies
If more than one bean are suitable for injec1on, @Autowired
cannot determine which one to choose...
Resolving dependencies
...and so a NoSuchBeanDefinitionException will be thrown
Annota&on-based wiring
It turns out that Spring supports different annota2ons:
• The @Autowired annota*on (Spring API)
• The @Inject annota)on (Dependency Injec)on API)
• The @Resource annota*on (Common Annota*ons API)
Dependency Injec+on API
Spring 3 added support for annota1ons coming from the
Dependency Injec+on API (JSR-330)
Dependency Injec+on API
JSR-330 defines some annota1ons contained in the
javax.inject package, such as @Inject and @Named
@Inject
The centerpiece of JSR-330 is the @Inject annota5on. This
annota5on is an almost complete drop-in replacement for Spring’s
@Autowired annota5on
@Inject
@Controller
@RequestMapping("/account")
public class AccountController {
private AccountService accountService;
@Inject
public AccountController(AccountService accountService) {
accountService = accountService;
}
}
Wiring the persistence layer
Of course, we can use @Autowired also to inject persistence
beans into service beans
Wiring the persistence layer
@Service
public class AccountServiceImpl implements AccountService {
private AccountRepository accountRepository;
@Autowired
public AccountServiceImpl(AccountRepository accountRepository) {
this.accountRepository = accountRepository;
}
}
References
References
• SpringSource, Spring Framework Reference
• SpringSource, ContextLoader Java-doc
• M. Deinum, K. Serneels, Pro Spring MVC: with Web flows,
Apress PublicaIons
• Craig Walls, Spring in AcIon (3rd EdiIon),
Manning PublicaIons
• Willie Wheeler, Spring in PracIce, Manning PublicaIons

More Related Content

What's hot

Configure Webserver & SSL secure & redirect in SuSE Linux Enterprise
Configure Webserver & SSL secure & redirect in SuSE Linux EnterpriseConfigure Webserver & SSL secure & redirect in SuSE Linux Enterprise
Configure Webserver & SSL secure & redirect in SuSE Linux Enterprise
Tola LENG
 
New text document (2)
New text document (2)New text document (2)
New text document (2)
Furqaan Aan
 
Dhcp & dhcp relay agent in cent os 5.3
Dhcp & dhcp relay agent in cent os 5.3Dhcp & dhcp relay agent in cent os 5.3
Dhcp & dhcp relay agent in cent os 5.3Sophan Nhean
 
Basic command to configure mikrotik
Basic command to configure mikrotikBasic command to configure mikrotik
Basic command to configure mikrotik
Tola LENG
 
Tola.leng mail server (sq_mail &amp; rcmail)_q5_
Tola.leng mail server (sq_mail &amp; rcmail)_q5_Tola.leng mail server (sq_mail &amp; rcmail)_q5_
Tola.leng mail server (sq_mail &amp; rcmail)_q5_
Tola LENG
 
Configure Proxy and Firewall (Iptables)
Configure Proxy and Firewall (Iptables)Configure Proxy and Firewall (Iptables)
Configure Proxy and Firewall (Iptables)Tola LENG
 
DNS windows server(2008R2) & linux(SLES 11)
DNS windows server(2008R2) & linux(SLES 11)DNS windows server(2008R2) & linux(SLES 11)
DNS windows server(2008R2) & linux(SLES 11)
Tola LENG
 
Xb30330.xb30350 management guide
Xb30330.xb30350 management guideXb30330.xb30350 management guide
Xb30330.xb30350 management guide
Danilo Eduardo Miranda
 
Basic security &amp; info
Basic security &amp; infoBasic security &amp; info
Basic security &amp; info
Tola LENG
 
Oauth2.0
Oauth2.0Oauth2.0
Oauth2.0
iratao
 
Open vpn server_linux
Open vpn server_linuxOpen vpn server_linux
Open vpn server_linux
Tola LENG
 
How to configure IPA-Server & Client-Centos 7
How to configure IPA-Server & Client-Centos 7How to configure IPA-Server & Client-Centos 7
How to configure IPA-Server & Client-Centos 7Tola LENG
 
Marcive Documents: Catching Up and Keeping Up
Marcive Documents: Catching Up and Keeping UpMarcive Documents: Catching Up and Keeping Up
Marcive Documents: Catching Up and Keeping Up
Roy Zimmer
 
دروغ پردازی های کیهان و مافیای کیهانیسم
دروغ پردازی های کیهان و مافیای کیهانیسمدروغ پردازی های کیهان و مافیای کیهانیسم
دروغ پردازی های کیهان و مافیای کیهانیسمIman Rahmatizadeh
 

What's hot (14)

Configure Webserver & SSL secure & redirect in SuSE Linux Enterprise
Configure Webserver & SSL secure & redirect in SuSE Linux EnterpriseConfigure Webserver & SSL secure & redirect in SuSE Linux Enterprise
Configure Webserver & SSL secure & redirect in SuSE Linux Enterprise
 
New text document (2)
New text document (2)New text document (2)
New text document (2)
 
Dhcp & dhcp relay agent in cent os 5.3
Dhcp & dhcp relay agent in cent os 5.3Dhcp & dhcp relay agent in cent os 5.3
Dhcp & dhcp relay agent in cent os 5.3
 
Basic command to configure mikrotik
Basic command to configure mikrotikBasic command to configure mikrotik
Basic command to configure mikrotik
 
Tola.leng mail server (sq_mail &amp; rcmail)_q5_
Tola.leng mail server (sq_mail &amp; rcmail)_q5_Tola.leng mail server (sq_mail &amp; rcmail)_q5_
Tola.leng mail server (sq_mail &amp; rcmail)_q5_
 
Configure Proxy and Firewall (Iptables)
Configure Proxy and Firewall (Iptables)Configure Proxy and Firewall (Iptables)
Configure Proxy and Firewall (Iptables)
 
DNS windows server(2008R2) & linux(SLES 11)
DNS windows server(2008R2) & linux(SLES 11)DNS windows server(2008R2) & linux(SLES 11)
DNS windows server(2008R2) & linux(SLES 11)
 
Xb30330.xb30350 management guide
Xb30330.xb30350 management guideXb30330.xb30350 management guide
Xb30330.xb30350 management guide
 
Basic security &amp; info
Basic security &amp; infoBasic security &amp; info
Basic security &amp; info
 
Oauth2.0
Oauth2.0Oauth2.0
Oauth2.0
 
Open vpn server_linux
Open vpn server_linuxOpen vpn server_linux
Open vpn server_linux
 
How to configure IPA-Server & Client-Centos 7
How to configure IPA-Server & Client-Centos 7How to configure IPA-Server & Client-Centos 7
How to configure IPA-Server & Client-Centos 7
 
Marcive Documents: Catching Up and Keeping Up
Marcive Documents: Catching Up and Keeping UpMarcive Documents: Catching Up and Keeping Up
Marcive Documents: Catching Up and Keeping Up
 
دروغ پردازی های کیهان و مافیای کیهانیسم
دروغ پردازی های کیهان و مافیای کیهانیسمدروغ پردازی های کیهان و مافیای کیهانیسم
دروغ پردازی های کیهان و مافیای کیهانیسم
 

Similar to Spring MVC - Wiring the different layers

1Hippeus - zerocopy messaging по законам Спарты, Леонид Юрьев (ПЕТЕР-СЕРВИС)
1Hippeus -  zerocopy messaging по законам Спарты, Леонид Юрьев (ПЕТЕР-СЕРВИС)1Hippeus -  zerocopy messaging по законам Спарты, Леонид Юрьев (ПЕТЕР-СЕРВИС)
1Hippeus - zerocopy messaging по законам Спарты, Леонид Юрьев (ПЕТЕР-СЕРВИС)
Ontico
 
Highload++2014: 1Hippeus - zerocopy messaging in the spirit of Sparta!
Highload++2014: 1Hippeus - zerocopy messaging in the spirit of Sparta!Highload++2014: 1Hippeus - zerocopy messaging in the spirit of Sparta!
Highload++2014: 1Hippeus - zerocopy messaging in the spirit of Sparta!
Leonid Yuriev
 
Homework Assignment 3 Chapter 3 St. Clair & Visick, Putting you.docx
Homework Assignment 3 Chapter 3 St. Clair & Visick, Putting you.docxHomework Assignment 3 Chapter 3 St. Clair & Visick, Putting you.docx
Homework Assignment 3 Chapter 3 St. Clair & Visick, Putting you.docx
fideladallimore
 
Cloudy with a Chance of Fireballs: Provisioning and Certificate Management in...
Cloudy with a Chance of Fireballs: Provisioning and Certificate Management in...Cloudy with a Chance of Fireballs: Provisioning and Certificate Management in...
Cloudy with a Chance of Fireballs: Provisioning and Certificate Management in...Puppet
 
Modern query optimisation features in MySQL 8.
Modern query optimisation features in MySQL 8.Modern query optimisation features in MySQL 8.
Modern query optimisation features in MySQL 8.
Mydbops
 
Chemlb27
Chemlb27Chemlb27
Chemlb27
Carlos Perez
 
Lightning Network (BOLT) Normal Operation Sequence
Lightning Network (BOLT) Normal Operation SequenceLightning Network (BOLT) Normal Operation Sequence
Lightning Network (BOLT) Normal Operation Sequence
Hiroki Gondo
 
M|18 User Defined Function
M|18 User Defined FunctionM|18 User Defined Function
M|18 User Defined Function
MariaDB plc
 
Kickstat File_Draft_ESXI5.1_Template
Kickstat File_Draft_ESXI5.1_TemplateKickstat File_Draft_ESXI5.1_Template
Kickstat File_Draft_ESXI5.1_TemplateLuca Viscomi
 
忙しい人のためのSphinx 入門 demo
忙しい人のためのSphinx 入門 demo忙しい人のためのSphinx 入門 demo
忙しい人のためのSphinx 入門 demo
Fumihito Yokoyama
 
Perf Tuning Short
Perf Tuning ShortPerf Tuning Short
Perf Tuning Short
Ligaya Turmelle
 
Using Optimizer Hints to Improve MySQL Query Performance
Using Optimizer Hints to Improve MySQL Query PerformanceUsing Optimizer Hints to Improve MySQL Query Performance
Using Optimizer Hints to Improve MySQL Query Performance
oysteing
 
Docker at RelateIQ
Docker at RelateIQDocker at RelateIQ
Docker at RelateIQDocker, Inc.
 
Windowing Functions - Little Rock Tech Fest 2019
Windowing Functions - Little Rock Tech Fest 2019Windowing Functions - Little Rock Tech Fest 2019
Windowing Functions - Little Rock Tech Fest 2019
Dave Stokes
 
Windowing Functions - Little Rock Tech fest 2019
Windowing Functions - Little Rock Tech fest 2019Windowing Functions - Little Rock Tech fest 2019
Windowing Functions - Little Rock Tech fest 2019
Dave Stokes
 
Development Workflows on AWS
Development Workflows on AWSDevelopment Workflows on AWS
Development Workflows on AWS
Amazon Web Services
 
The two little bugs that almost brought down Booking.com
The two little bugs that almost brought down Booking.comThe two little bugs that almost brought down Booking.com
The two little bugs that almost brought down Booking.com
Jean-François Gagné
 

Similar to Spring MVC - Wiring the different layers (20)

1Hippeus - zerocopy messaging по законам Спарты, Леонид Юрьев (ПЕТЕР-СЕРВИС)
1Hippeus -  zerocopy messaging по законам Спарты, Леонид Юрьев (ПЕТЕР-СЕРВИС)1Hippeus -  zerocopy messaging по законам Спарты, Леонид Юрьев (ПЕТЕР-СЕРВИС)
1Hippeus - zerocopy messaging по законам Спарты, Леонид Юрьев (ПЕТЕР-СЕРВИС)
 
Highload++2014: 1Hippeus - zerocopy messaging in the spirit of Sparta!
Highload++2014: 1Hippeus - zerocopy messaging in the spirit of Sparta!Highload++2014: 1Hippeus - zerocopy messaging in the spirit of Sparta!
Highload++2014: 1Hippeus - zerocopy messaging in the spirit of Sparta!
 
Homework Assignment 3 Chapter 3 St. Clair & Visick, Putting you.docx
Homework Assignment 3 Chapter 3 St. Clair & Visick, Putting you.docxHomework Assignment 3 Chapter 3 St. Clair & Visick, Putting you.docx
Homework Assignment 3 Chapter 3 St. Clair & Visick, Putting you.docx
 
Cloudy with a Chance of Fireballs: Provisioning and Certificate Management in...
Cloudy with a Chance of Fireballs: Provisioning and Certificate Management in...Cloudy with a Chance of Fireballs: Provisioning and Certificate Management in...
Cloudy with a Chance of Fireballs: Provisioning and Certificate Management in...
 
Modern query optimisation features in MySQL 8.
Modern query optimisation features in MySQL 8.Modern query optimisation features in MySQL 8.
Modern query optimisation features in MySQL 8.
 
Chemlb27
Chemlb27Chemlb27
Chemlb27
 
Rfc 3412
Rfc 3412Rfc 3412
Rfc 3412
 
Lightning Network (BOLT) Normal Operation Sequence
Lightning Network (BOLT) Normal Operation SequenceLightning Network (BOLT) Normal Operation Sequence
Lightning Network (BOLT) Normal Operation Sequence
 
Mysql56 replication
Mysql56 replicationMysql56 replication
Mysql56 replication
 
M|18 User Defined Function
M|18 User Defined FunctionM|18 User Defined Function
M|18 User Defined Function
 
Kickstat File_Draft_ESXI5.1_Template
Kickstat File_Draft_ESXI5.1_TemplateKickstat File_Draft_ESXI5.1_Template
Kickstat File_Draft_ESXI5.1_Template
 
忙しい人のためのSphinx 入門 demo
忙しい人のためのSphinx 入門 demo忙しい人のためのSphinx 入門 demo
忙しい人のためのSphinx 入門 demo
 
Perf Tuning Short
Perf Tuning ShortPerf Tuning Short
Perf Tuning Short
 
Using Optimizer Hints to Improve MySQL Query Performance
Using Optimizer Hints to Improve MySQL Query PerformanceUsing Optimizer Hints to Improve MySQL Query Performance
Using Optimizer Hints to Improve MySQL Query Performance
 
Dockercon 14
Dockercon 14Dockercon 14
Dockercon 14
 
Docker at RelateIQ
Docker at RelateIQDocker at RelateIQ
Docker at RelateIQ
 
Windowing Functions - Little Rock Tech Fest 2019
Windowing Functions - Little Rock Tech Fest 2019Windowing Functions - Little Rock Tech Fest 2019
Windowing Functions - Little Rock Tech Fest 2019
 
Windowing Functions - Little Rock Tech fest 2019
Windowing Functions - Little Rock Tech fest 2019Windowing Functions - Little Rock Tech fest 2019
Windowing Functions - Little Rock Tech fest 2019
 
Development Workflows on AWS
Development Workflows on AWSDevelopment Workflows on AWS
Development Workflows on AWS
 
The two little bugs that almost brought down Booking.com
The two little bugs that almost brought down Booking.comThe two little bugs that almost brought down Booking.com
The two little bugs that almost brought down Booking.com
 

More from Ilio Catallo

C++ Standard Template Library
C++ Standard Template LibraryC++ Standard Template Library
C++ Standard Template Library
Ilio 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
 
Arrays in C++
Arrays in C++Arrays in C++
Arrays in C++
Ilio Catallo
 
Pointers & References in C++
Pointers & References in C++Pointers & References in C++
Pointers & References in C++
Ilio Catallo
 
Java and Java platforms
Java and Java platformsJava and Java platforms
Java and Java platforms
Ilio Catallo
 
Web application architecture
Web application architectureWeb application architecture
Web application architecture
Ilio Catallo
 
Introduction To Spring
Introduction To SpringIntroduction To Spring
Introduction To Spring
Ilio Catallo
 
Gestione della memoria in C++
Gestione della memoria in C++Gestione della memoria in C++
Gestione della memoria in C++
Ilio Catallo
 
Array in C++
Array in C++Array in C++
Array in C++
Ilio Catallo
 
Puntatori e Riferimenti
Puntatori e RiferimentiPuntatori e Riferimenti
Puntatori e Riferimenti
Ilio Catallo
 
Java Persistence API
Java Persistence APIJava Persistence API
Java Persistence API
Ilio Catallo
 
JSP Standard Tag Library
JSP Standard Tag LibraryJSP Standard Tag Library
JSP Standard Tag Library
Ilio Catallo
 
Internationalization in Jakarta Struts 1.3
Internationalization in Jakarta Struts 1.3Internationalization in Jakarta Struts 1.3
Internationalization in Jakarta Struts 1.3
Ilio Catallo
 
Validation in Jakarta Struts 1.3
Validation in Jakarta Struts 1.3Validation in Jakarta Struts 1.3
Validation in Jakarta Struts 1.3
Ilio Catallo
 
Introduction to Struts 1.3
Introduction to Struts 1.3Introduction to Struts 1.3
Introduction to Struts 1.3
Ilio Catallo
 
Community Detection
Community DetectionCommunity Detection
Community DetectionIlio 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++
 
Java and Java platforms
Java and Java platformsJava and Java platforms
Java and Java platforms
 
Web application architecture
Web application architectureWeb application architecture
Web application architecture
 
Introduction To Spring
Introduction To SpringIntroduction To Spring
Introduction To Spring
 
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
 
Introduction to Struts 1.3
Introduction to Struts 1.3Introduction to Struts 1.3
Introduction to Struts 1.3
 
Community Detection
Community DetectionCommunity Detection
Community Detection
 

Recently uploaded

Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
91mobiles
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
SOFTTECHHUB
 
Enhancing Performance with Globus and the Science DMZ
Enhancing Performance with Globus and the Science DMZEnhancing Performance with Globus and the Science DMZ
Enhancing Performance with Globus and the Science DMZ
Globus
 
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex ProofszkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
Alex Pruden
 
Assure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyesAssure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyes
ThousandEyes
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Albert Hoitingh
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
Aftab Hussain
 
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
James Anderson
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
Ralf Eggert
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
Jemma Hussein Allen
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
BookNet Canada
 
Free Complete Python - A step towards Data Science
Free Complete Python - A step towards Data ScienceFree Complete Python - A step towards Data Science
Free Complete Python - A step towards Data Science
RinaMondal9
 
RESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for studentsRESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for students
KAMESHS29
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Paige Cruz
 

Recently uploaded (20)

Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
 
Enhancing Performance with Globus and the Science DMZ
Enhancing Performance with Globus and the Science DMZEnhancing Performance with Globus and the Science DMZ
Enhancing Performance with Globus and the Science DMZ
 
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex ProofszkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
 
Assure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyesAssure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyes
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
 
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
Free Complete Python - A step towards Data Science
Free Complete Python - A step towards Data ScienceFree Complete Python - A step towards Data Science
Free Complete Python - A step towards Data Science
 
RESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for studentsRESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for students
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
 

Spring MVC - Wiring the different layers

  • 1. Wiring the different layers Ilio Catallo - info@iliocatallo.it
  • 2. Outline • Adding a back-end container • Wiring the different layers • References
  • 3. Adding a back-end container
  • 4. Web beans DispatcherServlet ini$alizes its DI container from a single file named frontcontroller-servlet.xml +---------------------------------------------------+ | | | | | | | +------------+ +------------+ | | | +------------+ | +------------+ | | | | +-------------+ | | +-------------+ | | | | | | | | | | | | +-+ | Controllers | +-+ | Special | | | +-+ | +-+ beans | | | +-------------+ +-------------+ | | | | | | | +---------------------------------------------------+ Spring DI Container
  • 5. Web beans Controllers and special beans are the Web-specific components (Web beans) of our applica9on +---------------------------------------------------+ | | | | | | | +------------+ +------------+ | | | +------------+ | +------------+ | | | | +-------------+ | | +-------------+ | | | | | | | | | | | | +-+ | Controllers | +-+ | Special | | | +-+ | +-+ beans | | | +-------------+ +-------------+ | | | | | | | +---------------------------------------------------+ Spring DI Container
  • 6. Service and persistence beans What about all the other applica,on components (beans) that we will need for our applica6on? +---------------------------------------------------+ +--------------+ | | | +--------------+ | | | | +---------------+ | | | | | | | +------------+ +------------+ | +-+ | Service beans | | | +------------+ | +------------+ | +-+ | | | | +-------------+ | | +-------------+ | +---------------+ | | | | | | | | | | | +-+ | Controllers | +-+ | Special | | +--------------+ | +-+ | +-+ beans | | | +--------------+ | +-------------+ +-------------+ | | | +---------------+ | | | | | | | | | | | Persistence | | | +-+ | beans | +---------------------------------------------------+ +-| | Spring DI Container +---------------+
  • 7. Service and persistence beans Do we have also to declare service and persistence beans into frontcontroller-servlet.xml? +---------------------------------------------------+ +--------------+ | | | +--------------+ | | | | +---------------+ | | | | | | | +------------+ +------------+ | +-+ | Service beans | | | +------------+ | +------------+ | +-+ | | | | +-------------+ | | +-------------+ | +---------------+ | | | | | | | | | | | +-+ | Controllers | +-+ | Special | | +--------------+ | +-+ | +-+ beans | | | +--------------+ | +-------------+ +-------------+ | | | +---------------+ | | | | | | | | | | | Persistence | | | +-+ | beans | +---------------------------------------------------+ +-| | Spring DI Container +---------------+
  • 8. Mul$ple configura$on files Although not strictly required, it is a good idea to organize our Spring configura8on across mul+ple files
  • 9. Mul$ple configura$on files We will add some addi,onal XML configura,on files • One for the service layer • One for the persistence layer • One for data source configura7on
  • 10. Spli%ng by architectural layers By doing so, we separate applica&on beans (services and persistence beans) from Web beans (controllers and special beans)
  • 11. Spli%ng by architectural layers That is, the Spring configura3on is broken out by architectural layers ┌──────┐┌────────────────────────────────┐ │ D ││ │ │ o ││ Presentation layer │ │ m ││ │ │ a │└────────────────────────────────┘ │ i │┌────────────────────────────────┐ │ n ││ │ │ ││ Service layer │ │ m ││ │ │ o │└────────────────────────────────┘ │ d │┌────────────────────────────────┐ │ e ││ │ │ l ││ Persistence layer │ │ ││ │ └──────┘└────────────────────────────────┘
  • 12. ContextLoaderListener ContextLoaderListener is a Spring's component capable of loading addi$onal configura6on files
  • 13. Event listeners The Java Servlet specifica1on includes the capability to track events in a Web applica1on through event listeners
  • 14. ServletContextListener We can monitor events related to the Web app lifecycle by means of a ServletContextListener implementa5on public interface ServletContextListener { public void contextInitialized(ServletContextEvent sce); public void contextDestroyed(ServletContextEvent sce); }
  • 15. ServletContextListener ServletContextListeners are loaded at Web container startup, before any servlet public interface ServletContextListener { public void contextInitialized(ServletContextEvent sce); public void contextDestroyed(ServletContextEvent sce); }
  • 16. ContextLoaderListener ContextLoaderListener implements the ServletContextListener interface public class ContextLoaderListener implements ServletContextListener { ... }
  • 17. ContextLoaderListener As with any Web component, we need to declare ContextLoaderListener in web.xml <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener>
  • 18. ContextLoaderListener The loca)ons of the addi)onal configura)on files are specified as a context parameter named contextConfigLocation1 <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:service-context.xml classpath:persistence-context.xml classpath:dataSource-context.xml </param-value> </context-param> 1 Note that we are not allowed to specify any other parameter name
  • 19. ContextLoaderListener The classpath: prefix is used to inform Spring that the files are located in the CLASSPATH <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:service-context.xml classpath:persistence-context.xml classpath:dataSource-context.xml </param-value> </context-param>
  • 20. One configura,on file per layer Service beans are declared in service-context.xml <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:service-context.xml <!-- Service beans --> classpath:persistence-context.xml classpath:dataSource-context.xml </param-value> </context-param>
  • 21. One configura,on file per layer Persistence beans are declared in persistence-context.xml <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:service-context.xml classpath:persistence-context.xml <!-- Persistence beans --> classpath:dataSource-context.xml </param-value> </context-param>
  • 22. One configura,on file per layer Finally, dataSource-context.xml configures the data source we would like to use <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:service-context.xml classpath:persistence-context.xml classpath:dataSource-context.xml <!-- data source config --> </param-value> </context-param>
  • 23. How do the addi+onal configura+on files relate to frontcontroller-servlet.xml?
  • 24. Back-end container The ContextLoaderListener bootstraps an addi$onal container called the back-end container +--------------------------------------+ | | | | | | | | | | | | | | | | | | | | | | +--------------------------------------+ Spring DI back-end container
  • 25. Separa&ng the different areas Service and persistence beans will live within the back-end container... +---------------------------------------------------+ | | | | | +--------------+ | | | +--------------+ +--------------+ | | | | +--------------- | +--------------+ | | | | | + | | +---------------+ | | +-+ | Service beans | | | | | | | +-+ | | | | Persistence | | | +---------------| +-+ | beans | | | +-+ | | | +---------------+ | | | | | +---------------------------------------------------+ Spring DI back-end container
  • 26. Separa&ng the different areas ...whereas Web beans will live in the front-end container created by DispatcherServlet +---------------------------------------------------+ | | | | | | | +------------+ +------------+ | | | +------------+ | +------------+ | | | | +-------------+ | | +-------------+ | | | | | | | | | | | | +-+ | Controllers | +-+ | Special | | | +-+ | +-+ beans | | | +-------------+ +-------------+ | | | | | | | +---------------------------------------------------+ Spring DI front-end container
  • 27. Separa&ng the different areas We are effec(vely separa&ng the applica(on beans from the Web beans by crea(ng different containers +-------------------------+ +-------------------------+ | | | | | | | | | <----> | | | | | | | | | +-------------------------+ +-------------------------+ Front-end container Back-end container
  • 28. The back-end container is unique Remember: the back-end container is unique +---------------------------------------------------+ | | | | | +--------------+ | | | +--------------+ +--------------+ | | | | +--------------- | +--------------+ | | | | | + | | +---------------+ | | +-+ | Service beans | | | | | | | +-+ | | | | Persistence | | | +---------------| +-+ | beans | | | +-+ | | | +---------------+ | | | | | +---------------------------------------------------+ Spring DI back-end container
  • 29. The back-end container is unique In other words, ContextLoaderListener loads the different configura5on files into the same back-end container +---------------------------------------------------+ | | | | | +--------------+ | | | +--------------+ +--------------+ | | | | +--------------- | +--------------+ | | | | | + | | +---------------+ | | +-+ | Service beans | | | | | | | +-+ | | | | Persistence | | | +---------------| +-+ | beans | | | +-+ | | | +---------------+ | | | | | +---------------------------------------------------+ Spring DI back-end container
  • 30. Parent-child rela.onship The back-end container serves as a parent for the front-end container +----------------------+ | | | Back-end container | | | +----------^-----------+ | | Child of... | +----------+-----------+ | | | Front-end container | | | +----------------------+
  • 31. Parent-child rela.onship That is why the back-end container is also called the root container +----------------------+ | | | Back-end container | | | +----------^-----------+ | | Child of... | +----------+-----------+ | | | Front-end container | | | +----------------------+
  • 32. Parent-child rela.onship A container can only have a single parent, but it can have mul6ple children +--------------+ | | | Container A | | | +------^-------+ | +--------------+-------------+ | | +------+-------+ +-------+------+ | | | | | Container B | | Container C | | | | | +--------------+ +------^-------+ | +--------------+-------------+ | | +------+-------+ +-------+------+ | | | | | Container D | | Container E | | | | | +--------------+ +--------------+
  • 33. Parent-child rela.onship Child containers can access beans defined in the parent container +--------------+ | | | Container A | | | +------^-------+ | +--------------+-------------+ | | +------+-------+ +-------+------+ | | | | | Container B | | Container C | | | | | +--------------+ +------^-------+ | +--------------+-------------+ | | +------+-------+ +-------+------+ | | | | | Container D | | Container E | | | | | +--------------+ +--------------+
  • 34. Parent-child rela.onship However, parent beans cannot access beans in the child container +--------------+ | | | Container A | | | +------^-------+ | +--------------+-------------+ | | +------+-------+ +-------+------+ | | | | | Container B | | Container C | | | | | +--------------+ +------^-------+ | +--------------+-------------+ | | +------+-------+ +-------+------+ | | | | | Container D | | Container E | | | | | +--------------+ +--------------+
  • 35. Parent-child rela.onship Hence, shared beans and infrastructure configura2on are • Declared in the back-end container • Consumed in the front-end container by Web components
  • 37. Discovering beans Once created, we have to declare which beans are going to be part of the back-end container +---------------------------------------------------+ | | | | | +--------------+ | | | +--------------+ +--------------+ | | | | +--------------- | +--------------+ | | | | | + | | +---------------+ | | +-+ | Service beans | | | | | | | +-+ | | | | Persistence | | | +---------------| +-+ | beans | | | +-+ | | | +---------------+ | | | | | +---------------------------------------------------+ Spring DI back-end container
  • 38. Discovering beans We can configure Spring to automa&cally discover beans and declare them in the back-end container
  • 39. Stereotype annota+ons To this end, four stereotype annota+ons are available • @Component • @Controller • @Service • @Repository
  • 40. Stereotype annota+ons Both @Controller, @Service and @Repository are specializa2on of @Component
  • 41. Service beans Service beans should be annotated with the @Service annota/on @Service public class AccountServiceImpl implements AccountService { ... }
  • 42. Service beans Currently, @Service does not provide any addi1onal behavior over the @Component annota1on @Service public class AccountServiceImpl implements AccountService { ... }
  • 43. Service beans However, this annota/on might include service layer specific func/onality in future Spring releases @Service public class AccountServiceImpl implements AccountService { ... }
  • 44. Service beans Nevertheless, @Service is s,ll useful, as it helps in clearly demarca-ng the role of the bean @Service public class AccountServiceImpl implements AccountService { ... }
  • 45. Persistence beans Persistence beans should be annotated with the @Repository annota/on @Repository public class AccountRepositoryImpl implements AccountRepository { ... }
  • 46. Persistence beans Like @Service, the @Repository is a specializa/on of the @Component annota/on
  • 47. Persistence beans The @Repository annota)on makes the unchecked excep+ons thrown by the repository bean eligible for transla)on into Spring's DataAccessExceptions
  • 48. Auto-discovering beans With all the @Service and @Repository annota,ons in place, we are now ready to auto-discover beans
  • 49. Auto-discovering beans The auto-discovery mechanism will find all the service and persistence beans without having to provide XML configura<on entries for them
  • 50. Auto-discovering beans The <context:component-scan> loads into the container those beans annotated with any @Component specializa4on
  • 53. Auto-discovering beans No#ce that we already used the auto-discovery mechanism, since @Controller is also a @Component specializa#on
  • 55. Beans injec*on With the back-end container in place, we are now ready to inject instances of the service beans into the controllers
  • 56. Annota&on-based wiring The most common way of wiring beans is to use annota%ons @Controller @RequestMapping("/account") public class AccountController { private AccountService accountService; @PleaseInjectHere public AccountController(AccountService accountService) { accountService = accountService; } }
  • 57. Annota&on-based wiring It turns out that Spring supports different annota+ons • The @Autowired annota*on (Spring API) • The @Inject annota*on (Dependency Injec*on API) • The @Resource annota*on (Common Annota*ons API)
  • 58. Annota&on-based wiring It turns out that Spring supports different annota+ons • The @Autowired annota)on (Spring API) • The @Inject annota*on (Dependency Injec*on API) • The @Resource annota*on (Common Annota*ons API)
  • 59. @Autowired When Spring sees something annotated with @Autowired, it will try to perform wiring by type
  • 60. @Autowired @Autowired can be used: • On fields • On se*er methods • On constructor methods • On methods with arbitrary signature
  • 61. @Autowired @Controller @RequestMapping("/account") public class AccountController { private AccountService accountService; @Autowired public AccountController(AccountService accountService) { accountService = accountService; } }
  • 62. Resolving dependencies If no bean can be wired into the @Autowired-annotated argument, a NoSuchBeanDefinitionException will be thrown
  • 63. Resolving dependencies If more than one bean are suitable for injec1on, @Autowired cannot determine which one to choose...
  • 64. Resolving dependencies ...and so a NoSuchBeanDefinitionException will be thrown
  • 65. Annota&on-based wiring It turns out that Spring supports different annota2ons: • The @Autowired annota*on (Spring API) • The @Inject annota)on (Dependency Injec)on API) • The @Resource annota*on (Common Annota*ons API)
  • 66. Dependency Injec+on API Spring 3 added support for annota1ons coming from the Dependency Injec+on API (JSR-330)
  • 67. Dependency Injec+on API JSR-330 defines some annota1ons contained in the javax.inject package, such as @Inject and @Named
  • 68. @Inject The centerpiece of JSR-330 is the @Inject annota5on. This annota5on is an almost complete drop-in replacement for Spring’s @Autowired annota5on
  • 69. @Inject @Controller @RequestMapping("/account") public class AccountController { private AccountService accountService; @Inject public AccountController(AccountService accountService) { accountService = accountService; } }
  • 70. Wiring the persistence layer Of course, we can use @Autowired also to inject persistence beans into service beans
  • 71. Wiring the persistence layer @Service public class AccountServiceImpl implements AccountService { private AccountRepository accountRepository; @Autowired public AccountServiceImpl(AccountRepository accountRepository) { this.accountRepository = accountRepository; } }
  • 73. References • SpringSource, Spring Framework Reference • SpringSource, ContextLoader Java-doc • M. Deinum, K. Serneels, Pro Spring MVC: with Web flows, Apress PublicaIons • Craig Walls, Spring in AcIon (3rd EdiIon), Manning PublicaIons • Willie Wheeler, Spring in PracIce, Manning PublicaIons