20
What is Spring Framework : * The Spring Framework is an open source Lightweight Application Framework for the Java platform . * Spring is a general purpose framework we can develop Console Application, Web Application, Enterprise Application etc. * Spring provides a consistent framework for data access, whether using JDBC or an O/R mapping product such as TopLink, Hibernate or a JDO implementation. * Year 2002, Rod Johnson release it's first version. * The Spring 1.0 was released March 2004 under the Apache 2.0 license. * The Spring Framework manages the bean creation and lifecycle phases. It is better design to manage the beans and its dependency in the application. * Spring 2.5 or above version support Java 5 Annotations. For Spring 3 at least Jdk 1.5 is required. Features of Spring : LightWeight, Inversion of Control (IOC), AOP (Aspect Oriented Programming), MVC Framework, Transaction Management. Features of Spring 3.0 Spring 3.0 framework supports Java 5. It provides annotation based configuration support. Java 5 features such as generics, annotations, varargs etc can be used in Spring. A new expression language Spring Expression Language SpEL is being introduced. The Spring Expression Language can be used while defining the XML and Annotation based bean definition. Spring 3.0 framework supports REST web services. Data formatting can never be so easy. Spring 3.0 supports annotation based formatting. We can now use the @DateTimeFormat(iso=ISO.DATE) and @NumberFormat(style=Style.CURRENCY) annotations to convert the date and currency formats. Spring 3.0 has started support to JPA 2.0. Advantage of Spring Framework : * Easily integrate with Hibernate, JPA, EJB 3, JSF, Struts etc. * The Spring framework does not limited it self to any specific domain or API. It can be used a Console application, Web application, Enterprise application etc. It means that this is a general purpose framework. * Applications built using Spring are very easy to unit test. * Spring helps us solve many problems without using EJB. Spring can provide an alternative to EJB that's appropriate for many applications. For example, Spring can use AOP to deliver declarative transaction management without using an EJB container; even without a JTA implementation, if you only need to work with a single database. *

Spring Notes

Embed Size (px)

DESCRIPTION

Spring Notes

Citation preview

Page 1: Spring Notes

What is Spring Framework :* The Spring Framework is an open source Lightweight Application Framework for the Java platform.* Spring is a general purpose framework we can develop Console Application, Web Application, Enterprise Application etc.* Spring provides a consistent framework for data access, whether using JDBC or an O/R mapping product such as TopLink, Hibernate or a JDO implementation.

* Year 2002, Rod Johnson release it's first version.* The Spring 1.0 was released March 2004 under the Apache 2.0 license.* The Spring Framework manages the bean creation and lifecycle phases. It is better design to manage the beans and its dependency in the application.* Spring 2.5 or above version support Java 5 Annotations. For Spring 3 at least Jdk 1.5 is required.

Features of Spring :LightWeight, Inversion of Control (IOC), AOP (Aspect Oriented Programming), MVC Framework, Transaction Management.

Features of Spring 3.0 Spring 3.0 framework supports Java 5. It provides annotation based configuration support. Java 5

features such as generics, annotations, varargs etc can be used in Spring. A new expression language Spring Expression Language SpEL is being introduced. The Spring

Expression Language can be used while defining the XML and Annotation based bean definition. Spring 3.0 framework supports REST web services. Data formatting can never be so easy. Spring 3.0 supports annotation based formatting. We can now

use the @DateTimeFormat(iso=ISO.DATE) and @NumberFormat(style=Style.CURRENCY) annotations to convert the date and currency formats.

Spring 3.0 has started support to JPA 2.0.

Advantage of Spring Framework :* Easily integrate with Hibernate, JPA, EJB 3, JSF, Struts etc. * The Spring framework does not limited it self to any specific domain or API. It can be used a Console application, Web application, Enterprise application etc. It means that this is a general purpose framework.* Applications built using Spring are very easy to unit test.* Spring helps us solve many problems without using EJB. Spring can provide an alternative to EJB that's appropriate for many applications. For example, Spring can use AOP to deliver declarative transaction management without using an EJB container; even without a JTA implementation, if you only need to work with a single database.*

