0% found this document useful (0 votes)
77 views

Spring Framework

The document discusses issues with dependency management in Java/Java EE development and introduces the Spring Framework as a solution. It notes that standardization processes take too long and "home grown" solutions are developed for problems. Spring addresses this by providing a lightweight container and aspects like AOP and integration with other frameworks. The course will focus on IoC (Inversion of Control) and Dependency Injection with Spring. It compares the naive approach, service locator approach, and IoC approach for managing dependencies, noting that IoC provides the cleanest separation of concerns.

Uploaded by

jawad jo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
77 views

Spring Framework

The document discusses issues with dependency management in Java/Java EE development and introduces the Spring Framework as a solution. It notes that standardization processes take too long and "home grown" solutions are developed for problems. Spring addresses this by providing a lightweight container and aspects like AOP and integration with other frameworks. The course will focus on IoC (Inversion of Control) and Dependency Injection with Spring. It compares the naive approach, service locator approach, and IoC approach for managing dependencies, noting that IoC provides the cleanest separation of concerns.

Uploaded by

jawad jo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

3/25/2015

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

Introduction JEE Development Issues


Java/Java EE Development requirements: Standardization Processes take too long!

Separation of concerns Before:


In case of problems: development of “Home Made” solutions
Development productivity
Currently:
Platform independence
A problem => A Framework
Tests (costly and hard to realize in JEE environments)
Issues:
Proliferation of frameworks

Requires integration efforts

2009-2010 2009-2010

1
3/25/2015

Spring Response Notion of Lightweight Container


Management of Application’s Components
Notion of Lightweight Container
Configuration via XML

AOP (Aspect Oriented Programming) Support Management of components life cycles

Integration with other frameworks (Struts, Hibernate, JSF, Management of dependencies between components
etc.)
No specific infrastructure requirements
Just a JVM

2009-2010 2009-2010

Use of Spring in the Industry wrt to EJBs Spring Global Architecture

2009-2010 2009-2010

2
3/25/2015

Installing the Development Environment In this Course


Eclipse Spring covers many aspects!!
Eclipse JEE IDE
Spring IDE for Eclipse (optional)
Plugin Maven (optional) In this course we will focus on:

Tomcat - IoC (Inversion of Control) and Dependency Injection


Web Container

- Spring AOP
Framework Spring
Mainly .Jars!

tiers Framework that can be integrated to Spring


Struts, Hibernate, JUnit, …

2009-2010 2009-2010

The dependency issue in nowadays applications


The naïve approach

The Service Locator approach

Spring Framework, the foundations


The Inversion of Control approach

- IoC (Inversion of Control)


- Dependency Injection
- Lightweight Containers

2009-2010 2009-2010

3
3/25/2015

The Naïve Approach Service Locator: Approach


Example

public class Foo {


+
Example - Easy to understand/realize private IBar bar;
- private IBaz baz;
public class Foo { private IServiceLocator locator;
- Strong Coupling (dependency)
public Foo(IServiceLocator locator_) {
private IBar bar;
locator = locator_;
private IBaz baz; - Requires the knowledge of how to bar = (IBar)locator.Get(ServiceNames.BAR);
assemble dependent components baz = (IBaz)locator.Get(ServicesNames.BAZ);
public Foo() { (and their own dependencies) }
bar = new SomeBar(); }
baz = new SomeBaz(); - Need to have access to the code +
} in order to modify the application
} behavior - Easy to understand/realize
- Testable, Flexible, Extensible
- Forces the separation between the interface and the implementation
- Hard to test=> need to test also
all the dependencies. Impossibility -
to use Mocks. - Still a dependency to the Service Locator
- Need to get an instance of the Service Locator (in a static class for instance)
- Less coupling but still a coupling !!

2009-2010 2009-2010

Questions The IoC Approach


Example
How can we break this dependency?
public class Foo {
private IBar bar;
How can we ensure that Foo works with any implementation of IBar or private IBaz baz;
IBaz? Even if we don’t know them in advance (the implementations) public Foo(IBar bar_, IBaz baz_) {
bar = bar_; baz = baz_;
}
How to ensure that instances of the given implementations are
}
injected/reported inside the Foo class?
+
-The code is easy to understand/realize
But in this case, who is deciding/controlling the execution of the application? -Testable, Flexible, Extensible
It is an external application => Inversion of Control by Dependency - Forces the separation between the interface and the
Injection implementation
-

- We still need to create the dependencies but outside the application


This is where SPRING comes into the play!!
2009-2010 2009-2010

4
3/25/2015

The IoC Approach Inversion of Control within Lightweight Containers


Spring as a lightweight container takes in charge IoC with
Dependency Injection Dependency injection
Foo IBar

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>>

Principle: instantiation of the appropriate components,


management of the dependencies and the injections

2009-2010 2009-2010

