In this discussion ,I'll be focussing on spring framework , Why spring framework came into existence , and how to use spring framework to implement business logic.
Table of Contents :-
e.g.
Table of Contents :-
- What is spring framework ?
- Modules and its overview
- Advantages
- When to use It ?
- Beans
- Dependency injection
- How to use it in Real world ?
Spring Framework :-
Spring is a lightweight framework that provides comprehensive infrastructure support for developing Java applications from plain old Java objects . It provides support to various frameworks like Struts , hibernate etc .
Modules :-
The spring framework organized into several modules such as AOP(Aspect oriented programming ) , IOC , ORM , WEB MVC etc . As Shown in Fig. 1.1 , these modules are grouped into Core Container , Data Access/Integration , Web , AOP ,Instrumentation and tests .
Overview of spring framework Modules :-
Core Container :-
Core container consists of Beans , Core ,Context and Expresion language modules .The Core and Beans provides basic parts of framework including the IOC and dependency injection features.
Context module builds on the base provided by Core and Beans. This module inherits the features from Core and Bean and add support for internationalization ,event propagation , resource loading . It also supports Java EE features such as JMX , EJB and basic remoting
The Expression Language module provides expression language for quering and manipulating the object at run time .
Data Access / Integration :-
This layer consists of JDBC , ORM , OXM ,JMS and transactions modules .
This layer consists of JDBC , ORM , OXM ,JMS and transactions modules .
- JDBC provides JDBC abstraction layer for JDBC connection .
- ORM module provides the integration layer for object relational mapping API's including hibernate , iBatis etc.
- OXM module provides an abstraction layer that supports object/XML mapping implementation for JAXB, XMLBeans.
- JMS supports for producing and consuming messages.
- Transactions module provides transaction management for all the POJOs.
Web :-
- The Web layer consists of Web ,Servlet , Portlet and struts modules.
- Web module provides web oriented features such as file upload functionality and initialization of IOC container using web application context.
- Web Servlet module provides spring MVC implementation for Web application.
- Web Struts module provides support for integrating a classic struts web tier within spring application through supporting classes.
- Web Portlet module provides the MVC implementation to be used in spring enviroment.
AOP :-
- AOP module provides an AOP Alliance complaint aspect oriented programming implementation allowing you to define method interceptors to decouple the code that implements functionality.
Instrumentation :-
- It provides class implementation support and class-loader implementations for application servers.
Test :-
- It provides support for testing of spring components with JUnit ot TestNG.
Advantages of Spring Framework :-
Loose Coupling :-
Fast Development :-
- Due to dependency injection in spring , the applications in spring are loosely coupled. Later we will see in detail about dependency injection
Lightweight :-
- The Dependency injection feature makes the development faster.
Predefined Templates :-
- Due to POJO implementation spring framework is light weight.
Declarative Support :-
- Spring framework provides templates for JDBC , hibernate etc. So there is no need of writing too much code. It hides the basic steps of these technologies.
- It provides declarative support for caching , transactions , validation etc.
When to use Spring framework :-
- In large applications , objects dependencies arise or in other words objects are tightly coupled and it becomes very difficult to manage the objects as well as applications .Also from testing perspective , application consumes too much time for testing. So Spring framework was evolved.
- Here i am taking the example of draw shape to show how objects are tightly coupled and with the help of spring framework they can be loosely coupled.
Draw Shape Example :-
I have a interface Shape and classes that implements shape :-
public interface Shape { public void drawShape(); }
class Rectangle :-
public class Rectangle implements Shape { public void drawShape(){ System.out.println("Rectangle"); } }
class Triangle :-
public class Triangle implements Shape { public void drawShape(){ System.out.println("Triangle"); } }
Main class :-
public class App { public static void main( String[] args ) { Shape shapeObj= new Rectangle (); output.drawShape(); } }
Problem in above normal call :-
shapeObj is tightly coupled to Rectangle . Any changes in interface shape may lead to changes in code . If this code is using in your whole application , then it becomes bottleneck .Next Option is we can take helper class that will create the object of class for which shape to be drawn .
e.g.
public class ShapeCreatorService { private Shape shapeObj= new Rectangle (); public void draw() { shapeObj.drawShape(); } }Now create the main class that will create the object of service class
public class App { public static void main( String[] args ) { ShapeCreatorService shapeServiceObj= new ShapeCreatorService (); shapeServiceObj.draw(); } }
Problem in above call:-
Here problem lies in ShapeCreatorService . it has a shape variable that points to Rectangle .Here main problem arises due to new operator. what in future if i want to draw triangle . I have to change the source code to draw triangle . it means classes are very tightly coupled.
Solution to above problems - Dependency Injection and How to use it in Real word :-
- Instead of changing the code of ShapeCreatorService to draw rectangle or triangle why not we assign the task to spring container which creates the instance of bean by reading spring configuartion file .
- So Spring's dependency injection came into exist to remove tight coupling between classes.
Before knowing the dependency injection you should have knowledge of beans. Basically what beans are.
Beans :-
Beans are nothing , these are simply objects .Rather than creating objects with new operator to avoid tight coupling , beans creation or instantiation is managed by IOC container . Beans are created in configuration metadata that we supply to spring container . configuration metadata is basically configuration file as shown below :-
Now create spring configuration file having following beans :-
<!-- Spring-Common.xml --> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="ShapeCreatorService" class="com.src.main.java.impl.ShapeCreatorService"> <property name="shapeObj" ref="Triangle" /> </bean> <bean id="Triangle" class="com.src.main.java.impl.Triangle" /> <bean id="Rectangle" class="com.src.main.java.impl.Rectangle" /> </beans>
- In above xml file bean is defined in bean tag . id attribute is unique in xml file and class attribute provides actual class for this bean id.
- The Bean tag has a property tag to set property of particular bean.
- The property tag has an attribute ref which is used to inject the instance to bean. ref tag uses setter method of a bean to inject an instance.
- We modify our ShapeCreatorService class ,now rather than creating the object using new operator , it will have setter method in which we pass the object of shape , i.e. we are injecting the object to it.
public class ShapeCreatorService { private Shape shapeObj;; public void draw(){ shapeObj.drawShape(); } public void setshape(Shape shapeObj){ this.shapeObj= shapeObj; } }
Main application class for calling shapeCreatorService by reading spring configuartion file.
import
org.springframework.context.ApplicationContext;
importorg.springframework.context.support.ClassPathXmlApplicationContext;
public class App { public static void main( String[] args ) { ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"Spring-Common.xml"}); //ShapeCreatorService shapeServiceObj= new ShapeCreatorService (); shapeCreatorService shapeServiceObj= (shapeCreatorService)context.getBean("shapeCreatorService"); shapeServiceObj.draw(); } }
Now you don't need to change the code, just change the xml file.
This is what about general introduction of spring framework. For further details about spring like spring MVC you can read to my next tutorial(in progress).
This is what about general introduction of spring framework. For further details about spring like spring MVC you can read to my next tutorial(in progress).
No comments:
Post a Comment