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

Introduction To The Spring: Vamsi Mohan.V

The document provides an overview of the Spring Framework, including its history starting in 2002, its mission to make J2EE development easier, and its key concepts like inversion of control and dependency injection. It describes how Spring can help with different application tiers such as presentation, business, and persistence layers. The document also summarizes Spring's benefits like reduced coding effort and support for concepts like transactions.

Uploaded by

vamsitechnology
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views

Introduction To The Spring: Vamsi Mohan.V

The document provides an overview of the Spring Framework, including its history starting in 2002, its mission to make J2EE development easier, and its key concepts like inversion of control and dependency injection. It describes how Spring can help with different application tiers such as presentation, business, and persistence layers. The document also summarizes Spring's benefits like reduced coding effort and support for concepts like transactions.

Uploaded by

vamsitechnology
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 27

Introduction to the Spring

Vamsi Mohan.V
Overview
 Introduction
Spring Framework History
 Started 2002/2003 by Rod Johnson and
Juergen Holler
 Started as a framework developed around
Rod Johnson’s 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
Latest Spring releases
 Spring Framework 3.0.5.RELEASE is the current
production release (requires Java 1.5+)
 Can be downloaded from
http://www.springsource.org/download
 Spring Framework 2.5.6.SEC02 is the latest Spring 2.5.x
release (compatible with Java 1.4+)
 Spring Framework 2.0.8 is the latest Spring 2.0.x release
(compatible with Java 1.3+)
Spring Mission Statement
 J2EE should be easier to use
 OO design is more important than any
implementation technology, such as J2EE.
 Testability is essential, and a framework such as
Spring should help make your code easier to
test.
 Spring should not compete with good existing
solutions, but should foster integration.
What is the Spring Framework?
 A lightweight non-intrusive framework which addresses various tiers in a
J2EE application.

 Presentation layer: Integrates with Struts to initialize action classes


 Business layer: Lightweight IoC container with support for AOP-driven
interceptors and transaction.
 Persistence layer – DAO template support for Hibernate, SQLMaps and
JDBC
 Factory implementation to abstract and integrate various other facets of
enterprise applications like E-mails, JMS, WebServices, etc.

 Helps integrates tiers together using XML configuration instead of hard-


coding.
 Substantially reduces code, speeds up development, facilitates easy testing
and improves code quality.
Spring == J2EE Application
 Spring is NOT a J2EE application
 Spring can integrate nicely with J2EE application (or any
Java environment)
 Spring can, in many cases, elegantly replace services
traditionally provided by J2EE application.
Lessons Learned from Struts
 Before Struts, everyone wrote their own front controllers
 After Struts, the custom front controllers could be thrown
out
 Developers focus on solving business problems
 But with Struts (and most of the other web frameworks) you
still have to write your own business delegates or service
layers…
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.
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
Spring Benefits
 Not a J2EE container. Doesn’t compete with J2EE app
servers. Simply provides alternatives.
 POJO-based, non-invasive framework which allows a la
carte usage of its components.
 Promotes decoupling and reusability
 Reduces coding effort and enforces design discipline by
providing out-of-box implicit pattern implementations
such as singleton, factory, service locator etc.
 Removes common code issues like leaking connections
and more
 Support for declarative transaction management
 Easy integration with third party tools and technologies.
Fundamental Concepts
 Dependency Injection
 Also known as IoC (Inversion of Control)
 Aspect Oriented Programming (AOP)
 Runtime injection-based
 Portable Service Abstractions
 The rest of spring
 ORM, DAO, Web MVC, Web, etc.
 Allows access to these without knowing how they actually work
Spring Architecture Overview
from springframework.org

Note: Spring distribution comes as one big jar file and alternatively as a series of smaller jars broken
out along the above lines (so you can include only what you need)
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
BeanFactories
 By default, beans are treated as singletons
 Can also be prototypes

Here is an example The bean’s fully-


The bean’s ID qualified classname

<beans>
<bean id=“widgetService”
class=“com.zabada.base.WidgetService”>
<property name=“poolSize”>
<!—-property value here-->
</property>
</bean>
</beans>

Maps to a setPoolSize() call


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>
Property Values for BeanFactories
(continued)
 The real magic comes in when you can set a property on a
bean that refers to another bean in the configuration:
<bean name=“widgetService” class=“com.zabada.base.WidgetServiceImpl”>
<property name=“widgetDAO”>
<ref bean=“myWidgetDAO”/>
</property>
</bean>

calls
setWidgetDAO(myWidgetDAO)
where myWidgetDAO is another
bean defined in the configuration

 * This is the basic concept of Inversion of Control


Inversion of Control (Dependency Injection)
 Instead of objects invoking other objects, the dependant objects
are added through an external entity/container.
 Also known as the Hollywood principle – “don’t call me I will
call you”
 Dependency injection
 Dependencies are “injected” by container during runtime.
 Beans define their dependencies through constructor
arguments or properties
 Prevents hard-coded object creation and object/service lookup.
 Loose coupling, Easy to maintain and reuse
 Helps write effective unit tests.
Non-IoC/Dependency Injection
Non-IoC Service Object
public class OrderServiceImpl implements
IOrderService {
public Order saveOrder(Order order) throws
OrderException{
try{
// 1. Create a Session/Connection object
// 2. Start a transaction
// 3. Lookup and invoke one of the methods in a
// DAO and pass the Session/Connection object.
// 4. Commit transaction
}catch(Exception e){
// handle e, rollback transaction, //cleanup, //
throw e
}finally{
//Release resources and handle more exceptions
}
}

Code is tightly coupled or interspersed with Factory code!


IoC / Dependency Injection
IoC Service Object
public class OrderSpringService implements
IOrderService {
IOrderDAO orderDAO;
public Order saveOrder(Order order) throws
OrderException{
// perform some business logic…
return orderDAO.saveNewOrder(order);
}

public void setOrderDAO(IOrderDAO orderDAO) {


this.orderDAO = orderDAO;
}

 Program to interfaces for your bean dependencies!

Code is loosly coupled


Spring AOP
 Framework that builds on the aopalliance interfaces.
 Aspects are coded with pure Java code. You do not need
to learn pointcut query languages that are available in
other AOP implementations.
 Spring aspects can be configured using its own IoC
container.
 Objects obtained from the IoC container can be transparently
advised based on configuration
 Spring AOP has built in aspects such as providing
transaction management, performance monitoring and
more for your beans
 Spring AOP is not as robust as some other
implementations such as AspectJ.
 However, it does support common aspect uses to solve
common problems in enterprise applications
AOP in Spring
 Provides way to create declarative services and
custom aspects
 Transaction management is the most common aspect
(or concern)
 Spring handles AOP via advisors or interceptors
 Interception point is a joinpoint
 A set of joinpoints are called a pointcut
 pointcuts are key to Spring AOP, they allow intercepts without
explicit knowledge of the OO hierarchy
 Action taken by an interceptor is called advice (the block of
code that runs based on the pointcut definition)
 weaving – can be done at runtime or compile time. Inserts
the advice (crosscutting concerns) into the code (core
concerns).
AOP advice types
 Around
 Most common and powerful
 Execute code before and after joinpoint
 Before
 Executes before joinpoint, cannot stop execution
 Throws
 Executes code if exception is thrown
 After return
 Executes code after normal joinpoint execution
Resources
 Spring – www.springframework.org
 J2EE without EJB – Rod Johnson/ Jurgen Hoeller
 Better, Faster, Lighter Java – Bruce Tate
 Wiring your Web Application with Open Source Java
http://www.onjava.com/pub/a/onjava/2004/04/07/wiringwe
bapps.html

You might also like