Inversion of Control within Lightweight Containers


IoC within the Spring container

Spring Framework, the foundations

- IoC (Inversion of Control)


- Dependency Injection
- Lightweight Containers

2009-2010 2009-2010

5
3/25/2015

Bean Definition Bean Definition: Example


Class name => Bean’s implementation class
If the bean is built from a factory’s class static method, give the name of
this factory

Configuration of the bean’s behavior within the container <beans>


<bean id="beanFoo1" class="some.package.IBarImpl1"/>
(i.e. prototype or singleton, autowiring mode, dependency checking mode,
<bean id="beanFoo2" class="some.package.IBarImpl2" scope="prototype"/>
initialization & destruction methods)
</beans>

<!-- in case of using a class’s static method-->


Constructor arguments of the bean as well as its properties <bean id="exampleBean" class="examples.ExampleBean2" factory-method="createInstance"/>

<!-- in case of using a factory’s class static method-->

Other beans required by a given bean to execute <bean id="exampleBean" factory-bean="myFactoryBean" factory-method="createInstance"/>

i.e. collaborators => dependencies

2009-2010 2009-2010

The Bean Definition File Bean Definition: Instantiation


- Within a file named applicationContext.xml (standard) or name
defined by the user (beanDefinition.xml for instance) Two modes:
Singleton (a unique instance). By default
IMPORTANT: Prototype a new instance for each new request

- In the context of Java Desktop application, the file should be


placed at the root of the project
<bean id="exampleBean" class="examples.ExampleBean" singleton="false"/>

<bean name="yetAnotherExample" class="examples.ExampleBeanTwo" singleton="true"/>


- In the context of a web application (eg. JSP/Servlet) the file
should be placed in the WEB-INF folder (i.e. WEB-
INF/applicationContext.xml)

2009-2010 2009-2010

6
3/25/2015

Bean Definition: other options Spring’s Dependency Injection


Two Ways:
Attributes of the ‘bean’ element:

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

autowire : automatic resolution of dependencies (by type, by name).

init-method : method called automatically at bean’s creation time

destroy-method : method called automatically at bean’s destruction time

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

<property name="beanOne"><ref bean="anotherExampleBean"/></property>


<property name="beanTwo"><ref bean="yetAnotherBean"/></property> <beans>
<property name="integerProperty"><value>1</value></property>
<bean id="beanFoo" class="some.package.Foo">
</bean>
<bean id="anotherExampleBean" class="examples.AnotherBean"/> <property name="someString" value="theValue"/>
<bean id="yetAnotherBean" class="examples.YetAnotherBean"/> <property name="someBoolean" value="true"/>
public class ExampleBean { // the JAVA class <property name="someFloat" value="10.5"/>
private AnotherBean beanOne; <property name="someStringArray"
private YetAnotherBean beanTwo; value="value1,value2"/>
private int i;
</bean>
public void setBeanOne(AnotherBean beanOne) {this.beanOne = beanOne; }
</beans>
public void setBeanTwo(YetAnotherBean beanTwo) { this.beanTwo = beanTwo; }
public void setIntegerProperty(int i) { this.i = i; }
}

2009-2010 2009-2010

7
3/25/2015

Dependency Injection: the Constructor way Dependency Injection: the Constructor way
• The Java Class Bean Definition

public class ExampleBean {


<bean id="exampleBean" class="examples.ExampleBean">
private AnotherBean beanOne;
<constructor-arg><ref bean="anotherExampleBean"/></constructor-arg>
private YetAnotherBean beanTwo;
<constructor-arg><ref bean="yetAnotherBean"/></constructor-arg>
private int i;
<constructor-arg type="int"><value>1</value></constructor-arg>
public ExampleBean(AnotherBean anotherBean,
YetAnotherBean yetAnotherBean, int i) { </bean>
this.beanOne = anotherBean; <bean id="anotherExampleBean" class="examples.AnotherBean"/>
this.beanTwo = yetAnotherBean; <bean id="yetAnotherBean" class="examples.YetAnotherBean"/>
this.i = i;
}
}

2009-2010 2009-2010

Dependency Injection: the Constructor way Bean’s Life Cycle : Initialization


Bean Initialization
An Example
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
After initializing bean’s properties using either the Setter or Constructor way,
there is a mean to initialize the behavior/properties of the bean
<property name="driverClassName" value="... "/>
<property name="url" value="..."/>
<property name="username" value="...“> Two ways:
<property name="password" value="...“/>
</bean> • a) with « init-method »

<bean id="exampleInitBean" class="examples.ExampleBean" init-method="init"/>


