Spring to Spring Boot: A Comprehensive
Guide
1 Introduction
Spring Boot is a modern enhancement of the traditional Spring Framework, de-
signed to simplify and accelerate Java application development. While Spring
requires manual configuration through XML or Java config, Spring Boot provides
default configurations and auto-setup mechanisms to eliminate boilerplate code.
In traditional Spring, developers needed to:
• Manually create applicationContext.xml or annotate configuration classes.
• Set up web.xml for servlet mapping.
• Add each dependency individually.
In contrast, Spring Boot simplifies this by:
• Using @SpringBootApplication to annotate the main class.
• Leveraging spring-boot-starter-* dependencies.
• Auto-configuring embedded servers like Tomcat.
• Allowing applications to run directly without external deployment.
2 Key Annotations in Spring Boot
Spring Boot uses annotations to streamline configuration and development. Be-
low are the most commonly used annotations:
2.1 @SpringBootApplication
This annotation is a combination of:
• @Configuration: Defines Spring beans in the class.
• @ComponentScan: Scans the package and subpackages for Spring compo-
nents.
• @EnableAutoConfiguration: Enables Spring Boot’s automatic configura-
tion.
1
A single @SpringBootApplication replaces these three, setting up the applica-
tion effortlessly.
2.2 @RestController
Combines @Controller and @ResponseBody. Used for building REST APIs, with
methods annotated by @GetMapping, @PostMapping, etc., returning data di-
rectly.
2.3 @Autowired
Automatically injects dependencies into fields, constructors, or setters.
2.4 @Service
Marks a class as part of the service layer, containing business logic and enabling
component scanning.
2.5 @Repository
Indicates the data access (DAO) layer. Exceptions are wrapped into DataAccessException
by Spring.
2.6 @Entity and @Id
Used in JPA to represent database tables and their primary keys.
2.7 @Value
Injects values from application properties, e.g.:
@Value(”${app.name}”)
private String appName;
3 Layered Architecture in Spring Boot
Spring Boot promotes a layered architecture to ensure separation of concerns,
keeping business logic, presentation logic, and data logic distinct. The typical
layers are:
• Controller Layer: Handles HTTP requests and returns responses.
• Service Layer: Contains business logic.
• Repository Layer: Communicates with the database using Spring Data JPA.
• Model Layer: Represents data/entities.
Spring Boot automatically scans these layers if they are under the main package.
2
4 Service Layer
The service layer, annotated with @Service, is responsible for business logic. It
uses @Autowired to inject repository dependencies and perform operations like
saving or retrieving data.
Example of a service class:
@Service
public class StudentService {
@Autowired
private StudentRepository repo;
public List<Student> getAllStudents() {
return repo.findAll();
}
public Student saveStudent(Student s) {
return repo.save(s);
}
}
This layer should focus on processing and decision-making logic, avoiding HTTP
or database configuration code.
5 Repository Layer
The repository layer, annotated with @Repository, handles database communi-
cation using Spring Data JPA. By extending JpaRepository, CrudRepository,
or PagingAndSortingRepository, you gain access to methods like save(),
findAll(), findById(), and deleteById().
Example of a repository interface:
@Repository
public interface StudentRepository extends JpaRepository<Student,
Integer> {
// Custom derived query method
List<Student> findBySName(String sName);
}
Spring generates queries automatically based on method names, unless custom
queries are specified using @Query.
6 Example: Combining All Layers
Below is an example combining the controller, service, and repository layers to
manage a Student entity.
3
6.1 Model Layer (Student Entity)
@Entity
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String sName;
// Getters and Setters
public Integer getId() { return id; }
public void setId(Integer id) { this.id = id; }
public String getSName() { return sName; }
public void setSName(String sName) { this.sName = sName; }
}
6.2 Repository Layer
@Repository
public interface StudentRepository extends JpaRepository<Student,
Integer> {
List<Student> findBySName(String sName);
}
6.3 Service Layer
@Service
public class StudentService {
@Autowired
private StudentRepository repo;
public List<Student> getAllStudents() {
return repo.findAll();
}
public Student saveStudent(Student s) {
return repo.save(s);
}
}
6.4 Controller Layer
@RestController
@RequestMapping(”/students”)
public class StudentController {
@Autowired
private StudentService service;
@GetMapping
public List<Student> getAllStudents() {
4
return service.getAllStudents();
}
@PostMapping
public Student saveStudent(@RequestBody Student student) {
return service.saveStudent(student);
}
}
6.5 Main Application
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
7 Final Thoughts for Interviews
• Spring Boot reduces setup time and simplifies Java application develop-
ment.
• Always annotate the main class with @SpringBootApplication.
• Adhere to layered architecture for clean separation of concerns.
• Use @Autowired for dependency injection, preferring constructor-based
injection for testability.
• Keep controller, service, and repository logic separate.
• JpaRepository provides powerful data access without boilerplate code.