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
- Core Container:-
- The Core module provides the fundamental parts of the framework, including the IoC and Dependency Injection features.
- The Bean module provides BeanFactory which is a sophisticated implementation of the factory pattern.
- 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.
- The Expression Language module provides a powerful expression language for querying and manipulating an object graph at runtime.
- Data Access/Integration:-
- The JDBC module provides a JDBC-abstraction layer that removes the need to do tedious JDBC related coding.
- The ORM module provides integration layers for popular object-relational mapping APIs, including JPA, JDO, Hibernate, and iBatis.
- The OXM module provides an abstraction layer that supports Object/XML mapping implementations for JAXB, Castor, XMLBeans, JiBX and XStream.
- The Java Messaging Service JMS module contains features for producing and consuming messages.
- The Transaction module supports programmatic and declarative transaction management for classes that implement special interfaces and for all your POJOs.
- Web:-
- 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.
- The Web-Servlet module contains Spring's model-view-controller (MVC) implementation for web applications.
- The Web-Struts module contains the support classes for integrating a classic Struts web tier within a Spring application.
- The Web-Portlet module provides the MVC implementation to be used in a portlet environment and mirrors the functionality of Web-Servlet module.
- Miscellaneous:-
- 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.
- The Aspects module provides integration with AspectJ which is again a powerful and mature aspect oriented programming (AOP) framework.
- The Instrumentation module provides class instrumentation support and class loader implementations to be used in certain application servers.
- The Test module supports the testing of Spring components with JUnit or TestNG frameworks.
- Spring BeanFactory Container
- Spring ApplicationContext Container
Spring Configuration:-
There are following three important methods to provide configuration metadata to the Spring Container:
- XML based configuration file.
- Annotation-based configuration
- Java-based configuration
<?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>
- DEFAULT_INIT_METHOD:- It is used to call the init method in case all the beans have init method of same name.
- DEFAULT_DESTROY_METHOD:-It is used to call the destroy method in case all the beans have destroy method of same name.
- BEAN_NAME:- It is the unique name used to identify the bean.
- CLASS_NAME; It is the POJO class that will be used to instantiate the bean variables and methods.
- SCOPE:- It is used to define the scope of the bean. The valid values are:-
- singleton:- This scopes the bean definition to a single instance per Spring IoC container (default).
- 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.
- request:- This scopes a bean definition to an HTTP request. Only valid in the context of a web-aware Spring ApplicationContext.
- session:- This scopes a bean definition to an HTTP session. Only valid in the context of a web-aware Spring ApplicationContext.
- global-session:- This scopes a bean definition to a global HTTP session. Only valid in the context of a web-aware Spring ApplicationContext.
- METHOD_AT_BEAN_INITIALIZATION:- The name of method to be called during bean initialization.
- 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).
- PARENT_BEAN_ID:- It is used to inherit the properties from some other bean.
- 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.
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:-
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.
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
- Via constructor
- Via setter methods
- Very loose coupling between objects.
- POJO based so not forced in any way to implement unwanted methods.
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
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