<bean id="itemDao"
class="org.springframework.samples.jpetstore.dao.ibatis.SqlMapItemDao"> //Java Class
<property name="dataSource" ref="dataSource"/>
<property name="sqlMap" ref="sqlMap"/> public class ExampleBean {
public void init() { // do some initialization work } }
</bean>
<bean id="petStore“ • b) By implementing the «InitializingBean » interface
class="org.springframework.samples.jpetstore.domain.logic.PetStoreImpl">
<constructor-arg ref="orderDao"/> <bean id="exampleInitBean" class="examples.AnotherExampleBean"/>
<constructor-arg ref="itemDao"/>
public class AnotherExampleBean implements InitializingBean {
</bean> public void afterPropertiesSet() { // do some initialization work } }

2009-2010 2009-2010

8
3/25/2015

Bean’s Life Cycle : Finalization Property Definitions


Finalisation du Bean For a property it is possible to inject :
Possibilité d’exécuter un comportement
A constant value : value
Two ways:

• a) using the « destroy-method » A reference to another bean : ref

<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

<bean id="exampleInitBean" class="examples.AnotherExampleBean"/> Properties : props


public class AnotherExampleBean implements DisposableBean {
public void destroy() { // eg.close connection} } The name of the bean : idref

2009-2010 2009-2010

Property Definitions: Example Bean Factory & Application Context


<beans>
<bean id="beanFoo" class="some.package.Foo">
<property name="someValue" value="someHardCodedValue"/>
<property name="someRef" ref="beanBar"/>
The Bean Factory defines the interface used by the application in
<property name="someList">
<list>
order to interact with the Lightweight Container (Spring in our
<ref bean="bean1"/>
case)
<ref bean ="bean2"/>
</list>
</property>
<property name="someMap">
The Application Context is also an interface, which extends the Bean
<map>
Factory with some additional functionalities
<entry key="key1" value-ref="bean1"/>
<entry key="key2" value-ref="bean2"/>
</map>
</property>
</bean>
</beans>
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 :

ApplicationContext context = new ClassPathXmlApplicationContext(new


String[]{"applicationContext.xml",
"applicationContext-part2.xml"});
IFoo foo = (IFoo)context.getBean("foo"); ApplicationContext context =
WebApplicationContextUtils.getWebApplicationContext(getServletContext());

IFoo foo =(IFoo)context.getBean("foo");

Id of the bean defined in the XML file

Id of the bean defined in the XML file

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

Spring Jars Spring Jars

Bean Definitions File Bean Definitions File

2009-2010 2009-2010

10
3/25/2015

AOP
Introduction to AOP

AspectJ AOP

Spring Framework Spring AOP

Integration of Spring with AspectJ


AOP (Aspect Oriented Programming)

2009-2010 2009-2010

AOP: Introduction AOP: Basic Concepts


Cross-cutting issues Joinpoint
An Identifiable point in the execution of a program (Constructor, a method
invocation, etc.). In Spring AOP it always represents the execution of a
Functionalities which their implementation cross-cuts different modules method

Multiple examples: traces and logging, transaction management, security, Pointcut


caching, exception handling, performance monitoring A programmatic expression that selects the Joinpoints where to apply the
aspect

AOP Advice
Code to be executed before/after/around a Joinpoint expressed by a Pointcut

A way of programming in order to handle cross-cutting concerns.


Target
The main class for which the aspects have to be woven. The caller does not
see the difference between the target class and the proxy class (the proxy
class implements the same interfaces as the target class

2009-2010 2009-2010

11
3/25/2015

Coupling of Spring and AspectJ Spring AOP


2 possible approaches :

Declarative weaving of aspects: in the XML file Pointcut Advice

Aspect weaving using annotations.

Aspects are applied :

At compilation time : aspects + source -> class


Target
Object
Over the binary files : aspects + binaries -> binaries

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

Definition of a Pointcut Weaving of Aspects: using annotations


When a method is executed : @AspectJ
execution(void Point.setX(int))
here the code of the class that contains the method is advised by an aspect package example;

When a method is called : @Aspect


call(void Point.setX(int)) public class EmailLogger {
here the code of the client that call the method is advised by an aspect @Before("execution(* send(String, *, *,String)) && args(address, *,*,zipcode)")
public void log(String zipcode, String address)
When the executing object (‘this’) is of a certain type {
this(SomeType) }
}
When the target of an object is of a certain type :
target(SomeType) The pointcut is written similarly as in the declarative weaving way

When the code being executed belongs to a class :


within(MyClass)

2009-2010 2009-2010

13
3/25/2015

Tissage par annotations : @AspectJ Spring AOP or AspectJ?


<beans ...> Use Spring AOP when:
<aop:aspectj-autoproxy/>
<bean id="emailerLogger" Method Interceptions is enough
class="example.EmailLogger"/>
... Other beans ... We don’t want to use a specific compiler
</beans>
The pointcuts relate only to objects defined within the Spring context
You must add in the XML file the line above even if you are using annotations
Use AspectJ:

For all the rest

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

You might also like