Tuesday, September 10, 2013

Springs

Dependency Injection :- Adding dependency at run time using setters or constructors using IOC

OOP :- Unit of modularity is CLASS

AOP :- Unit of modularity is ASPECT

ASPECTS and CROSS CUTTING CONCERNS:- These are the functions that are used at different points in an application like logging, declarative transactions, security, and caching etc.

Dependency Injection :- It is used to decouple an application objects from each other.

AOP :- It is used to decouple aspects from the objects they are affecting.

Springs Framework:- It consists of around 20 modules
  1. Core Container:-
    1. The Core module provides the fundamental parts of the framework, including the IoC and Dependency Injection features.
    2. The Bean module provides BeanFactory which is a sophisticated implementation of the factory pattern.
    3. The Context module builds on the solid base provided by the Core and Beans modules and it is a medium to access any objects defined and configured. The ApplicationContext interface is the focal point of the Context module.
    4. The Expression Language module provides a powerful expression language for querying and manipulating an object graph at runtime.
  2.  Data Access/Integration:-
    1. The JDBC module provides a JDBC-abstraction layer that removes the need to do tedious JDBC related coding.
    2. The ORM module provides integration layers for popular object-relational mapping APIs, including JPA, JDO, Hibernate, and iBatis.
    3. The OXM module provides an abstraction layer that supports Object/XML mapping implementations for JAXB, Castor, XMLBeans, JiBX and XStream.
    4. The Java Messaging Service JMS module contains features for producing and consuming messages.
    5. The Transaction module supports programmatic and declarative transaction management for classes that implement special interfaces and for all your POJOs. 
  3. Web:-
    1.  The Web module provides basic web-oriented integration features such as multipart file-upload functionality and the initialization of the IoC container using servlet listeners and a web-oriented application context.
    2. The Web-Servlet module contains Spring's model-view-controller (MVC) implementation for web applications.
    3. The Web-Struts module contains the support classes for integrating a classic Struts web tier within a Spring application.
    4. The Web-Portlet module provides the MVC implementation to be used in a portlet environment and mirrors the functionality of Web-Servlet module.
  4. Miscellaneous:-
    1. The AOP module provides aspect-oriented programming implementation allowing you to define method-interceptors and pointcuts to cleanly decouple code that implements functionality that should be separated.
    2.  The Aspects module provides integration with AspectJ which is again a powerful and mature aspect oriented programming (AOP) framework.
    3. The Instrumentation module provides class instrumentation support and class loader implementations to be used in certain application servers.
    4. The Test module supports the testing of Spring components with JUnit or TestNG frameworks.
Spring provides following two distinct types of containers.
  1. Spring BeanFactory Container
  2.  Spring ApplicationContext Container
The ApplicationContext container includes all functionality of the BeanFactory container, so it is generally recommended over the BeanFactory.  

Spring Configuration:-
There are following three important methods to provide configuration metadata to the Spring Container:
  1. XML based configuration file.
  2. Annotation-based configuration
  3. Java-based configuration 
XML based configuration file:-

<?xml version="1.0" encoding="UTF-8"?>
<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-3.0.xsd" default-init-method="DEFAULT_INIT_METHOD" default-destroy-method="DEFAULT_DESTROY_METHOD">
<!-- A simple bean definition -->
    <bean id="BEAN_NAME" class="CLASS_NAME" scope="SCOPE" init-method = "METHOD_AT_BEAN_INITIALIZATION" destroy-method = "METHOD_AT_BEAN_DESTRUCTION" parent="PARENT_BEAN_ID" abstract = "ABSTRACT_TRUE_FALSE"
>
<constructor-arg ref="ARGUMENT_FIRST"/>
<constructor-arg ref="ARGUMENT_SECOND"/>
       <!-- collaborators and configuration for this bean go here -->
    </bean>
