A.1) SpringBoot is a framework which is built on top of the Spring Framework that simplifies the development of java applications especially web and microservices applications. It reduces boilerplate codes and it gives fast setup with minimal configuration. Easy integration with databases, messaging, security etc. It is perfect for microservices Architecture.
A.2) 1. Auto Configuration
Automatically configures Spring components based on the project setup.Reduces manual setup and boilerplate code.
2. Starter Dependencies
Provides a set of convenient dependency descriptors (starters). Example: spring-boot-starter-web, spring-boot-starter-data-jpa.
3. Embedded Web Servers
Built-in support for embedded Tomcat, Jetty, and Undertow.No need to deploy WAR files; just run the .jar file.
4. Spring Boot Actuator
Exposes endpoints for monitoring and managing the app (e.g., health, metrics, info). Useful in production environments.
5. Spring Boot CLI
Command-line tool that lets you run Spring Boot apps using Groovy or Java. Good for quick prototyping.
6. Minimal Configuration
Sensible defaults to using applications. Properties or application.yml. Reduces the need for XML or Java configuration.
7. Production Ready
Built-in tools like Actuator and support for external configuration and profiles.Easy integration with logging, security, and monitoring tools.
8. Developer Tools (DevTools)
Supports automatic restart, live reload, and configurations for a better dev experience.
9. Microservices Support
Easily build and deploy microservices.Seamless integration with Spring Cloud, Docker, and Kubernetes.
10. Test Support
Includes testing libraries like JUnit, Mockito, and Spring Test. Helps create unit and integration tests easily.
A.3) 1. Faster Development
Spring Boot reduces boilerplate code with auto-configuration and starter dependencies.
Speeds up development with ready-to-use setups.
2. Auto Configuration
Eliminates the need for manual configuration.
Automatically configures the application based on classpath dependencies.
3. Embedded Servers
No need for external server deployment (e.g., Tomcat).
Just run the app as a standalone jar.
4. Microservice Ready
Ideal for building microservices with Spring Cloud.
Supports service discovery, config server, circuit breakers, etc.
5. Easy to Learn and Use
Simplifies Spring’s complexity.
Minimal learning curve for Java developers.
6. Production-Ready Features
Includes tools like Spring Boot Actuator for monitoring and managing your app.
Supports metrics, health checks, and logging out of the box.
7. Wide Ecosystem Support
Integrates easily with databases (JPA, JDBC), messaging (Kafka, RabbitMQ), security, cloud services, and more.
8. Spring Boot CLI
A command-line tool to quickly prototype and run Spring apps using Groovy/Java.
Allows running Spring Boot applications from the terminal using Groovy scripts.
Good for quick prototyping.
5.) Spring Boot Actuator
Provides endpoints for monitoring and managing the application. Example endpoints: /health, /metrics, /info
6.)Spring Boot Dev Tools
Helps with faster development:
Auto-restart
Live reload
Better logging for dev mode
7. Application Properties / YAML
External configuration via application.properties or application.yml.
Used to set ports, DB configs, logging, etc.
A.5) Internal Working of Spring Boot
Spring Boot simplifies the process of setting up and developing a Spring-based application. Internally, it follows a layered, auto-configuring, and opinionated architecture.
1. Application Startup (main() method)
Your app starts with the main() method that calls:
java
SpringApplication.run(MyApp.class, args);
This bootstraps the application by creating the Spring ApplicationContext.
2. Auto-Configuration
@EnableAutoConfiguration (included in @SpringBootApplication) tells Spring Boot to:
Scan the classpath.
Create and configure beans automatically based on dependencies found (like spring-boot-starter-web will configure Tomcat, Spring MVC, Jackson, etc.).
Example: If spring-boot-starter-data-jpa is on the classpath, Spring Boot:
Sets up EntityManagerFactory
Configures a DataSource
Enables JpaRepository support.
3. Component Scanning
@ComponentScan (also part of @SpringBootApplication) automatically scans for:
@Component, @Service, @Repository, @Controller, etc.
These classes are registered as beans in the context.
4. External Configuration
Reads from:
application.properties or application.yml
Environment variables
Command-line args
These values are injected using @Value, @ConfigurationProperties, etc.
5. Embedded Server Initialization
If you're building a web app, Spring Boot:
Starts an embedded server (Tomcat, Jetty, or Undertow).
Automatically configures routes, dispatchers, and servlets.
6. DevTools & Actuator (Optional)
If added, Spring Boot enables:
Live reload & restart (DevTools)
Health checks, metrics, and env info (Actuator)
7. Application is Ready
Once the context is created, beans are initialized, properties are set, and the server is up — the app is ready to serve requests.
Summary Flow:
main() →
SpringApplication.run() →
Create ApplicationContext →
Load Configuration →
Perform Component Scan →
Apply Auto-Configuration →
Start Embedded Server →
Application Ready
A.6) Spring Boot Starters are a set of convenient dependency descriptors you can include in your application. They simplify the process of adding jars (libraries) to your Spring Boot project by grouping commonly used dependencies together.
Why Use Starters?
Reduce the need to manually specify individual dependencies.
Automatically pull in transitive dependencies.
Provide auto-configuration support for common functionality.
Starter Name
Description
spring-boot-starter
Core starter – includes auto-configuration, logging, and YAML support.
spring-boot-starter-web
Builds web apps using Spring MVC, includes Tomcat by default.
spring-boot-starter-data-jpa
Adds Spring Data JPA + Hibernate for database operations.
spring-boot-starter-security
Adds Spring Security for authentication and authorization.
spring-boot-starter-test
Adds testing libraries: JUnit, Mockito, Spring Test, etc.
spring-boot-starter-thymeleaf
Adds Thymeleaf templating engine support.
spring-boot-starter-mail
Adds support for sending emails via JavaMail.
spring-boot-starter-actuator
Adds endpoints for monitoring and management (health, metrics, etc).
spring-boot-starter-validation
Adds Bean Validation (Hibernate Validator).
spring-boot-starter-aop
Adds support for aspect-oriented programming (AOP).
spring-boot-starter-cache
Adds caching abstraction support.
spring-boot-starter-logging
Configures SLF4J with Logback for logging.
spring-boot-starter-batch
Adds Spring Batch support for processing large volumes of data.
spring-boot-starter-amqp
Adds Spring AMQP and RabbitMQ support.
A.7) A Spring Boot application is started using the main() method, just like any standard Java application. However, Spring Boot provides a bootstrap mechanism through the SpringApplication class that takes care of a lot of configuration and setup behind the scenes.
1. Main Class with @SpringBootApplication
@SpringBootApplication
public class MyApp {
public static void main(String[] args) {
SpringApplication.run(MyApp.class, args);
}
}
This annotation combines:
@Configuration: for Java-based config
@EnableAutoConfiguration: for auto setup of Spring context
@ComponentScan: for scanning beans in the package
Run from IDE:
Just right-click the main class and click Run.
Run from Terminal:
bash
mvn spring-boot:run
or if using Gradle:
bash
./gradlew bootRun
Or by executing the packaged JAR:
bash
java -jar myapp.jar
A.8) The @SpringBootApplication annotation is a convenience annotation introduced by Spring Boot to simplify configuration. It bundles together three commonly used Spring annotations.
Internally, it Combines:
java
@SpringBootApplication
=>
@Configuration
@EnableAutoConfiguration
@ComponentScan
Example:
java
@SpringBootApplication
public class MyApp {
public static void main(String[] args) {
SpringApplication.run(MyApp.class, args);
}
}
A.9) Spring Boot uses several core annotations to simplify configuration, dependency injection, and behavior definition. Below are the most commonly used basic annotations in Spring Boot:
1.) @SpringBootApplication
Combines @Configuration, @EnableAutoConfiguration, and @ComponentScan.
Entry point of a Spring Boot application.
java
@SpringBootApplication
public class MyApp {
public static void main(String[] args) {
SpringApplication.run(MyApp.class, args);
}
}
2.) @RestController
Combination of @Controller + @ResponseBody.
Used to create RESTful web services.
java
@RestController
public class MyController {
@GetMapping("/hello")
public String hello() {
return "Hello, Spring Boot!";
}
}
3.) @RequestMapping, @GetMapping, @PostMapping, etc.
Used for defining beans and components for automatic detection.
Annotation
Purpose
@Component
Generic component
@Service
Business logic layer
@Repository
Persistence (DAO) layer
@Controller
MVC controller (UI layer)
6.) @Value
Injects values from property files.
java
@Value("${server.port}")
private int port;
7.) @Configuration
Marks the class as a source of Spring bean definitions.
java
@Configuration
public class AppConfig {
@Bean
public MyBean myBean() {
return new MyBean();
}
}
8.) @Bean
Used to define a Spring-managed bean.
9.) @EnableAutoConfiguration
Enables Spring Boot’s auto-configuration feature.
10.) @ComponentScan
Scans packages for Spring-managed components.
A.10) Yes, we can easily do that and change it’s default porter in many ways because spring Boot uses embedded Tomcat Server by default.
1.Change Port via application.properties
# Change default port to 9090
server.port=9090
properties
# Change default port to 9090
server.port=9090
2. Change Port via application.yml
server:
port: 9090
3. Change Port Programmatically (Java Code)
You can also configure it in your main class or a @Configuration class:
java
@Bean
public WebServerFactoryCustomizer<ConfigurableWebServerFactory> webServerFactoryCustomizer() {
return factory -> factory.setPort(9090);
}
4. Pass Port as a Command-Line Argument
java -jar myapp.jar --server.port=9090
A.11) The default port for the embedded Tomcat server in Spring Boot is:
plaintext
8080
Example
If you start a basic Spring Boot web application without configuring any port, it will run on:
http://localhost:8080
How to Confirm
When you run your app, you’ll typically see this in the console:
Tomcat started on port(s): 8080 (http)
How to Change It (Optional)
You can change it in:
application.prserver.port=9090
application.yml:
server:
port: 9090
A.12) In Spring Boot, auto-configuration is enabled by default, but you can disable specific auto-configuration classes using the exclude attribute in the @SpringBootApplication annotation.
You want full manual configuration of a component (like DataSource, Security, JPA, etc.)
You don’t want an auto-configured bean that interferes with your custom setup.
A.13) @RestController Annotation in Spring Boot
The @RestController annotation in Spring Boot is used to simplify the creation of RESTful web services. It is a specialized version of the @Controller annotation, introduced to make building REST APIs easier.
🔹 What it Does:
Combines @Controller and @ResponseBody.
Automatically serializes returned Java objects into JSON or XML (usually JSON).
Eliminates the need to annotate every method with @ResponseBody.
When accessed via http://localhost:8080/hello, it will return:
Hello from Spring Boot!
Key Points:
Used to define RESTful API endpoints.
Automatically returns data (not views).
Commonly used in microservice architecture and REST APIs.
A.14) Difference between @Controller and @RestController:
Feature
@Controller
@RestController
Definition
Marks a class as a Spring MVC controller.
Combines @Controller and @ResponseBody.
Purpose
Used to create web pages/views (e.g., Thymeleaf, JSP).
Used to create RESTful APIs (JSON/XML responses).
Response Type
Returns view names which are resolved by ViewResolvers.
Returns data directly (serialized as JSON or XML).
Annotation Needed for Response
Needs @ResponseBody to return data instead of view.
Automatically returns data, no need for @ResponseBody.
Usage
Traditional MVC web apps.
REST API development (microservices).
Example Use Case
Returning HTML pages.
Exposing endpoints that return JSON (e.g., for frontend apps)
Use @Controller for web UIs.
Use @RestController for REST APIs (especially when building microservices or backend for frontend apps).
A.15) @RequestMapping vs @GetMapping
Feature
@RequestMapping
@GetMapping
Purpose
Maps HTTP requests to handler methods for any HTTP method
Shortcut specifically for GET requests
Introduced In
Spring 2.5
Spring 4.3
Syntax
Requires specifying the HTTP method
No need to specify the HTTP method
Flexibility
Supports GET, POST, PUT, DELETE, etc.
Only for GET
Use Case
Use when multiple methods need to be handled
Use when handling only GET requests
Use @GetMapping for cleaner and more readable GET endpoints.
Use @RequestMapping when you need to support multiple HTTP methods or use advanced configuration.
A.16) They allow you to:
Load different configurations for different environments.
Avoid manual changes in the code when switching environments.
Activate specific beans only when a particular profile is active.
Declaring a Profile
You can assign a profile to a bean using the @Profile annotation:
java
@Configuration
@Profile("dev")
public class DevConfig {
@Bean
public DataSource devDataSource() {
// return development DataSource
}
}
java
@Configuration
@Profile("prod")
public class ProdConfig {
@Bean
public DataSource prodDataSource() {
// return production DataSource
}
}
Activating a Profile
You can activate a profile in different ways:
1. In application.properties
properties
spring.profiles.active=dev
2. Via Command Line
java -jar myapp.jar --spring.profiles.active=prod
3. In Tests
@ActiveProfiles("test")
public class MyTest {
// test code
}
Real-World Use Case
Use dev profile for development DB, logging, and debug settings.
Use test profile for mock beans and in-memory DB.
Use prod for real DB and performance-tuned configurations.
A.17) Spring Boot Actuator is a powerful tool that provides production-ready features to help you monitor and manage your Spring Boot application. It exposes a set of built-in endpoints over HTTP (or JMX) to inspect the internal state of your application without writing extra code.
🔹 Key Features:
Feature
Description
Health checks
Know whether your app is up (/actuator/health)
Metrics
Gather JVM, CPU, memory, GC, HTTP requests, etc. metrics
Info
Display custom application info like version, name, etc.
Environment
View configuration properties (/actuator/env)
Beans
List all Spring beans loaded in the app
Thread dumps
Get thread dump of running app
HTTP Traces
Shows recent HTTP requests and responses
🔹 How to Use It
Step 1: Add the dependency to your pom.xml or build.gradle