Difference between Spring and Struts 1 :* Struts is a web framework only, Struts can be compare with the SpringMVC. And SpringMVC is subset of the Spring framework. So we can say that Sturts can be seen as the subset of the spring framework in functionality point of view.* Spring is a Application Framework while Struts is a Web Framework.* Spring is a Layered architecture but Struts is not.* Spring is Light weight Framework while Struts is Heavy weight Framework.* Spring does not support Tag Library but Struts support Tag Library.* Spring is loosely coupled while Struts is tightly coupled.* Spring provides easy integrated with ORM technologies while in Struts we need to do coding manually.* Struts easily integrate with other client side technologies. It is not easy in case of Spring.* What Action class do in struts, Controller does in Spring. And action in Struts is a Abstract class but Controller in Spring is an interface, This is very good advantage of the spring.

Page 2: Spring Notes

* Spring don’t have any Action from, it bind the http form values directly into pojo.* No ActionForm, binds directly to domain objects. * ActionForward in struts is replace with the ModelAndView in Spring. Model component contain the business object to be displayed via view component.* Spring is a general purpose framework we can develop Console Application, Web Application, Enterprise Application etc but in Struts we can develop only Web Application.

Difference between Spring and Struts 2 :*

Difference between Spring and JSF :*

* Spring is based on two concept : [i] IOC (Inversion Of Control) [ii] AOP (Aspect Oriented Programming).

Inversion of Control (IOC):- * IOC provides the core functionality to the Spring Framework.* IOC provides the facility to create an object, dependency satisfaction and lifecycle management. * The org.springframework.beans and org.springframework.context packages provide the basis for the Spring Framework's IoC container.* The basic concept of the Inversion of Control pattern (dependency injection) is that programmers don’t need to create our objects but describe how they should be created. * In a IOC scenario, the container creates all the objects, connects them together by setting the necessary properties, and determines when methods will be invoked. The implementation pattern types for IOC used by SpringFramework are as follows: # Dependencies can be assigned through JavaBeans properties (setter methods). # Dependencies are provided as constructor parameters and are not exposed as JavaBeans Setter properties.

Following two approach are used to provide the Implementation of IOC.1. Dependency Injection - In case of DI (Dependency Injection), dependency of object are satisfied by IOC container on its own i.e IOC container are not ask to satisfied the dependency of an object.2. Dependency Lookup - In case of DL (Dependency Lookup) ,dependency of an object are satisfied only when IOC container is asked to do so.Note:-The spring framework does not support dependency lookup (DL) only support dependency injection (DI) approach.

Wiring : Wiring is the process of injecting the Bean Dependencies. Wiring can be done in two ways :1. Explicit Wiring - We identify the Bean dependencies and configure the dependencies in the Spring Context xml.2. Implicit Wiring or Auto Wiring. - Spring Container detects the Bean dependencies and injects them automatically.There are possible values of autowire attributes : - byName, byType, Constructor, autodetect.

Page 3: Spring Notes

Aspect Oriented Programming (AOP):-The AOP provides the concept of cross-cutting concern. The cross-cutting concern first one is concern, the concern is a task that perform as part of an operation and second one is cross-cutting, the cross-cutting is a set of concern that participate multiple operation.

AOP provides a new kind of programming which allows us to separate middle-level service code from the core business logic. Without AOP, we need to compile core business logic for our Enterprise application and middle-level service like security, transaction management etc.

Because of mixing, when we modify the middle-level services then we need to update all business services of our application which gives us maintenance problem.

To avoid maintenance problem, AOP provides clean separation between business logic and middle-level services code.

* The Spring Framework is a general purpose framework that provide following modules.   

Modules of Spring Framework 2.5:-----------------------------------------Scope of Spring Framework :-  There are many scope of spring framework which are  Core , AOP , Context , TX , JDBC , ORM , Web MVC , Web , Struts Integration , Enterprise Integration and Testing . Spring all scope which are describe following :-

Core: This module provides basic implementation of IOC container. It provides Dependency injection, Object creation and Lifecycle management. The important basic concept is factory method to control the object creation by the IOC container and application developer using singleton or prototype model.

AOP (Aspect Oriented Programming):-  This modules build over the core and provide the concept of cross-cutting concern. The cross-cutting concern first one is concern , the concern is a task that perform as part of an operation and second one is cross-cutting, the cross-cutting is a set of concern that participate multiple operation.                           

Context:-  This modules build over the core and represent the advance implementation of IOC container.

TX (Transaction Management):- This modules build over the AOP and provide facility to manage transaction in spring.

JDBC:- This modules build over the TX modules and provide template implementation of JDBC.

