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

Spring Overview Tuitorial

Basic tuitorial about spring framework

Uploaded by

Sáshí Kïràñ
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
83 views

Spring Overview Tuitorial

Basic tuitorial about spring framework

Uploaded by

Sáshí Kïràñ
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 51

Spring Overview

TALENTSPRINT | Copyright 2012


Topics:

Introduction to Spring
Motivation for Spring
Spring Background
The Spring Framework
Features of Spring framework
Spring's non-invasive programming model
Simplicity and Power
Design Principles behind Spring
Controlling Object Creation: The Core
Module
Web Applications: The Web Module
Persistence: The DAO and ORM Modules
The Aspect-Oriented Programming Module
Installation and Configuration

2
TALENTSPRINT | Copyright 2012 2
What is the Spring Framework?

Spring is a Lightweight
Application Framework
Where Struts, WebWork and
others can be considered Web
frameworks, Spring addresses all
tiers of an application
Spring provides the plumbing so
that you dont have to!

TALENTSPRINT | Copyright 2012 3 3


Spring Framework History

Started 2002/2003 by Rod


Johnson and Juergen Holler
Started as a framework
developed around Rod
Johnsons book Expert One-
on-One J2EE Design and
Development
Spring 1.0 Released March
2004
2004/2005 Spring is emerging
as a leading full-stack
Java/J2EE application
framework
4
TALENTSPRINT | Copyright 2012 4
Spring == J2EE Application Server?

Spring is NOT a J2EE application


server
Spring can integrate nicely with
J2EE application servers (or any
Java environment)
Spring can, in many cases,
elegantly replace services
traditionally provided by J2EE
application servers

TALENTSPRINT | Copyright 2012 5 5


Lessons Learned from Struts

Before Struts, everyone wrote their own


front controllers (or YIKES! put their
controller logic in JSP)
After Struts, the custom front
controllers could be thrown out
Developers focus on solving business
problems
Productivity Gain!
But with Struts (and most of the other
web frameworks) you still have to write
your own business delegates or service
layers

TALENTSPRINT | Copyright 2012 6 6


Spring Can Help!

Spring brings a consistent structure to


your entire application
Spring provides a consistent way to
glue your whole application together
Spring provides elegant integration
points with standard and defacto-
standard interfaces: Hibernate, JDO,
TopLink, EJB, RMI, JNDI, JMS, Web
Services, Struts, etc.
Just as Struts did on the web tier, we
can realize huge productivity gains by
not having to write the common
integration points across your
application
TALENTSPRINT | Copyright 2012 7 7
The Spring Framework Mission Statement

The authors of Spring believe that:


J2EE should be easier to use
It's best to program to interfaces, rather than
classes. Spring reduces the complexity cost of
using interfaces to zero.
JavaBeans offer a great way of configuring
applications.
OO design is more important than any
implementation technology, such as J2EE.
Checked exceptions are overused in Java. A
framework shouldn't force you to catch
exceptions you're unlikely to be able to
recover from.
Testability is essential, and a framework such
as Spring should help make your code easier
to test.
TALENTSPRINT | Copyright 2012 8 8
Spring Overview

TALENTSPRINT | Copyright 2012 9 9


Spring Core

The Core package is the most


import component of the Spring
Framework.
This component provides the
Dependency Injection features.
The BeanFactory provides a
factory pattern which separates
the dependencies like initialization,
creation and access of the objects
from your actual program logic.
TALENTSPRINT | Copyright 2012 10 10
Spring AOP

One of the key components of


Spring is the AOP framework. AOP
is used in Spring:
To provide declarative enterprise services, especially as a
replacement for EJB declarative services. The most important such
service is declarative transaction management, which builds on
Spring's transaction abstraction.
To allow users to implement custom aspects, complementing their
use of OOP with AOP

TALENTSPRINT | Copyright 2012 11 11


Spring DAO

The DAO (Data Access Object)


support in Spring is primarily for
standardizing the data access work
using the technologies like JDBC,
Hibernate or JDO.

TALENTSPRINT | Copyright 2012 12 12


Spring ORM

The ORM package is related to the


database access. It provides
integration layers for popular
object-relational mapping APIs,
including JDO, Hibernate and
iBatis.

TALENTSPRINT | Copyright 2012 13 13


Spring Web

The Spring Web module is part of


Springs web application
development stack, which includes
Spring MVC.

TALENTSPRINT | Copyright 2012 14 14


Spring Web MVC

This is the Module which provides


the MVC implementations for the
web applications.

TALENTSPRINT | Copyright 2012 15 15


Spring Context

This package builds on the beans


package to add support for
message sources and for the
Observer design pattern, and the
ability for application objects to
obtain resources using a consistent
API

TALENTSPRINT | Copyright 2012 16 16


Spring is Non-Invasive

What does that mean?


