Laliwala IT Services

Laliwala IT Services
Website Development

Wednesday, February 19, 2014

Spring

Introduction Spring



l    What Is exactly Spring?

l    Overview Of Dependancy Injection.

l    Overview Of Aspect-Oriented Programming(AOP).

l    Spring Application Context.

l    Bean Life Cycle

l    Creating Bean And Xml Configuration

l    Spring Module






















What Is exactly Spring?

u   Spring is great framework for development of Enterprise grade applications.

u   Spring is a light-weight framework for the development of enterprise-ready applications.

u   Spring can be used to configure declarative transaction management, remote access to your logic using RMI or web services, mailing facilities and various options in persisting your data to a database.

u   Spring framework can be used in modular fashion(MVC).

u   it allows to use in parts and leave the other components which is not required by the application.

u    It is Developed based On Dependancy injection and Aspect oriented Programming.












Dependancy Injection


u   Dependancy injection (DI) is makes Your Code Simpler,Easier to Understand ,And Eassier to Test.

u   Its Basic Design Principle on which Spring Use.

u   It Avoid writing unnecessary creation and lookup code.

u   It Can lead with Highly Coupled and hard -to-test Code. To make its loose Coupling.

u   don’t call me I will call you


Eg.


              If class X,requires a class Y's functionlity,
We dont have to write a number of lines of code to create an instance of X to use its'method,Spring uses DI and inject the object of Y into object X during runtime.



 


                   

 








Aspect-Oriented Programming(AOP)
        AOP is m Mechanism by Which you can introduce functionalities into your existing codeWithout modifyinfig the design of it.
u            It Referred As cross-cutting concern. Because they tend to cut across mulitiple components in a system.
u        However, generic functionalities like logging, security or even transactions are often needed in many places in the application. We can state that those functionalities cross-cut the application, and impact a lot of classes.

u              The problem
        Cross-cutting concerns cannot be captured cleanly inside a single abstraction, and cannot even be reused. Therefore, we’re forced to write the same code over and over again (most of the time we simply copy and paste the code, and slightly adapting it to the context). By doing this, it’s clear that we’re failing to modularize our application, introducing code duplication (ex. logging) and coupling of concerns (ex. security check in an account method).
        Of course, the code that handles these concerns can be added to each class separately, but wouldn’t that violates the principle that each class has well-defined responsibilities?
        AOP defines a new programming dimension, called an aspect. Aspects are used to capture cross-cutting functionalities in a separate programmatic entity.
AOP helps to modularize cross-cutting concerns and eliminates coupling and code duplication. With these aspects out of the way, developers can focus on core problems.
Examples of cross-cutting concerns
·     Logging & Tracing
·     Transaction management
·     Security
·     Caching
·     Locking

·     Event handling 



Spring Application Context

u   Application Context  is Spring Container Responsible for Providing Dependancy Injection.

u   It Act As a Respository For  all The  Configured bean.

u   Org.Springframework.context wildly used container in Enterprise level features.

u   Spring Comes With Sevral Application Context.

        1. ClasspathXmlApplicationContext
      
       -load context from an xml file located in classpath

         2. FileSystemXmlApplicationContext

       -load context from an xml file located in Given                 Directory

         3. XmlwebApplicationContext


       -load context from an xml file ffor Web Application





Creating Bean And Xml Configuration
Wiring:
           
                        To Inject one bean value to another bean is called wiring.You can create wiring with two way
           
1.By Setter Based

            Public class person
                {
                                Private String name;
                                Private Car car;
               
                                Public void setName(String name)
                                {
                                                this.name=name;
                                }
               
                                Public String getName()
                                {
                                                Return name;
                                }                                                                                                          Creating  Person Bean Class

                                Public void setCar(Car car)
                                {
                                                This .car=car;
                                }

  
                                 Public Car getCar()
                                 {
                                                Return car;
                                 }

                }
                Public class Car
                {
                                Private String model;
                                Private String color;

                                Public String model;
                                Private String colr;
               
                                Public void setModel(String model)
                                {
                                                this.model=model;
                                }
                                 Public String getModel()
                                {
                                   Return model;                                                                                      Creating Car Bean Class
                                 }

                                Public void setColor(String color)
                                {
                                                   this.color=color;
                                  }

                                Public String getColor()
                                 {
                                                Return color;
                                }
                }
      
Lets Now Make Xml Configuration Of the Both Bean Class

            for that first we need to create one XML file. And then Write
            1) xml Schema
            2) And Setting Bean Property

Xml Schema References


       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”                         >

....


Setting Up Bean Property

           
           


           
           

2.By Constructor Based


                Public class person
                {
                                Private String name;
                                Private Car car;

                                Public Person(Car car)
                                {
                                                this.car=car;
                                }
                                Public void setName(String name)
                                {
                                                this.name=name;
                                }              
               
                                Public String getName()
                                {
                                                Return name;
                                }

                                Public Car getCar()
                                {              
                                                Return carr;
                                }
                }


                Public class Car
                {
                                Private String model;
                                Private String color;

                                Public Car(String model,String color)
                                {
                                                this.model=model;
                               
                                }
                                Public void setModel(String model)
                                {
                                                this.model=model;
                                }
               
                                Public String getModel()
                                {
                                                Return model;
                                }

               
                                 Public void  setColor()
                                 {
                                                this.color=colorr;
                                }              
               
                                 Public String getColor()
                                 {
                                                Return color;
                                }
                }



Xml Configuratuin

           
           
           
           






No comments:

Post a Comment