Spring Framework
1.1 What?
- An application framework and inversion of control container for the Java
platform
- Open-source
- Provides infrastructure support for developing Java applications
- One of the most popular Java Enterprise Edition (Java EE) frameworks
- Spring helps developers create high performing applications using plain old Java objects (POJOs)
- Spring MVC is a part of the Spring framework that helps in handling HTTP requests and responses
- On the other hand, Spring Boot is the extension of the Spring framework and provides a faster way to
build applications.
1.2 Dependency Injection - (Inversion of Control)
- Dependency Injection (DI) is a design pattern that removes the dependency from the programming code so
that it can be easy to manage and test the application.
- Dependency Injection makes our programming code loosely coupled
- Spring IoC (Inversion of Control) Container is the core of Spring Framework. It creates the objects,
configures and assembles their dependencies, manages their entire life cycle. The Container uses
Dependency Injection(DI) to manage the components that make up the application.
- How to Perform Dependency Injection in Spring?
- XML based Configuration (Beans)
- Java Annotation-based Configurations (Autowiring)
- Different ways of Dependency Injection
- Constructor based Dependency Injection (@Component , @Autowired - is used on top of the class
constructor)
@Component
class A {}
class B {
private A obj;
@Autowired
public B(A obj) {
this.obj = obj;
}
}
Setter based Dependency Injection (@Component , @Autowired is used on top of the class's setter
method)
@Component
class A {}
class B {
private A obj;
@Autowired
public setB(A obj) {
this.obj = obj;
}
}
Field or Property-based Dependency Injection (@Autowired is used on top of the field or property)
@Component
class A {}
class B {
@Autowired
private A obj;
}
2.1 JDBC - Spring JDBC - JPA - Spring Data JPA
- JDBC
- Write a lot of SQL queries
- Write a lot of Java code
- Spring JDBC
- write a lot of SQL queries
- But lesser Java code
- JPA
- Do not worry about queries
- Just map entities to tables
- Spring Data JPA
- Makes JPA even more simple
- It will take of everything