SPRING

Spring was developed by Rod Johnson in 2003.
 
Spring framework makes the easy development of JavaEE application.

Spring is a lightweight framework.

Spring can be thought of as a framework of frameworks because it provides support to various frameworks such as Struts, Hibernate, Tapestry, EJB, JSF, etc.

The Spring framework comprises several modules such as IOC, AOP, DAO, Context, ORM, WEB MVC etc.

Inversion Of Control (IOC) and Dependency Injection
:
These are the design patterns that are used to remove dependency from the programming code.
They make the code easier to test and maintain.

Dependency Injection (DI) is a design pattern that removes the dependency from the programming code.

Dependency Injection makes our programming code loosely coupled.

Spring framework provides two ways to inject dependency
1) By Constructor
2) By Setter method

IOC makes the code loosely coupled.

In Spring framework, IOC container is responsible to inject the dependency.

There are two types of IoC containers. They are:
1) BeanFactory
2) ApplicationContext

The org.springframework.beans.factory.BeanFactory and the org.springframework.context.ApplicationContext interfaces acts as the IoC container. 

The ApplicationContext interface is built on top of the BeanFactory interface. 
It adds some extra functionality than BeanFactory such as simple integration with Spring's AOP, message resource handling (for I18N), event propagation, application layer specific context (e.g. WebApplicationContext) for web application. 

ApplicationContext is better to use than BeanFactory.

Spring Security is a framework which provides various security features like: authentication, authorization to create secure Java Enterprise Applications.

A sub-project of Spring framework which was started in 2003 by Ben Alex. Later on, in 2004, It was released under the Apache License as Spring Security 2.0.0.

This framework targets two major areas of application are authentication and authorization.

Authentication is the process of knowing and identifying the user that wants to access.

Authorization is the process to allow authority to perform actions in the application.


Difference between @RequestParam and @PathVaraible in Spring MVC?
@RequestParam and @PathVariable can both be used to extract values from the request URI.
@RequestParams extract values from the query string.
@GetMapping("/foos")
@ResponseBody
public String getFooByIdUsingQueryParam(@RequestParam String id) {
    return "ID: " + id;
}
http://localhost:8080/spring-mvc-basics/foos?id=abc
----
ID: abc

Mapping All Parameters
@PostMapping("/api/foos")
@ResponseBody
public String updateFoos(@RequestParam Map<String,String> allParams) {
return "Parameters are " + allParams.entrySet();
}
curl -X POST -F 'name=abc' -F 'id=123' http://localhost:8080/spring-mvc-basics/api/foos
-----
Parameters are {[name=abc], [id=123]}

Mapping a Multi-Value Parameter
@GetMapping("/api/foos")
@ResponseBody
public String getFoos(@RequestParam List<String> id) {
return "IDs are " + id;
}
http://localhost:8080/spring-mvc-basics/api/foos?id=1,2,3
----
IDs are [1,2,3]

A Default Value for the Request Parameter
@GetMapping("/api/foos")
@ResponseBodypublic String getFoos(@RequestParam(defaultValue = "test") String id) {
return "ID: " + id;
}
http://localhost:8080/spring-mvc-basics/api/foos
----
ID: test

 

@PathVariables extract values from the URI path.
@GetMapping("/foos/{id}")
@ResponseBody
public String getFooById(@PathVariable String id) {
    return "ID: " + id;
}
http://localhost:8080/spring-mvc-basics/foos/abc
----
ID: abc


Spring Bean Life Cycle

Bean life cycle is managed by the spring container.
Initialization callbacks
void afterPropertiesSet() throws Exception;
public void init() {}
public class ExampleBean implements InitializingBean {
   public void afterPropertiesSet() {
      // do some initialization work
   }
}
Destruction callbacks
void destroy() throws Exception;
public class ExampleBean implements DisposableBean {
   public void destroy() {
      // do some destruction work
   }
}

Spring Bean Scopes
1) singleton - This scopes the bean definition to a single instance per Spring IoC container (default). Stateless.  The same object will be shared for each request made for that bean.
2) prototype - This scopes a single bean definition to have any number of object instances. Statefull.
A new instance will be created for a single bean definition every time a request is made for that bean.
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.


Spring - Transaction Management
A database transaction is a sequence of actions that are treated as a single unit of work.
These actions should either complete entirely or take no effect at all.
The concept of transactions can be described with the following ACID
Atomicity − A transaction should be treated as a single unit of operation, which means either the entire sequence of operations is successful or unsuccessful.
Consistency − This represents the consistency of the referential integrity of the database, unique primary keys in tables, etc.
Isolation − There may be many transaction processing with the same data set at the same time. Each transaction should be isolated from others to prevent data corruption.
Durability − Once a transaction has completed, the results of this transaction have to be made permanent and cannot be erased from the database due to system failure.

Spring supports two types of transaction management −
Programmatic transaction management − This means that you have to manage the transaction with the help of programming. That gives you extreme flexibility, but it is difficult to maintain.
Declarative transaction management − This means you separate transaction management from the business code. You only use annotations or XML-based configuration to manage the transactions. Spring supports declarative transaction management through the Spring AOP framework.

Isolation & Description
1) TransactionDefinition.ISOLATION_DEFAULT
This is the default isolation level.
2) TransactionDefinition.ISOLATION_READ_COMMITTED
Indicates that dirty reads are prevented; non-repeatable reads and phantom reads can occur.
3) TransactionDefinition.ISOLATION_READ_UNCOMMITTED
Indicates that dirty reads, non-repeatable reads, and phantom reads can occur.
4) TransactionDefinition.ISOLATION_REPEATABLE_READ
Indicates that dirty reads and non-repeatable reads are prevented; phantom reads can occur.
5) TransactionDefinition.ISOLATION_SERIALIZABLE
Indicates that dirty reads, non-repeatable reads, and phantom reads are prevented.


Propagation & Description
1) TransactionDefinition.PROPAGATION_MANDATORY
Supports a current transaction; throws an exception if no current transaction exists.
2) TransactionDefinition.PROPAGATION_NESTED
Executes within a nested transaction if a current transaction exists.
3) TransactionDefinition.PROPAGATION_NEVER
Does not support a current transaction; throws an exception if a current transaction exists.
4) TransactionDefinition.PROPAGATION_NOT_SUPPORTED
Does not support a current transaction; rather always execute nontransactionally.
5) TransactionDefinition.PROPAGATION_REQUIRED
Supports a current transaction; creates a new one if none exists.
6) TransactionDefinition.PROPAGATION_REQUIRES_NEW
Creates a new transaction, suspending the current transaction if one exists.
7) TransactionDefinition.PROPAGATION_SUPPORTS
Supports a current transaction; executes non-transactionally if none exists.
8) TransactionDefinition.TIMEOUT_DEFAULT
Uses the default timeout of the underlying transaction system, or none if timeouts are not supported.

Comments

Popular posts from this blog

PL/SQL

JAVA8 Features

Build Automation