ORM (Object Relation Mapping):-This modules build over the JDBC that provide the facility of integration spring application to ORM framework such as Hibernate, Toplink etc.

Web:-This module is build over context provide automation of common operation of web application such as transfer of request ,data to domain object ,file uploading ,tiles convention etc.

Web MVC:- This module build over the web and provide an MVC implementation of spring for developing web application.

Page 4: Spring Notes

Struts Integration:- This module build over the web and provide facility to integrate spring application to struts.

Enterprise Integration:-This module provide facility of integration enterprise services such as JNDI, JMS , Java Mail ,Web Services etc. to Spring application.

Testing:-This module over the core and provide the facility of unit an integration of testing.

IOC Concept of Spring Framework------------------------------------------

The spring framework work the concept inversion of control, this concept provides the facility to create an object, satisfied the dependency and lifecycle management. In the case of conventional approach creation of an object, dependency satisfaction and lifecycle management all of these tasks are responsible for application developer.

Problem associated with conventional usage of an object:-

1. An application developer need to focus on object creation, dependency satisfaction and lifecycle management in addition to object usage.

2. Conventional method create tight coupling between object and there user. The tight coupling always create maintenance problem.

3. Conventional approach result in memory wastage because each user creates own object which prevent object reusability.

IOC propose the runtime environment called IOC container that should be used by the application developer to request object. If creation of an object their dependency satisfaction and lifecycle management is manage by IOC container then application developer can focus on used to object. Object can be shared among and loose coupling between object and user can be provided.

Following two approach are used to provide the Implementation of IOC.      A. Dependency Injection     B. Dependency Lookup

Dependency Injection (DI): In case of DI (Dependency Injection), dependency of object are satisfied by IOC container on its own i.e IOC container are not ask to satisfied the dependency of an object.

Dependency Lookup (DL): In case of DL (Dependency Lookup), dependency of an object are satisfied only when IOC container is asked to do so.

Note: The Spring framework does not support Dependency Lookup (DL) only support Dependency Injection (DI) approach.

Terminology of Spring Framework

3.1 .Bean :-Bean definition object represented by the XmlBeanFactory (DefaultListableBeanFactory). the bean manage object creation ,dependency satisfaction through the IOC container which contain following details:-

the <bean> element is used to specify the configuration of a bean to IOC container. the syntax of <bean > element.

<bean id="UniqueIdentifier" name="alias" class="BeanClass" scope="singleton/prototype" factory-method="Name of factory method" factory-bean="Name of factory bean">

Page 5: Spring Notes

3.2 .Bean Tag Element:-There are many types of bean tag element in the spring framework which are following:-

1. id and name:-The bean identify in which type of bean class are requested by the user.

2. class:-The bean class provide the information of requested by user.

3. singleton and prototype:-The bean create object one time in the case of singleton and bean create object requirement of user it means more then one time in the case of proto type.

4. constructor argument:-This configuration provide value in constructor of the bean class if which are defined in bean class. This configuration used in constructor injection.

5. property bean:-This configuration set the value in setter method which are define in the bean class. This configuration used in setter injection.

3.3 .Identify Bean by IOC container:-IOC container usage id, name and class attribute is same sequence to identify a bean.

3.4 .Resource:-Resource is an interface provided by the framework it contain method for managing configuration information. The configuration information represent information required by IOC container for creating ,dependency satisfaction and lifecycle management of bean.

3.5 .BeanFactory:-BeanFactory is an interface provided by spring framework . This interface represent thefunctionality of basic IOC container . Implementation of it is provided by the framework.

Creation of an Object in Spring Framework

5.1 .Introduction :-In the Spring Framework provide the concept IOC container which are responsible for creation of an object, satisfied the dependency and lifecycle management. First task of the IOC container is creation of an objects. Objects can be create through constructor and factory method.

                        By default IOC container create object using constructor. In order to facilitate object creation using factory method. Information of the factory need to be provided.

5.2 .Type of Object Creation :-In the spring framework concept IOC container which are create an object following two type 

          a. Using Constructor                           

          b. Using Factory Method

5.3 .Object Creation Using Constructor :-IOC container create object through the constructor, If  we are creating object through IOC container first we will declare parameterize constructor in our bean class and define method to print any message through the configuration file. The following syntax used in configuration file if we are create object through the constructor   

<bean id="hello" class="org.r4r.Hello"><constructor-arg value="Hello World !"/></bean>

