Spring is a lightweight application development framework, it is used to develop java and web based applications. It was developed by Rod Johnson in 2003.
It provides integration support with various other frameworks like hibernate, struts, jsf, etc.
Spring framework is based on two design principles – IOC(DI) and AOP.
1.Where DI is the dependency Injection &
2.AOP is the aspect oriented programming.
Advantages of Spring Framework :
- Loose Coupling – the beans(classes) in the spring applications are loosely coupled because of dependency injection.
- Provided Templates – Spring framework provides templates for various other frameworks to integrate, for example : hibernate template, jdbc template, etc.
- Ease in Test – the concept of dependency injection makes it easier to test the application.
- Other Declarative Support – It provides declarative support for transaction management.
Why not talk about Beans first ? before we move on ..
Beans are the objects which form the backbone of the application that are managed by the IOC containers in spring. Beans are created with the help of configuration metadata which we provide to spring in the form of xml configurations or annotations these days. With the help of this config metadata , containers get to know about the following :
- How to create a bean
- Bean’s lifecycle
- And its dependencies
Bean Scope
Spring Framework supports the following five scopes:
| S.No | Scope | Description |
|---|---|---|
| 1 | singleton | This scope restricts the IOC container to create and manage only one instance per container, i.e one object instance of a bean per container. |
| 2 | prototype | This scopes a single bean definition to have any number of object instances per container. |
| 3 | request | Only valid in the context of a web-aware Spring ApplicationContext, this scopes a bean definition to an http request. |
| 4 | session | Only valid in the context of a web-aware Spring ApplicationContext, this scopes a bean definition to an http session. |
| 5 | global-session | Only valid in the context of a web-aware Spring ApplicationContext, this scopes a bean definition to a global http session. |
IOC – Inversion of Control in spring
It is a design pattern when help spring manage the dependencies on its own rather than user creating objects everytime using new keyword. The IoC container is responsible to instantiate, configure and assemble the objects. This design pattern is implemented using a technique called Dependency Injection
Spring provides two types of containers :
1. BeanFactory
2. ApplicationContext
| S.No | BeanFactory | ApplicationContext |
|---|---|---|
| 1 | It provides basic support for Dependency Injection | It provides some additional functionalities like resolve textual messages from properties file, has simple integration with Spring AOP, event propagation, etc.. |
| 2 | Interface : org.springframework.beans.factory.BeanFactory | Interface : org.springframework.context.ApplicationContext |
| 3 | Though Bean Factory has less features compared to the ApplicationContext but is lightweight which makes it useful for small applications which are mobile based or in applets. | It has an advantage over BeanFactory and hence also advisable to use. |
Autowiring in Spring & its 5 types
Dependency injection is also achieved using autowire feature which enables you to inject the objec dependency implicitly. Internally it uses setter or constructor injection but its doesn’t work with injecting primitive and string values, works only for reference.
Advantages of autowiring:
Less code to write as compared to manually using CI and SI(explicit way of doing injection)
Drawbacks of autowiring : Disadvantages
works only for reference and not for primitive and string values
Ways of Autowiring
| S.No | Mode | Description |
|---|---|---|
| 1 | no | It is default autowiring mode. It means no autowiring by default. |
| 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. |
Summary
In this tutorial we learnt about Spring Inversion of Control, Autowiring, and the Bean scopes.
Hope you liked it !