Spring Framework

Introduction

Index

  1. Definition
  2. Why Spring Needed?
  3. Spring Modules
  4. IOC and DI
    1. Introduction
    2. Differences

Definition

  1. Spring is an open source framework since February 2003.
  2. Allows us to give capability of EJBs to plain JavaBeans without using an application server.
  3. Spring is a lightweight , dependency injection and aspect-oriented container and framework. works on IOC(Inversion of control)
    1. Lightweight Lesser no of JARs – JavaBeans / POJOs in Spring-enabled application often have no dependencies on Spring-specific classes.
    2. Dependency Injection Spring allows loose coupling through dependency injection (DI).
      • DI =instead of an object looking up dependencies from a container(in EJB from EJB container or in RMI from RMIRegistry) or creating its own dependency(in Fixed JDBC , conn = DM.getCn(….)) , the container gives the dependencies to the object at instantiation without waiting to be asked.
      • You can think of D.I as JNDI(Java naming & directory iterface — Naming service) in reverse.
    3. Aspect-oriented Spring supports aspect-oriented programming(AOP)
    4. (AOP) allows separating application business logic from system services (such as auditing and transaction management,logging , security,transactions). Application objects perform only business logic and nothing more.
    5. Why Spring is a Container ?
      1. Spring is a container which manages the life-cycle and configuration of application objects.(spring beans) In Spring, using config XMLs or annotations or java config, you can declare –
        • how to create each of your application objects(spring beans)
        • how to configure them
        • how they should be associated with each other.(collaboration/wiring/coupling=connecting dependencies with dependent objs)
    6. Why Spring is a framework ?
      1. Spring allows you to configure and compose complex applications from simpler components. In Spring, application objects are composed declaratively, typically in an XML file or using annotations.
      2. Spring also provides you with ready made implemetations of services like – transaction management, persistence framework,web mvc etc.
Spring IOC container

Why learn one more framework — when you already have EJB,Struts,Hibernate etc….

  1. Build applications from plain old Java objects (POJOs) (known as spring beans )
  2. Apply enterprise services non-invasively.
    • (w/o invasion means — POJOs DONT implement or extend from spring APIs) This capability applies to the Java SE programming model and to full and partial Java EE.
  3. Example of how you, as an application developer, can use the Spring platform advantage:
    1. Make a Java method execute in a database transaction without having to deal with transaction APIs.
    2. Make a local Java method a remote procedure without having to deal with remote APIs.
    3. Make a local Java method an ORM operation without having to deal with overheads of ORM set up.

Why Spring needed? (in Simple terms – generic term & 4 key points of why spring)

To simplify Java development.

  1. lightweight & min intrusive(POJOs not tied to spring) development with POJOs
  2. Loose coupling thro’ DI/IoC & with usage of i/f
  3. Declarative prog(XML or anno or java config) + thro’ Aspects & common conventions
  4. Boilerplate code reduction thro aspects & templates.

Spring framework Modules

Spring Modules
  1. Core Container
    1. beans
    2. Core
    3. Context
    4. Expression Language
  2. Web
    1. Web
    2. Serlvlet
    3. Portlet
    4. Struts
  3. Data Access/Integration
    1. ORM
    2. JMS
    3. JDBC
    4. Transactions
  4. AOP
  5. Test

IOC And DI

  • The org.springframework.beans and org.springframework.context packages are the basis for Spring Framework’s IoC container.

IOC- Inversion of Control

  1. Inversion of Control Rather a generic term, (IoC) is an object-oriented programming practice where the object coupling(dependent obj bound with dependency) is bound at run time by an assembler object(eg spring container) and is typically not known at compile time using static analysis.
  2. Unlike in traditional programs — where dependent obj creates dependencies leading to tight coupling , container sets the dependencies (not US –not a prog or not a dependent obj) —so its inversion of control

Inversion of Control is sometimes referred to as the “Hollywood Principle: Don’t call us, we’ll call you”,

Why IoC or Dependency Injection (advantages of IoC)

  1. There is a decoupling of the execution of a certain task from implementation.
  2. Every module can focus on what it is designed for.
  3. Modules make no assumptions about what other systems do but rely on their contracts/specs (=i/f)
  4. Replacing modules has no side effect on other modules.

Dependency injection (DI)

  1. (DI) is the ability to inject dependencies. DI can help make your code architecturally pure. It aids in using a design by interface approach as well as test driven development by providing a consistent way to inject dependencies.
    • For example :
      1. a data access object (DAO) may need a database connection. Thus the DAO depends on the database connection. Instead of looking up the database connection with JNDI, you could inject it.
      2. or another eg is JMS — conn factory or destination

Why Spring – Code example!

Published by

Unknown's avatar

sevanand yadav

software engineer working as web developer having specialization in spring MVC with mysql,hibernate

Leave a comment