- Open source java framework
- We can develop standalone and enterprise apps
Advantages of spring
- Modular and lightweight
- Flexible configuration
- Dependency injection
- Aspect oriented programming
- Simplified database access
- Testing support
- Security
- Integration capabilities
- Scalability
- Open source
Spring container - Core component (heart) - IoC container
Responsibilities
- Manage bean objects (creation, initialization, destruction)
- Manage bean life cycle
- Dependency Injection
- AOP
- Transaction management
- I18N (Internationalization)
- Integration etc.
Types
- BeanFactory (old)
- ApplicationContext (new) (interface) (invokes spring container)
flowchart TD POJO/JavaBean --read--> SC[Spring Container] Configurations --read--> SC SC --- AC[Access Objects]
JavaBean class
It’s a convention. It is a regular java class, except it follows some conventions -
- All properties are private (use getters/setters)
- A public no argument constructor
- Implements serializable Spring beans are singleton by default
ApplicationContext (interface)
The ApplicationContext (spring container) is an interface in spring which is used to manage beans, handle application events, and access resources. Some implemented classes are -
- ClassPathXmlApplicationContext (Used for XML config)
- AnnoationConfigApplicationContext (Used for java config)
Bean life cycle
flowchart TD XML/Annotations --- 1[a. Loading bean definitions] 1 --- 2[b. Bean instantiation - Bean object is created] 2 --- 3[c. Bean initialization - Bean object is initialized] 3 --- 4[d. Bean destruction - Bean object is destroyed or deleted after use] 2a[No argument or default constructor called - No values set] --- 2 3a[Property tag, setter and @Value called] --- 3 4a[destroy method is called] --- 4
- Loading Bean definitions - Spring loads bean definitions from various sources, such as XML configuration files, java based configuration classes or component scanning These bean definitions contain information about the bean class, dependencies and other configuration details.
- Bean instantiation - After bean definitions are loaded, the spring container creates instances of beans based on these definitions. This involves invoking the bean class’s constructor to create an actual instance of the bean.
- Bean Post processor - An interface that enables custom logic to be executed before and after the initialization of spring beans.
- Bean initialization - Once the bean is instantiated, spring proceeds to configure and initialize it. Property values are set using setters, constructor args, or field injection.
Dependency Injection
- It’s a design pattern to achieve Inversion of Control (IoC)
- It’s main task is to “inject” the “dependency” - inject one object into another object.
- It is used to achieve “loose coupling”
- We can achieve DI by 2 ways -
- Setter method DI
- Constructor DI
Instead of creating the object inside another object (hardcoding the dependency), we can insert it from outside using the constructor or setter methods. This way, we can decide to extend or switch the object using inherited or implemented classes.
Annotation @Autowired is used to achieve dependency injection.
- Setter method DI
@Component
class Car{
@Autowired
private Engine engine;
}
@Component
class Engine{
}- Constructor method DI
@Component
class Car{
private Engine engine;
@Autowired
public car(Engine engine){
this.engine=engine;
}
}
@Component
class Engine{
}It is a good practice to use @Autowired on constructor of the object class because it
- Promotes immutability
- Easier to test
- Guarantees dependency availability
The @Qualifier and @Primary annotation in spring helps pick the right bean among multiple beans of the same type. It helps spring to know which bean you want injected, resolving ambiguity.