spring framework

32
orkshop of Spring Application Framework at Capegemini Presented By Rajkumar Singh www.rajkrrsingh.blogspot.c

Upload: rajkumar-singh

Post on 06-May-2015

2.339 views

Category:

Technology


2 download

DESCRIPTION

Spring workshop at Capegemini Pune on 15th June 2012 presented by Rajkumar Singhwww.rajkrrsingh.blogspot.con

TRANSCRIPT

Page 1: Spring framework

Workshop of Spring Application Framework at Capegemini

Presented ByRajkumar Singh

www.rajkrrsingh.blogspot.com

Page 2: Spring framework

Agenda

www.rajkrrsingh.blogspot.com

IntroductionSpring ArchitectureSpring BeansSpring IOCSetting up Spring EnvironmentBeans Life cyclesSpring DAO Spring ORMSpring AOPQ&A

Page 3: Spring framework

Introduction

www.rajkrrsingh.blogspot.com

Spring framework was initially written by Rod Johnson and was first released under the Apache 2.0 license in June 2003.

Primary purpose is to reduce the dependencies(loose couple)

Spring deliver many significant benefits to the developers reducing development effort and cost while improving test coverage and quality

Handle the infrastructure so that developer can focus on the development only

Enables you to build application from POJOs

Spring does not recreate all the framework but make use of the existing one like ORM,logging,JEE framework.

Page 4: Spring framework

What Spring Offers

www.rajkrrsingh.blogspot.com

Dependency Injection

Aspect Oriented Programming (AOP)

Portable SerivesORMDAOMVC

Page 5: Spring framework

Application Layering

www.rajkrrsingh.blogspot.com

Presentation LayerSpring MVCSpring Web

Persistence LayerJDBCORM

Domain LayerPOJOs

Service Layer Gateway to expose business logic to the outside world

Not well defined in many applications today or tightly coupled in an inappropriate layer.

Manages ‘container level services’ Eg. transactions, security, data access logic, and manipulates domain objects

Page 6: Spring framework

Spring Architecture

www.rajkrrsingh.blogspot.com

Page 7: Spring framework

Spring Architecture

www.rajkrrsingh.blogspot.com

Core & Beans: Provides fundamental functionality & Dependency Injection features.Primary component is the BeanFactory (Factory pattern )

The Context module Builds on the base modules. Access objects in a framework-style manner similar to a JNDI registry. Also supports Java EE features such as EJB, JMX ,and basic remoting. The ApplicationContext interface is the focal point of the module.

The Expression Language module Provides a powerful expression language for querying and manipulating an object graph at runtime.

Page 8: Spring framework

Spring Architecture

www.rajkrrsingh.blogspot.com

The JDBC module a JDBC-abstraction layer that removes tedious JDBC coding, parsing of database-vendor specific error codes.

The ORM module integration layers for ORM APIs, including JPA, JDO, Hibernate, & iBatis.

The OXM module supports Object/XML mapping implementations for JAXB, Castor, XMLBeans, JiBX and XStream.

The JMS Modulecontains features for producing and consuming messages.

The Transaction module supports programmatic and declarative transaction management

Page 9: Spring framework

Spring Architecture

www.rajkrrsingh.blogspot.com

Spring's Web module provides basic web-oriented integration features (multipart file-upload) initialization of the IoC container using servlet listeners

Web Servlet Spring's model-view-controller (MVC)

The Web-Struts module contains Support classes for integrating a classic Struts web tier within a Spring application.

The Web-Portlet module provides the MVC implementation to be used in a portlet environment

Page 10: Spring framework

Spring IOC

www.rajkrrsingh.blogspot.com

Inversion of control is all about Object Dependencies

Traditional Object Creation Approach Direct Object Creation new Employee()

FactoryImplementation EmpFactory().getEmp()JNDI Services naming.lookup()

Spring Approach

Spring Container creates all the objects, wires them together by setting the necessaryproperties, and determines when methods willbe invoked.

Page 11: Spring framework

Spring Container

www.rajkrrsingh.blogspot.com

Fundamental part of the framework. Two packages provides the basis for the Spring Framework's IoC container. – org.springframework.beans – org.springframework.context

BeanFactory provides the configuration framework and basic functionality, and the ApplicationContext adds more enterprise-specific functionality.

The org.springframework.beans.factory.BeanFactory is the actual representation of the Spring IoC container

Responsible for containing and managing beans.

The most commonly used BeanFactory implementation is the XmlBeanFactory class.

The XmlBeanFactory takes XML configuration metadata and uses it to create a fully configured system or application.

Page 12: Spring framework

Spring Container

www.rajkrrsingh.blogspot.com

The interface org.springframework.context.ApplicationContext represents the Spring IoC container and is responsible for instantiating, configuring, and assembling the beans.

The container gets its instructions by reading configuration metadata.

The configuration metadata is represented in XML and Java annotations

Several implementations of the ApplicationContext interface are supplied with Spring.

In standalone applications it is common to create an instance of ClassPathXmlApplicationContext or FileSystemXmlApplicationContext

Page 13: Spring framework

Ways To Initialize Spring Container

www.rajkrrsingh.blogspot.com

Page 14: Spring framework

Type of Dependency Injection

www.rajkrrsingh.blogspot.com

Setter method Injection

Constructor Injection

Configuration File Injection

Page 15: Spring framework

Examples for dependency Injection

www.rajkrrsingh.blogspot.com

Setup Enviornment for Spring

Install JAVAIf you are running Windows and installed the JDK in C:\jdk1.6.0_15, you would have to put the following line in your C:\autoexec.bat file.set PATH=C:\jdk1.6.0_15\bin;%PATH% set JAVA_HOME=C:\jdk1.6.0_15