You are not forced to import or
extend any Spring APIs
An invasive API takes over your
code.
Anti-patterns:
EJB forces you to use JNDI
Struts forces you to extend Action

TALENTSPRINT | Copyright 2012 17 17


But really, what IS Spring?

At its core, Spring provides:


An Inversion of Control Container
Also known as Dependency Injection
An AOP Framework
Spring provides a proxy-based AOP framework
You can alternatively integrate with AspectJ or
AspectWerkz
A Service Abstraction Layer
Consistent integration with various standard and 3rd
party APIs

These together enable you to write powerful,


scalable applications using POJOs.

TALENTSPRINT | Copyright 2012 18 18


Seriously though, what IS Spring?

Spring at its core, is a


framework for wiring up your
entire application

BeanFactories are the heart


of Spring

TALENTSPRINT | Copyright 2012 19 19


BeanFactories

A BeanFactory is typically
configured in an XML file with the
root element: <beans>
The XML contains one or more
<bean> elements
id (or name) attribute to identify the bean
class attribute to specify the fully qualified class

TALENTSPRINT | Copyright 2012 20 20


BeanFactories

package pack;

public class HelloWorld {

public void sayHellow()


{
System.out.println("HELLO WORLD");

TALENTSPRINT | Copyright 2012 21 21


BeanFactories

By default, beans are treated as singletons


Can also be prototypes
Here is an example: The beans ID

<beans>
<bean id="a" class="pack.HelloWorld">
<property name=msg> The beans fully-
qualified
<!-property value here--> classname
</property>
</bean>
</beans>
Maps to a sayHellow()
call

TALENTSPRINT | Copyright 2012 22 22


Property Values for BeanFactories

Strings and Numbers


<property name=size><value>42</value></property>
<property name=name><value>Jim</value></property>

Arrays and Collections


<property name=hobbies>
<list>
<value>Basket Weaving</value>
<value>Break Dancing</value>
</list>
</property>

TALENTSPRINT | Copyright 2012 23 23


Property Values for BeanFactories

The real magic comes in when you


can set
a property on a bean that refers to
another
bean in the configuration:
<bean id="b1" class="pack.
HelloIndia">
<property name="s" ref="c"/>
</bean>
<bean id="a" class="pack.
HelloWorld"/>
<bean id="c" class="pack.
Student"/> This is the basic concept
of Inversion of Control

TALENTSPRINT | Copyright 2012 24 24


Dependency Injection (Inversion of Control)

Complicated sounding terms


for a fairly simple concept
The Hollywood Principle:
Dont call me, Ill call you
Dependencies used from within
a bean arent asked for
outwardly, but are injected
into the bean by the container

TALENTSPRINT | Copyright 2012 25 25


Dependency Injection (Inversion of Control)

Eliminates lookup code from within


your application
Allows for pluggablity and hot
swapping
Promotes good OO design
Enables reuse of existing code
Makes your application extremely
testable
TALENTSPRINT | Copyright 2012 26 26
Dependency Inversion: Precursor to Dependency
Injection

The first reference to what would eventually


become Dependency Injection appeared in
1994 in a paper by Robert C. Martin called
The Dependency Inversion Principle.
In The Dependency Inversion Principle (or
DIP), the author states the three defining
factors of bad code:
It is hard to change because every change affects
too many other parts of the system (Rigidity)
When you make a change, unexpected parts of the
system break (Fragility)
It is hard to reuse in another application because it
cannot be disentangled from the current application
(Immobility).

TALENTSPRINT | Copyright 2012 27 27


Dependency Inversion: Precursor to Dependency Injection

According to Martin, interdependency


causes these coding problems (we'll call
them RFI for Rigidity, Fragility, and
Immobility). To fix RFI issues in your
OO code, DIP has two basic rules:
High level modules should not depend upon
low level modules, both should depend upon
abstractions.
Abstractions should not depend upon
details, details should depend upon
abstractions

TALENTSPRINT | Copyright 2012 28 28


High level modules should not depend upon low level modules,
both should depend upon abstractions.

In other words, high level modules


which contain your business logic and
all of the important meat of your
application should not depend on
lower level components.
The reason for this is if these lower
level components were to change, the
changes might affect the higher level
components as well.
This is the defining concept behind
dependency inversion, that the
prevailing wisdom of having higher-level
modules dependent on lower-level
modules is in fact a bad idea.
TALENTSPRINT | Copyright 2012 29 29
Abstractions should not depend upon details, details
should depend upon abstractions

This is another way to say that before


you begin coding to the abstraction
the interface or abstract class -- you
should find the common behaviors in
the code and work backwards.
Your interface /abstraction should cater
to the intersection between the needs of
your business logic and the common
behaviors of the lower level modules.
You should also leave the details of how
these behaviors are implemented to the
implementation classes.
TALENTSPRINT | Copyright 2012 30 30
Spring AOP

Aspect-oriented programming (AOP)


provides for simplified application of
cross-cutting concerns
Transaction management
Security
Logging
Auditing
Locking
AOP sometimes (partially) achieved via
Decorators or Proxies
CORBA Portable Interceptors
Servlet Filters

TALENTSPRINT | Copyright 2012 31 31


Data Access

DAO support provides pluggable


framework for persistence
Currently supports JDBC, Hibernate,
JDO, and iBatis
Defines consistent exception hierarchy
(based on RuntimeException)
Provides abstract Support classes for
each technology
Template methods define specific queries

TALENTSPRINT | Copyright 2012 32 32


JDBC Support

JDBCTemplate provides
Translation of SQLExceptions to more meaningful Spring Runtime
exceptions
Integrates thread-specific transactions

MappingSQLQuery simplifies
mapping of ResultSets to Java
objects

TALENTSPRINT | Copyright 2012 33 33


Spring Web Framework

DispatcherServlet

The DispatcherServlet is the Spring


Front Controller
Initializes WebApplicationContext
Uses /WEB-INF/[servlet-name]-servlet.
xml by default
WebApplicationContext is bound into
ServletContext

TALENTSPRINT | Copyright 2012 34 34


Spring
Installation and Configuration

35
TALENTSPRINT | Copyright 2012 35
Spring IDE - Installation

Spring IDE is an eclipse plug-in


that helps in developing Spring
Application.
First we will see how to install the
Spring IDE and later we will create
our first Spring project using it.
We are using Eclipse 3.4.1 version
to demonstrate this.

TALENTSPRINT | Copyright 2012 36 36


To install Spring IDE, Go to Help -> Software Updates

TALENTSPRINT | Copyright 2012 37 37


Add Update Site

Click the "Add Site" button and enter


"http://springide.org/updatesite" in the
Add Site popup.

38
TALENTSPRINT | Copyright 2012 38
Select Spring features

Select all the Spring IDE features and


click Install.

39
TALENTSPRINT | Copyright 2012 39
First Spring project

Once the
installation is
complete you are
done. Now let's
see how to create
the hello world
example using the
Spring IDE.
First create a
Spring project, go
to File -> New ->
Project.
TALENTSPRINT | Copyright 2012 40 40
Select Spring Project and click Next

TALENTSPRINT | Copyright 2012 41 41


Enter the project name and click Finish.

The "S" in the upper right


corner indictes it is a Spring
Project.

TALENTSPRINT | Copyright 2012 42 42


HelloWorld Class

Right click the src package and create a new


package "com.company.data". Create the following
HelloWorld class

package com.company.data;
public class HelloWorld {
private String message;
public void setMessage(String message) {
this.message = message;
}
public void sayHellow()
{
System.out.println(message);
}
}

TALENTSPRINT | Copyright 2012 43 43


Now we have created the
HelloWorld bean class, the next
step is to add an entry for this in
the bean configuration file.
The bean configuration file is used
to configure the beans in the
Spring IoC container.

TALENTSPRINT | Copyright 2012 44 44


Bean Configuration File

To create a new
bean
configuration file
right click the src
folder and select
New -> Spring
Bean
Configuration File.

45
TALENTSPRINT | Copyright 2012 45
Bean Configuration File

Enter the bean name and click Next.

46
TALENTSPRINT | Copyright 2012 46
Bean Configuration File

Select the beans option and click


Finish.

47
TALENTSPRINT | Copyright 2012 47
Bean Configuration File

Now the Spring configuration file is


created. Add the following code to
created an entry for the HelloWorld
bean class.

<bean id="a" class=com.company.data.


HelloWorld">
<property name="message" value="Hello
World!"></property>
</bean>

TALENTSPRINT | Copyright 2012 48 48


Bean Configuration File

The id attribute of the bean element is used to


give a logical name to the bean and the class
attribute specifies the fully qualified class
name of the bean. The property element
within the bean element is used to set the
property value. Here we set the message
property to "Hello World!".

If you want to display a different message, the


only change you need to is to change the
value of the message in the bean configuration
file.

This is one of the main benefits of using the


Dependency Injection design pattern, this
makes the code loosely coupled.

TALENTSPRINT | Copyright 2012 49 49


To display the message, create the following HelloWorldApp
class.

package com.company.apps;

import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

public class HelloWorldApp {


public static void main(String[] args) {
XmlBeanFactory fact=new XmlBeanFactory(new
ClassPathResource(com/sqlstar/beans.xml"));
HelloWorld h=(HelloWorld)fact.getBean("a");
h.sayHellow();
}
} To execute the example run the HelloWorldApp file. The "Hello World!" message gets
printed on the console.

TALENTSPRINT | Copyright 2012 50 50


Spring Overview

TALENTSPRINT | Copyright 2012 51

You might also like