10 Basic Spring Boot Questions Every Developer Should Know
10 Basic Spring Boot Questions Every Developer Should Know
This annotation is a convenience feature that does three major things behind the
scenes:
1. @Configuration: It tells Spring Boot that this class will define some settings or
configurations for the application. For example, you might have configurations
for connecting to a database.
Practical Use:
Example Code:
@SpringBootApplication
public class MyApplication {
Here, the MyApplication class is marked with @SpringBootApplication . This single line
tells Spring Boot to get busy setting up your application by combining essential
configurations and scanning for components.
Running a Spring Boot application is quite straightforward. Here's how you do it:
Through the Main Method:
Spring Boot applications are Java applications that have a main method. This is the
standard entry point where the Java Virtual Machine (JVM) starts execution of an
application.
In a Spring Boot application, the main method does something special: it calls
SpringApplication.run() . This method is responsible for bootstrapping the Spring
application context, which is the central hub of a Spring application. It's where all
the magic of dependency injection, configurations, and more happens.
Example:
Suppose you have a Spring Boot application with a main class called MyApplication .
Running it would look like this:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
2. Using a Build Tool: If you're using a build tool like Maven or Gradle, you can use
the command line to run your application.
Both these methods compile your code, if necessary, and start the Spring application
context, which, among other things, starts an embedded web server if your application
is a web application.
A Spring Boot Starter is a special kind of dependency descriptor in Spring Boot that
simplifies the Maven or Gradle build configuration. Starters are essentially a set of
convenient dependency groupings for various types of development in Spring Boot.
The primary purpose of a Spring Boot Starter is to provide all the necessary
dependencies in one place so that you don't have to hunt down and specify each
one individually.
They help in reducing boilerplate code in your build configuration file ( pom.xml for
Maven or build.gradle for Gradle).
using Spring MVC. It provides all the necessary dependencies to build a web
application.
Maven ( pom.xml ):
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
Gradle ( build.gradle ):
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
}
By adding this starter, you can immediately start developing your web application
without worrying about individual dependencies and version conflicts, as Spring Boot
manages them for you. It's a key feature of Spring Boot that contributes to its
convention-over-configuration approach, making it easier and faster to get a Spring
application up and running.
Understanding Auto-Configuration
Concept:
How It Works:
1. Dependency Detection:
For example, if it sees that you have added a web-related library like Spring
MVC, it assumes you are building a web application and sets up the necessary
configuration accordingly.
2. Conditional Configuration:
If certain conditions are met (like the presence of a specific class in the
classpath), the auto-configuration logic kicks in.
Spring Boot provides sensible defaults for various configurations, but it also
allows you to override these defaults.
In Spring Boot, if you include a database driver on your classpath (like H2, MySQL, etc.)
and provide database properties in your application.properties or application.yml ,
Spring Boot will automatically configure a DataSource bean for you.
Example of application.properties :
spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.datasource.username=myuser
spring.datasource.password=mypass
Spring Boot will use these properties to auto-configure a connection to your MySQL
database. This process significantly simplifies the setup and reduces the amount of
boilerplate configuration code.
A Spring Profile in Spring Boot is a feature that allows you to define sets of
configurations that can be selectively enabled under different conditions. It's like having
different settings for an application that you can switch based on the environment or
circumstances.
How It Works:
When the application runs, you activate a specific profile, and only the
configurations associated with that profile are applied.
Example Use-Cases:
1. Different Databases: You might want to use an in-memory database like H2 for
testing but a real database like MySQL in production.
2. API Keys: You could have different API keys for development and production
environments.
Activating a Profile:
Example in application.properties :
spring.profiles.active=dev
Using Annotations:
You can also define beans for specific profiles in your code using the @Profile
annotation.
@Configuration
@Profile("dev")
public class DevDatabaseConfig {
// Configuration beans for development
}
@Configuration
@Profile("prod")
public class ProdDatabaseConfig {
// Configuration beans for production
}
In this example, the DevDatabaseConfig class will only be loaded if the dev profile is
active, and similarly for ProdDatabaseConfig with the prod profile.
Spring Profiles are a powerful tool for managing and organizing different configurations
for a Spring Boot application, enabling a clean separation of concerns and making your
application adaptable to various environments.
The default server port for a Spring Boot web application is 8080 .
Explanation:
When you create a web application using Spring Boot and run it without specifying any
port configuration, it will start up on port 8080 . This means that you can access the
application locally by navigating to http://localhost:8080 in your web browser.
Example in application.properties :
http://localhost:9090 instead.
Example in application.yml :
server:
port: 9090
Alternatively, you can specify the port directly when running the application:
This flexibility in configuring the server port is useful in various scenarios, like avoiding
port conflicts with other running applications or setting up different environments.
You can use online ASCII art generators to create text-based graphics if you
like.
When you start your Spring Boot application, it will automatically detect the
banner.txt file and display its contents in the console.
Example Code:
import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class MyApplication {
Example in banner.txt :
My Application
Version: ${spring.application.version}
It provides sensible defaults for Maven plugins. For instance, it configures the
java.version property to define the Java version for compilation, and sets up
The parent POM pre-configures the Spring Boot Maven plugin, which is
essential for packaging and running Spring Boot applications. This plugin helps
4. Resource Filtering:
It includes settings for resource filtering, which can be useful for including
properties files and other resources in your JARs with support for variable
substitution.
Example:
In your Spring Boot project’s pom.xml , you would typically declare the spring-boot-
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.0</version> <!-- Specify the Spring Boot version here -->
</parent>
By specifying this parent POM, your project inherits all the default configurations set by
Spring Boot, making setup and dependency management much simpler.
Benefits:
Simplifies Maven Configuration: Reduces the amount of Maven configuration
needed in your pom.xml .
Easy to Update: Simplifies the process of updating Spring Boot versions - you only
need to change the version in the parent POM.
Example:
Suppose you have a property in your application.properties file:You can access this
property in a Spring component as follows:
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class MyBean {
@Value("${app.greeting}")
private String greeting;
Example:
First, define your properties in application.properties :Then, create a configuration
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "app")
public class AppConfig {
Validation: You can add validation annotations to your configuration class fields.
Tips:
Property Source: You can define properties in different profiles like application-
dev.properties or application-prod.properties for different environments.
Default Values: With the @Value annotation, you can specify default values using
the colon : syntax, like @Value("${some.property:default}") .
When you use @RestController , you tell Spring Boot that this controller is going
to handle REST API requests and should serialize/deserialize service
responses/requests to JSON or XML.
endpoint response.
Example Usage:
Here’s an example to illustrate how @RestController can be used in a Spring Boot
application:
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MyRestController {
@GetMapping("/hello")
public String sayHello() {
return "Hello, World!";
}
}
When a GET request is made to /hello , this method returns a simple string "Hello,
Use Cases:
Creating API Endpoints: Commonly used to create RESTful endpoints in
microservices or web applications.