</bean>


  1. DEFAULT_INIT_METHOD:- It is used to call the init method in case all the beans have init method of same name.
  2. DEFAULT_DESTROY_METHOD:-It is used to call the destroy method in case all the beans have destroy method of same name.
  3. BEAN_NAME:- It is the unique name used to identify the bean.
  4. CLASS_NAME; It is the POJO class that will be used to instantiate the bean variables and methods.
  5. SCOPE:- It is used to define the scope of the bean. The valid values are:-
    1. singleton:- This scopes the bean definition to a single instance per Spring IoC container (default).
    2. prototype:- To force Spring to produce a new bean instance each time one is needed, you should declare the bean's scope attribute to be prototype.
    3. request:- This scopes a bean definition to an HTTP request. Only valid in the context of a web-aware Spring ApplicationContext.
    4. session:- This scopes a bean definition to an HTTP session. Only valid in the context of a web-aware Spring ApplicationContext.
    5. global-session:- This scopes a bean definition to a global HTTP session. Only valid in the context of a web-aware Spring ApplicationContext.
  6. METHOD_AT_BEAN_INITIALIZATION:- The name of method to be called during bean initialization.
  7. METHOD_AT_BEAN_DESTRUCTION:- The name of method to be called during bean destruction. Destroy method is not called for beans of scope prototype. This is because the context doesn't keep track of the prototype scope objects (if it does, it will cause a memory leak as spring doesn't know when to dispose it). 
  8. PARENT_BEAN_ID:- It is used to inherit the properties from some other bean.
  9. ABSTRACT_TRUE_FALSE:- abstract can be set to true if we want to create a bean definition template for bean inheritance explained below. It is false by default. The class attribute should not be given in case abstract is set to true i.e. if bean template is being created. 
BeanPostProcessor:- Its is an interface that is used to provide own instantiation logic before or after bean initialization. The class is mapped in config file.

Bean Definition Inheritance:-We can define a bean in config file and this bean definition can be inherited by other beans as shown below.

Dependency Injection:-
Consider you have an application which has a text editor component and you want to provide spell checking. Your standard code would look something like this:
 
public class TextEditor {
   private SpellChecker spellChecker;
   public TextEditor() {
      spellChecker = new SpellChecker();
   }
}
 
What we've done here is create a dependency between the TextEditor and the SpellChecker.

Now consider the code as 
 
public class TextEditor {
   private SpellChecker spellChecker;
   public TextEditor(SpellChecker spellChecker) {
      this.spellChecker = spellChecker;
   }
}
 
Here TextEditor should not worry about SpellChecker implementation. The SpellChecker will be implemented independently and will be provided to TextEditor at the time of TextEditor instantiation and this entire procedure is controlled by the Spring Framework.
Here, we have removed the total control from TextEditor and kept it somewhere else (ie. XML configuration file) and the dependency ( ie. class SpellChecker) is being injected into the class TextEditor through a Class Constructor. Thus flow of control has been "inverted" by Dependency Injection (DI) because you have effectively delegated dependances to some external system.

Second method of injecting dependency is through Setter Methods of TextEditor class where we will create SpellChecker instance and this instance will be used to call setter methods to initialize TextEditor's properties.

So there are two ways in which Dependency can be injected
  1. Via constructor
    1.  
  2. Via setter methods
Benefits of using Springs framework:-
  1.  Very loose coupling between objects.
  2. POJO based so not forced in any way to implement unwanted methods.
  3.  

Minimum Jars required for Struts, Springs, Hibernate to work :-
Controller org.springframework.context
RequestMapping org.springframework.web
RequestMethod org.springframework.web
ModelMap org.springframework.context
DispatcherServlet                                                                org.springframework.web.servlet
BeansException                                                                  org.springframework.beans
NestedRuntimeException                                                    org.springframework.core
LogFactory                                                                        commons-logging
ClassVisitor                                                                       org.springframework.asm
PropertyAccessor                                                                org.springframework.expression

javax.servlet.jsp.jstl.core.Config                     javax.servlet.jsp.jstl
org.apache.commons.dbcp.BasicDataSource                          commons-dbcp
org.apache.commons.pool.impl.GenericObjectPool         commons-pool.jar
JdbcTemplate                                           org.springframework.jdbc
org.springframework.dao.DataAccessException            springs-dao.jar


No comments:

Post a Comment

SpringBoot Application Event Listeners

When a spring boot application starts few events occurs in below order ApplicationStartingEvent ApplicationEnvironmentPreparedEvent Applicat...