5.4 .Object Creation Using factory method :- IOC container create object through the factory method, If we are creating object through IOC container first we will declare parameterize method in our bean class and print any message through the configuration file. The following syntax used in configuration file if we are create object through the factory method.<bean id="number" class="org.r4r.Complex" factory-method="getNumber" scope="prototype"/>

Page 6: Spring Notes

Factory method can be divided in following three way:-

5.4.1. Same class static factory:-

     class A{

     private(){

      //Logic is written

      }

      public static A getA(){

      return new A();

      }

      }

Following configuration need to provided to the IOC container to use getA() factory method.

<bean id="a" class="A" factory-method="getA"/>

5.4.2. Different class static factory:-

    class B{

     {

      ==

      }

      class A{  //Different class static factory

      public static B getB(){

      return new B();

      }

      }

Following configuration need to provided the IOC container to use getB() factory method.

<bean id="b" class="A" factory-method="getB"/>

5.4.3. Different class non-static factory:-

     class B{

     {

Page 7: Spring Notes

      ==

      }

      class A{  //Different class nonstatic factory

      public B getB(){

      return new B();

      }

      }

Following configuration need to provide the IOC container to use getB() factory method.

<bean id="a" class="A"/>

<bean id="b" class="B" factory-method="getB" factory-bean="a"/>

Scope Attributes of Bean------------------------------

Spring framework provides the facility how many time we creating object through the IOC container. If we want this object is create only one time or more the one time then we give the all information in our configuration file. Through "SCOPE" attribute Spring framework provide this solution. If we are define "SCOPE" attribute in bean tag element then developer choose two option first option if we create object only one time then give "SINGLETON BEAN" in scope attribute and we create object more then one time then give "PROTOTYPE BEAN" in scope attribute.Syntax :

<bean id=”” class=”” scope=””/>

Type of Scope attribute of bean:-There are following types of bean in SCOPE attribute which are following:-       1. Singleton Bean       2. Prototype Bean       3. Request Bean       4. Session Bean       5. global-session Bean

Singleton Bean:-This bean provide the facility to create only one bean if any user request bean then same bean provide more than one user.

Syntax of Singleton bean:-<bean id=" " factory-bean=" " factory-method=" " scope="singleton"></bean>

Prototype Bean:-This bean provide the facility to request more than one bean if any user request bean then different bean provide each request.

Syntax of Prototype bean:-<bean id=" " factory-bean=" " factory-method=" " scope="prototype"></bean>

Request Bean: This is equals to HttpServletRequest in the Web based application.

Syntax of Prototype bean:-

Page 8: Spring Notes

<bean id=" " factory-bean=" " factory-method=" " scope="request"></bean>Session Bean: This is equals to HttpSession in the Web based application.

Syntax of Prototype bean:-<bean id=" " factory-bean=" " factory-method=" " scope="session"></bean>

Global-session: This is equals to Session of portlet based Web Application.

Syntax of Prototype bean:-<bean id=" " factory-bean=" " factory-method=" " scope="global-session"></bean>

Dependency Injection

13.1 .Introduction:-In case of DI (Dependency Injection) ,dependency of object are satisfied by IOC container on its own i.e IOC container are not ask to satisfied the dependency of an object.

13.2 .Approach of Dependency Injection:-There are two approach of Dependency Injection which are following:-

                  1. Constructor Dependency Injection

                  2.Setter Dependency Injection

13.2.1 .Constructor Dependency Injection:-In this approach the IOC container provide the facility to satisfied the dependency of an object in mandatory case and IOC container inject the dependency through the constructor.

13.2.1 .Setter Dependency Injection:- In this approach the IOC container inject the dependency through the default constructor and setter method. This approach is used in the case of optional dependency.

13.3 .Type of Dependency Injection:-There are three type of dependency injection which are following:-

                 1. Dependency on primitive values (String is consider primitive by String).

                 2. Dependency on Object.

                 3. Dependency on Collection.

13.3.1 .Dependency on primitive value:-In the case of constructor injection, constructor-arg is the sub element of <bean ......> is used to specified the dependency of a bean i.e to be satisfied to a constructor this elements uses values and reference attribute which are used to specified value primitive type and object respectively.

Syntax:-

<bean .................>   

<constructor-arg value="primitive or String"/>

<constructor-arg ref="Reference of a bean"/>

................

</bean>

Page 9: Spring Notes

13.3.2 .Dependency on object:-In the case of constructor injection, constructor-arg is the sub element of <bean ......> is used to specify the dependency of an object i.e to be satisfied the constructor-arg element uses reference of an attribute which are used to specify object respectively.

Syntax:-

 <bean .................>   

<constructor-arg >

<list><ref  bean="Reference of a bean"/>

................

</list></constructor-arg>

</bean>

13.3.2 .Dependency on collection:-In the case of constructor injection, constructor-arg is the sub element of <bean ......> is used to specify the dependency of a collection i.e to be satisfied the constructor-arg element uses list ,set and map sub element to specify the dependency of list ,set and map respectively.

Syntax:-

<bean .................>   

<constructor-arg >

<list or set or map>

<Value></value>

................

</list or set or map></constructor-arg>

</bean>

Wiring : Wiring is the process of injecting the Bean Dependencies.Wiring can be done in two ways :1. Explicit Wiring2. Implicit Wiring / Auto Wiring.

Explicit Wiring : When we identify the Bean Dependencies and configure the Dependencies in the Spring context xml file then it is called as Explicit Wiring.Example :class A{}class B{}class Hello{A aobj;B bobj;}

<bean id=”ao” class=”com.spring3.ioc.lab.A”/><bean id=”bo” class=”com.spring3.ioc.lab.B”/><bean id=”hello” class=”com.spring3.ioc.lab.Hello”><property name=”aobj” ref=”ao”/>

Page 10: Spring Notes

<property name=”aobj” ref=”bo”/></bean>

Implicit Wiring / Auto Wiring : In the case of Implicit Wiring / Auto Wiring, Spring Container detects the Bean Dependencies and injects them automatically i.e. we no need to configure the Bean Dependencies in the Spring Context xml (through setter or Constructor injection). Example :class A{}class B{}class Hello{A aobj;B bobj;}

<bean id=”ao” class=”com.spring3.ioc.lab.A”/><bean id=”bo” class=”com.spring3.ioc.lab.B”/><bean id=”hello” class=”com.spring3.ioc.lab.Hello” autowire=”xxxx”/>

Autowire in Spring Framework--------------------------------------

An autowiring is the facility provided by framework to satisfied by dependency of manage bean without configuration information. It means that the autowiring solved problem to write very long configuration information.

22.2 .Type of method to Autowire:- The following four method are used by IOC container to autowire bean dependency.1. byType2. byName3. Constructor4. autodetect

byType : When autowire attribute value is byType then Spring Container checks whether any bean instance running in the Container whose Type (means data type) is same as bean property type or not.

If zero bean is found with the (or for the) matching type then bean property remains uninjected. If exactly one bean is found with the matching type then it will be injected. If two or more beans is found with the matching type then exception will be thrown called

UnsatisfiedDependencyException.

Note:- Facility of autowiring are used only with object type dependency i.e primitive dependency are note autowire.

Syntax:-

<bean id="x" class="org.r4r.A"/><bean id="b" class="org.r4r.B" autowire="byType"/>

Disadvantage of Autowire:-

1. Optional dependency are specified using by type because dependency are injected by setter method.

2. Autowiring by type can not be used if there are more than one beans. Configured for a properties type.

22.2.2 .Autowire by Name:-In this case IOC container uses the name of a bean property to fined out the bean for injecting i.e in this approach the configuration bean to be used for satisfied the dependency of the bean must have the same as the bean property.

Page 11: Spring Notes

Syntax:-<bean id="a" class="org.r4r.A"/><bean id="y" class="org.r4r.A"/><bean id="b" class="org.r4r.B" autowire="byName"/> 

Disadvantage of Autowire:-

1. Only optional dependency can be autowire for this approach.

2. This approach would work of a bean of different type is configure using the same name as the property of a bean which uses autowiring.

22.2.3 .Autowire by Constructor:-In this approach type of parameter of the constructor are used to identified the bean to be injected in case of overloaded constructed has a maximum parameter will choose.

                    This approach is same as autowiring by type accept dependency are injected through constructor in it.

Syntax:-

<bean id="a" class="org.r4r.A"/><bean id="b" class="org.r4r.B" autowire="constructor"/>

22.2.4 .Autowire by Autodetect:-This approach is a combination of autowiring by constructor and autowiring by type in it dependency is injected using the type of property if target bean has a default constructor and dependency are injected using constructor injection if target bean has a parameterize constructor.

Note:-This is the default strategy of the autowiring.

<bean id="x" class="org.r4r.A"/><bean id="b" class="org.r4r.B" autowire="autodetect"/>22.3 .Limitation of Autowiring:-Major draw back of autowiring is understandability is loosed

Lifecycle management of IOC container

27.1 .Introduction:-The IOC container provide the facility of notifying beans about the creation and destruction show that bean can perform initialization or cleanup perforation. It means that the IOC container provide the facility to notifying create of bean and destroy of bean. Initialization and destruction of a bean is notified to it.

                  The IOC container notifying the creation and destruction either of the following two way :-

                                  1. Non-Intrusive approach

                                  2. Intrusive approach

27.2 .Non-Intrusive approach:-A non-intrusive approach an initialization method is defined in the bean and is specified in the bean configuration using init-method attribute.

Syntax:-

<bean id="one" class="org.r4r.One" init-method="init"/>

Page 12: Spring Notes

27.3 .Intrusive approach:-In the intrusive approach initialization bean interface provided by the framework is implemented in the bean class and initializing logic is contain with it after property set method. This method is provided by "InitializingBean" interface.

27.3.1 .InitializingBean:- The InitializingBean interface provide cleanup operation at the time of unloading of a bean either destroy-method attribute of bean is used.

   This interface provide a method name which are following:-

   public void afterPropertiesSet();

Syntax:-

<bean id="one" class="org.r4r.One" destroy-method="init"/> 

27.3.2 .DisposableBean:- This bean interface is provided by framework is implemented. This interface provide a method name destroy() if is invoked by IOC container before unload by IOC container before unloaded bean.

           This interface provide single method name destroy().

     public void destroy();                 

Method Injection in Spring Framework

31.1 .Introduction:-Spring 1.1 introducing method injection for a new IOC-Oriented feature with allows greater flexibility for interactions between collaborators.

31.2 .Type of Method Injection:-

       The spring framework method injection provide this facility in to two way:-

              1.Lookup Method Injection

              2.Method replacement

31.3 .Lookup Method Injection:-The lookup method injection provide the facility to implement in our application one bean is depends on another bean with a different life cycle. It means that singleton bean depends on non-singleton bean. In this situation setter and constructor injection create instance of singleton bean not a non-singleton bean. In some case application require singleton bean obtain new instance of the non-singleton every time it requires the bean.

           If we achieve this implementation in our application the implement BeanFactoryAware interface in our singleton bean. Then using BeanFactory instance, the singleton bean can lookup a new instance of a non-singleton dependency every time it needs it.

           The Lookup Method Injection allows the singleton bean to declare that it requires a non-singleton dependency and receive a new instance of the non-singleton bean each time it needs to interact with it, without needing to implement any spring-specific interfaces. 

31.4 .Method Replacement:-This Method injection provide a facility to replace the implementation of any method on a bean arbitrary, without changing our source code.

Note:-If we are use lookup method injection and method replacement in our application, the add the CGLIB jar file in our application.

Page 13: Spring Notes

 

AOP (Aspect Oriented Programming)

33.1.Introduction:-An Aspect Oriented Programming (AOP) is a programming model that compliments object oriented programming model and provides an efficient mechanism of implementing behaviors which have crosscutting concerns.

33.2.Terminology of AOP:- There are following terminology of AOP (Aspect Oriented Programming) in spring framework:-

1.Concerns:-A concerns represent a task i.e performed as part of an operation. A concerns is set to be crosscutting concerns is it participate of multiple operation.

2.Advice:-Implementation of a concerns is called Advice.

3.Joinpoint:- Joinpoint represent easily identifiable intersection point in the execution of an application where Advices can be inserted. Loading of classes, creation of an object, invocation of method etc. can be used as joinpoint. A spring AOP implementation only support method invocation joinpoint.

5.Pointcut:- A pointcut is a collection of Joinpoint.

6.Aspect:- An Aspect is a collection of Pointcut and Advices.

33.3.What is crosscutting concern in Spring AOP:-

A concern represent a task that is performed as part of an operation. A concern is set to be crosscutting is it participate of multiple operation.

       Concerns can be divided in to multiple category such as:-

                1.Implementation Concern

                2.Business Concern

                3.Organization Concern

                  etc.

1.Implementation Concern:- Implementation Concerns represents those task which are required before implementing for example:-Validation, Authorization, etc

2.Business Concern:- Business concern represent actual logic or main task of an operation.

3.Organization Concern:- Organization concern represent additional services, facility and requirement associated an operation by an organization.

33.4.What is Weaving in Spring AOP:-

The process of inserting advices is called weaving. Different AOP framework supports either weaving types:-

                1.Compilation time weaving

                2.Runtime weaving

Page 14: Spring Notes

1.Compilation time Weaving:- In Compilation time weaving advices are inserted at the time of Compilation at the special AOP compiler.

Advantage:- The major advantage of this approach are performance.

Disadvantage:- Special AOP compiler are required and application need to be recompile.

2.Runtime Weaving:- In this approach advices are dynamically inserted which the help of proxy object.

Advantage:- Special AOP compiler are not required and Application are not required to be modified or recompile each time . Number of concerns or order changed.

Disadvantage:- Performance are degrade because of additional overhead are proxy.

Note:-A Spring AOP support Runtime weaving.

Proxy in Spring Framework

34.1.Introduction:-A proxy is an Object that represents the functionality of an other Object. Proxy are used an abstraction of complex services expose by the Object to another Object in AOP proxy are use to add advices before or after method invocation.

34.2.Type of Proxy in Spring Framework:-

          There are two type of Proxy Object in the Spring Framework:-

                  1.Static Proxy

                  2.Dynamic Proxy

34.3.Static Proxy:-Static Proxy are created by the developer at the time of development. Static Proxy develop in two way first through the developer and second through the used of any tools for example RMI compiler etc.

34.4.Dynamic Proxy:-Dynamic proxy are created at runtime. It is provided more flexibility.

34.5.Type of Dynamic Proxy:-There are two types of Dynamic Proxy in Spring Framework:-

                  1.JDK Dynamic Proxy

                  2.CGLIB Dynamic Proxy

34.6.JDK Dynamic Proxy:-JDK Dynamic Proxy is the facility provided by Sun micro System. In Reflection API to create dynamic proxy can not be created of those classes which implements some interface that is JDK dynamic proxy to implemented those method which are described by this interface implemented by target class.

             A JDK Dynamic proxy represent in instance dynamic proxy class which extends java.leng.reflect proxy class implemented by the target class. JDK dynamic proxy uses on Object.

34.7.CGLIB Dynamic Proxy:-CGLIB dynamic proxy is proxy of all the method of class perspective of the interface are implemented by the class.

Advices in Spring Framework

Page 15: Spring Notes

37.1.Introduction:-Implementation of a concerns is called Advice. It means that concerns represent a task i.e performed as part of an operation. Any operation are implemented through the concern is called Advices.

37.2.Type of Advices in Spring Framework:-There are four type of advices in spring framework which are following

                       1.Before Advice

                       2.After Advice

                       3.Around Advice

                       4.After Throws Advice

37.3.Before Advice:-This Spring Before advice provide the service before invoking the method. It means that Before advices represent those concern which are to be applied before a method is invoked. Which are applied in Validation, Authorization, etc. are the task which can be implemented using before advice.

Framework provide an interface named "MethodBeforeAdvice" this interface provide a method that is used by application developer to define the task that are to be executed before the invocation target method.

37.4.After Advice:-After advice represents those task which are to be performed after the completion of method. A Spring Framework implements After Advice are invoked from the implementation of AfterReturningAdvice interface.

                This interface provide a method name AfterReturning which is invoked InvocationHandler after the actual method completed. Through this method reference of target method, argument, method object and value returned by the Object are provided to the developer. Application developer may used the return value but can not change it.

37.5.Around Advice:-An around advice are used when some operations are to be performed before and after method call and controlled over the return value of the method is to be gain.

                To apply around advice implementation of "MethodInterceptor" interface need to be provided by the developer. This interface provide a method name invoke which received parameter of type of Invocation. MethodInvocation Object encapsulate invocation of the actual method of the target object and exposes the method name proceed() which is used by application developer to get the actual method invoked.

38.6.After Throws Advice:-An after throws advice is used to performed some operation between throwing and catching an exception.

                To implements after throws advice implementation of "ThrowsAdvice" interface need to be provided. It is the marker interface. The class which implements this interface can define a method name afterThrowing() which can have either of the following signature.

         public void afterThrowing(Exception e);

         public void afterThrowing(Methid m, Object[] arg, Object target);