Unix (Solaris, Linux, etc.), if the SDK is installed in /usr/local/jdk1.6.0_15 and you use the C shell, you would put the following into your .cshrc file.setenv PATH /usr/local/jdk1.6.0_15/bin:$PATH setenv JAVA_HOME /usr/local/jdk1.6.0_15

Download Spring Jars

Install Common LoggingDownload Apache Commons Logging API from http://commons.apache.org/logging

Install Eclipse and setup for Spring

Page 16: Spring framework

Reference to Other Beans (DI)

www.rajkrrsingh.blogspot.com

Page 17: Spring framework

Some More Example

www.rajkrrsingh.blogspot.com

Alias

Import

Initialize bean static Bean Factory Method

<bean id="mp3ply" class="com.test.Mp3Player" factory-method="factoryMethod">

</bean>

public Class Mp3Player{private static Mp3Player mp3ply = new Mp3Player();public Mp3Player(){}

public static Mp3Player factoryMethod(){return mp3ply;}}

Page 18: Spring framework

Some More Example

www.rajkrrsingh.blogspot.com

Collections

Inner Beans

Autowire

Namespaces

Depends Upon

ApplicationContextAware interface

BeanNameAware interface

InitializingBean interface

Page 19: Spring framework

Beans Scopes

www.rajkrrsingh.blogspot.com

Scope Description

singleton This scopes the bean definition to a single instance per Spring IoC container (default).

prototype This scopes a single bean definition to have any number of object instances.

requestThis scopes a bean definition to an HTTP request. Only valid in the context of a web-aware Spring ApplicationContext.

sessionThis scopes a bean definition to an HTTP session. Only valid in the context of a web-aware Spring ApplicationContext.

global-sessionThis scopes a bean definition to a global HTTP session. Only valid in the context of a web-aware Spring ApplicationContext.

Page 20: Spring framework

Beans Life Cycle

www.rajkrrsingh.blogspot.com

 When a bean is instantiated, it may be required to perform some initialization to get it into a usable state

When the bean is no longer required and is removed from the container, some cleanup may be required.

Register the shutdown hook for the ApplicationContext

Callback Methods: Init and destroy methodsThe init-method attribute specifies a method that is to be called

on the bean immediately upon instantiationdestroy-method specifies a method that is called just before a bean

is removed from the container.

Page 21: Spring framework

Beans Post Processors

www.rajkrrsingh.blogspot.com

  The BeanPostProcessor interface defines callback methods that you can implement to provide your own instantiation logic

You can also implement some custom logic after the Spring container finishes instantiating, configuring, and initializing a bean by impl PostProcessors

You can have multiple Bean Post processor and can define their order of execution

ApplicationContext automatically detects any bean as a post processor who implements BeanPostProcessor Interface

Page 22: Spring framework

Aspect Oriented Programming(AOP)

www.rajkrrsingh.blogspot.com

ObjectAmethodA()..methodN()v

ObjectBmethodA()..methodN()

ObjectCmethodA()..methodN()

ObjectDmethodA()..methodN()

Logging Security

Aspect Configuration

Page 23: Spring framework

Aspect Oriented Programming(AOP)

www.rajkrrsingh.blogspot.com

Complements OO programming  Aspect Oriented Programming entails breaking down program logic into distinct parts called so-called concerns

Cross-cutting concerns are conceptually separate from the application's business logic, e.g. ogging, auditing, declarative transactions, security, and caching etc

Spring AOP module provides interceptors to intercept an application, for example, when a method is executed, you can add extra functionality before or after the method execution.

Page 24: Spring framework

Aspect Oriented Programming(AOP)

www.rajkrrsingh.blogspot.com

Components of AOP

Aspect – unit of modularity for crosscutting concerns

Join point – well-defined points in the program flow

Pointcut – join point queries where advice executes

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

Page 25: Spring framework

Setting up AOP Environment in Eclipse

www.rajkrrsingh.blogspot.com

AOP Dependencies

AspectJ: http://www.eclipse.org/aspectj/downloads.phpAOP Alliance: http://aopalliance.sourceforge.net/CGILIB: http://cglib.sourceforge.net/Spring 3 ASM: http://asm.ow2.org/

Examples…..

Page 26: Spring framework

Spring DAO and ORM

www.rajkrrsingh.blogspot.com

Built in code templates that support JDBC, Hibernate, JDO, and iBatis SQL Maps

Simplifies data access coding by reducing redundant code and helps avoid common errors.

Alleviates opening and closing connections in your DAO code.

No more ThreadLocal or passing Connection/Session objects.

Transaction management is handled by a wired bean

You are dropped into the template with the resources you need for data access – Session, PreparedStatement, etc.

Optional separate JDBC framework

Page 27: Spring framework

JdbcTemplate

www.rajkrrsingh.blogspot.com

Central class in the JDBC core package

Handles the creation and release of resources.

Executes the core JDBC workflow like statement creation andExecution

Executes– SQL queries– update statements– stored procedure calls– iteration over ResultSets– extraction of returned parameter values.

Also catches JDBC exceptions

Page 28: Spring framework

DataSource Configuration

www.rajkrrsingh.blogspot.com

Page 29: Spring framework

Spring ORM : Working with Hibernate

www.rajkrrsingh.blogspot.com

Page 30: Spring framework

Spring ORM : Working with Hibernate

www.rajkrrsingh.blogspot.com

Page 31: Spring framework

Q&A

www.rajkrrsingh.blogspot.com

Page 32: Spring framework

Thanks

if you have any query concernswrite to me

[email protected]

www.rajkrrsingh.blogspot.com