# Java Spring

## Basic

————————————————————————————————————————————————————————————**Bean**

The objects that form the backbone of your application and that are managed by the Spring IoC container are called beans.

————————————————————————————————————————————————————————————**Bean Scope**

It's used to decide which type of bean instance should be returned from Spring container back to the user.

1. Singleton :It returns a single bean instance per Spring IoC container.This single instance is stored in a cache of such singleton beans, and all subsequent requests and references for that named bean return the cached object.If no bean scope is specified in bean configuration file, default to singleton.
2. Prototype: It returns a new bean instance each time when requested. It does not store any cache version like singleton.
3. Request: It returns a single bean instance per HTTP request.
4. Session: It returns a single bean instance per HTTP session (User level session).
5. GlobalSession: It returns a single bean instance per global HTTP session. It is only valid in the context of a web-aware Spring ApplicationContext (Application level session).

————————————————————————————————————————————————————————————**Spring Transaction**

Basically, transaction means "all-or-nothing". So a sequence of actions can be treated as a single unit of work. And these actions should either complete entirely or take no effect at all. Spring framework provides an abstract layer on top of different underlying transaction management APIs. Spring's transaction support aims to provide an alternative to EJB transactions by adding transaction capabilities to POJOs. Spring supports both programmatic and declarative transaction management. EJBs require an application server, but Spring transaction management can be implemented without the need of an application server.

————————————————————————————————————————————————————————————**Autowire**

It enables you to inject the object dependency implicitly.

|   | Attribute   | Meaning                                                                                                                                                                 |
| - | ----------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| 1 | no          | It is the default autowiring mode. It means no autowiring bydefault.                                                                                                    |
| 2 | byName      | The byName mode injects the object dependency according to name of the bean. In such case, property name and bean name must be same. It internally calls setter method. |
| 3 | byType      | The byType mode injects the object dependency according to type. So property name and bean name can be different. It internally calls setter method.                    |
| 4 | constructor | The constructor mode injects the dependency by calling the constructor of the class. It calls the constructor having large number of parameters.                        |
| 5 | autodetect  | It is deprecated since Spring 3.                                                                                                                                        |

————————————————————————————————————————————————————————————**IoC (Inversion of Control)**

The full name is Inversion of Control. It's a principle by which the control of objects is transferred to the container or framework.

Advantages:

* Decoupled the execution of a task from its implementation Make it easier to switch between different implementations Greater modularity a program Greater ease in testing a program by isolating a component or mocking its dependencies and allowing components to communicate through contracts

————————————————————————————————————————————————————————————**DI (Dependency Injection)**

The full name is Dependency Injection. It's a design pattern that implements IoC for resolving dependencies.

————————————————————————————————————————————————————————————
