Spring Framework
Spring Framework
Spring Framework
Reda Bendraou
Spring Framework
[email protected]
http://pagesperso-systeme.lip6.fr/Reda.Bendraou/
This course is inspired by the readings/sources listed in the last slide Introduction
2009-2010 2009-2010
2009-2010 2009-2010
1
3/25/2015
Integration with other frameworks (Struts, Hibernate, JSF, Management of dependencies between components
etc.)
No specific infrastructure requirements
Just a JVM
2009-2010 2009-2010
2009-2010 2009-2010
2
3/25/2015
- Spring AOP
Framework Spring
Mainly .Jars!
2009-2010 2009-2010
2009-2010 2009-2010
3
3/25/2015
2009-2010 2009-2010
4
3/25/2015
IBarImplN
Spring :
IBarImpl1 • Manages complex inter dependencies between components
IBarImpl2 SPRING
<<creates>> • Manages component life cycles: use of the singleton or
prototype instantiation patterns
<<injects>>
2009-2010 2009-2010
2009-2010 2009-2010
5
3/25/2015
Other beans required by a given bean to execute <bean id="exampleBean" factory-bean="myFactoryBean" factory-method="createInstance"/>
2009-2010 2009-2010
2009-2010 2009-2010
6
3/25/2015
lazy : lazy loading / instant loading Injection using Setters (i.e. setXXX(); )
parent : the ‘parent’ of the bean from whom we can reuse injections
Injection using the class’s constructor
name : alias
2009-2010 2009-2010
Dependency Injection: the Setters way Dependency Injection: the Setters way
Injection of values ‘constants’ :
a reference to
<bean id="exampleBean" class="examples.ExampleBean"> another bean
2009-2010 2009-2010
7
3/25/2015
Dependency Injection: the Constructor way Dependency Injection: the Constructor way
• The Java Class Bean Definition
2009-2010 2009-2010
2009-2010 2009-2010
8
3/25/2015
<bean id="exampleInitBean" class="examples.ExampleBean" destroy-method="cleanup"/> A list : list (with multiple values or references to other beans)
//Java Class
A set : set (with multiple values or references to other beans)
public class ExampleBean {
public void cleanup() { // close connection} }
A map : map (with entries like : value/reference to another bean)
• b) By implementing the « DisposableBean » interface
2009-2010 2009-2010
9
3/25/2015
Bean Factory & Application Context Bean Factory & Application Context
Instantiation of Spring lightweight container in the context of a
Instantiation of Spring lightweight container :
WEB Application :
2009-2010 2009-2010
Example of a Spring project: Packaging Example of a Web Project using Spring : Packaging
Spring Project Web Project
using Spring
Your POJOs
Your POJOs
Your Servlets
2009-2010 2009-2010
10
3/25/2015
AOP
Introduction to AOP
AspectJ AOP
2009-2010 2009-2010
AOP Advice
Code to be executed before/after/around a Joinpoint expressed by a Pointcut
2009-2010 2009-2010
11
3/25/2015
At loading time : modification of classes when they are loaded in the JVM
Proxy
Advisor
2009-2010 2009-2010
Weaving of Aspects: the Declarative Way Weaving of Aspects: the Declarative Way
<beans> public class EmailLogger {
<aop:config>
public void log(String zipcode, String address) {
<aop:aspect id="emailLoggerBean“ ref="emailerLogger">
<aop:before
System.out.println(“hello”);
pointcut="execution(* send(String, *, *, String)) }
and args(address, *, *, zipcode)" }
method="log" arg-names=“zipcode, address"/>
</aop:aspect>
</aop:config>
<bean id="emailerLogger“ class="example.EmailLogger"/>
</beans>
Explanation: The log method is invocated before all the calls to methods called
“send” and having 4 parameters with the first parameter and the forth one are
Strings. The first and the forth parameters are forwarded to the log method
2009-2010 2009-2010
12
3/25/2015
Weaving of Aspects: the Declarative Way Weaving of Aspects: the Declarative Way
<beans> public class EmailLogger {
<aop:config> public void log(ProceedingJoinPoint pjp, String zipcode, String address) throws
<aop:aspect id="emailLoggerBean“ ref="emailerLogger"> Throwable {
<aop:around
pointcut="execution(* send(String, *, *, String)) if(Calendar.getInstance().get(Calendar.HOUR_OF_DAY)>12)
and args(address, *, *, zipcode)" {
method="log" arg-names="pjp, zipcode, address"/> //Continue the normal execution
</aop:aspect> pjp.proceed();
</aop:config> }
<bean id="emailerLogger“ class="example.EmailLogger"/>
</beans> else
{
Explanation: the log method is called in place of all the calls to methods called // do nothing
‘send’ and having 4 parameters with the first parameter and the forth one are }
Strings. The first and the forth parameters are forwarded to the log method with
an additional parameter, the pjp (ProceedingJoinPoint) which represents the }
execution context }
.
2009-2010 2009-2010
2009-2010 2009-2010
13
3/25/2015
2009-2010 2009-2010
Conclusions Readings/Sources
Spring AOP is a simple yet very powerful mean to deal with cross-cutting concerns - The Spring specification : https://www.springsource.org/
AspectJ integration within Spring brings all the advantages of AOP in the same - Book: Spring par la pratique: J. Dubois, et al., Eyrolles, 2007
environment
- Book: Spring in Action, Craig Walls, 2008, Manning publications
The choice of using AOP or not can be taken at any time: not a strategic decision
- H. Ouahidi Courses, UniConsulting (slides in French not provided online)
2009-2010 2009-2010
14