Table of Contents
ToggleSpring Boot is the best Framework to develop the software projects. Because it is loosely coupled, easy to maintain, easy to develop the Web application and most important thing is that it is auto configurable to add dependency in POM file and Easily setup with MicroService architecture development.
In Spring boot IOC container, Scopes, Profiles, server configuration, Database connectivity, spring Annotation, Spring security, Spring exception topic are most Important during interview.
We’ve gathered some commonly asked questions and answers about Spring Boot to assist you in performing well in the interview. Now, let’s explore a variety of helpful interview questions related to Spring Boot,Like as: spring boot interview questions and answers or Top Spring Boot Interview Questions & Answers (2024) or Spring Boot Annotations or spring boot dependencies or What is Spring Boot used for? extra…
1). What is Spring Boot? (Most Imp Question)
Spring boot is developed by Red Jonson in 2003. The latest version of Spring boot -3.2.0 .Spring is framework for java and a Spring module which is offers Rapid Application Development to Spring framework. Spring module is used to create an application based on Spring framework which requires to configure few Spring files. Spring Boot not only provides auto-configuration , it is also lot of things for you but also improves the productivity because it lets you focus only on writing your business logic.
For example: you don’t need to setup a Tomcat server to run your web application. You can just write code and run it as Java application because it comes with an embedded Tomcat server. You can also create a JAR file or WAR file for deployment based on your convenience.
Note: RAD or Rapid Application Development process is an adoption of the waterfall model; it targets developing software in a short period. it is a modify waterfall model which is focus to develop the software development in short time.
2). What is Features of Spring boot ? (Imp Question)
Features of Spring Boot:
• You can create both Jar and war type War files project.
• It is provides simple POM file for maven configuration.
• It automatically configure Spring whenever possible.
• Provide production-ready features such as metrics, health checks, and externalized configuration.
• It avoiding heavy configuration of XML which is present in spring
• It provides easy maintenance and creation of REST endpoints
• It includes embedded Tomcat-server
• Deployment is very easy, war and jar files can be easily deployed in the tomcat server
3).What is the different between Spring and Spring Boot? (Imp Question)
Spring |
Spring Boot |
A web application framework |
A module of Spring |
Provides tools and libraries to |
Used to create a Spring |
Spring is more complex than |
Spring Boot is less complex |
4). What are the Spring Boot Annotations? or What are the annotation used in Spring Boot? (Most Imp Question)
Spring Boot Annotationsis a form of metadata that provides data about a program. In other words, annotations are used to provide supplemental information about a program.
@SpringBootApplication: It consists of @Configuration, @Enabe-AutoConfiguration and @Component-Scan.
The class annotated with @SpringBootApplication is kept in the base package. This annotation does the component scan. However, only the sub-packages are scanne
@EnableAutoConfiguration: When we add dependency in POM file then this annotation will Auto configure that related dependency things.Like as – if we add Web dependency in POM so it will Auto all things related to Web dependency.
@Configuration: This annotation is class level annotation and it return the Beans objects to the application and these beans manage by IOC container.
@ComponentScan: This annotation tells Spring to scan the Base or current package and its all sub-packages to check there is any bean or not.
@RestController: it provides the Restful web service. It make combination of
@Respond_Body : it convert Java Objects into JSON objects and send to client + @Controller: It contains MVC architecture or Model view and Contorller)
@Autowired: This annotation is used to inject dependencies into a class.Autowiring enables the programmer to inject the bean automatically.
@Component: Whenever a class is denote as Component annotation. It means that Spring framework will auto-detect these classes for dependency injection.
@Service: in this class we write the Business logic of project.
@Repository: It is a Data Access Object (DAO) that accesses the database directly.
@Bean: The @Bean annotations are used at the method level and indicate that a method produces a bean objects that is to be managed by the Spring container.
@Qualifier: it is used along with @Autowired annotation. It is used when more control is required over the dependency injection process.
Note: it is used to resolve ambiguity when multiple beans of the same type exist and @Qualifier is more priority given.
@Primary annotation: it is designating a default bean to be injected when no qualifier is specified.
@Lazy: Suppose there is many bean objects is running in application that means they will reduce the perform of your application that case we make those as @lazy annotate with @componant class . So it these @lazy classes will be run when the call , otherwise they will not run .
Like as: Bean factory Lazy load
Egar Initlization: Application context in early load
Note: @SpringBootApplication,@Lazy, @configuration, @Qualifier & @Primary most asked annotated by intervier)
@PathVariable: @PathVariable annotation, retrieve data from the URL path.
@RequestParam: when we fetching From data.
@RequestBody: it change the Json data into java objects which data coming form DB.
@ResonseBody: It convert the java objects into json and make it serialize and send to Db.
@Get: To get or fetch the data from Database.Data send in header.
or @RequestMapping: Request _Mapping is used to map the HTTP request or it map a URL pattern to a controller methods.
@Post: this annotation use to create or save data in database and data will be secure.
5). What is the difference between POST and PUT annotation? (Most Imp Question)
Both are use for update data.
@PUT: In put you send all the parameter in put but in. like as – id , name, phone, Addess , email extra
@Patch: Patch you have send to update that particular parameter.
6). how to create a Spring Boot application using Maven.
There are many way to create a Spring Boot application using maven, but following are the ways to create a Spring Boot project/application using maven:
. Spring Boot CLI
. Spring Starter Project Wizard
. Spring Initializr
. Spring Maven Project
7). What is IOC Container in spring boot? (Most Imp Question)
IOC stands for Inversion of Control.It is predefined program or component.
It maintain the Object life cycle in Spring.
. Create the object
. Hold the object in memory
. Inject one object into another object – Dependency Injection
We need to tell the container:
1) Beans objects information: Pojo class
2) Configuration class information: which object depend on which object
So IOC container with the help of Configuration, make the Beans or Pojo class objects and maintain their lifecycle.
Our application,just get these objects.
Types of IOC Container: IOC container have 2 types and both are interface.
. Bean factory: it is contains less function and implementing class.
. Application Context Factory: Application Context Factory contains more features like – messaging Exception handling , i-18 ,
8). What is dependency Injection? (Most Imp Question)
The process of injecting dependent bean objects into target bean objects is called dependency injection.
Note: Dependency injection is a Design Pattern .
Suppose there is 2 class A and B when we make A class object into B Class with new keyword that means A class is dependent on B class.So dependency injection not create object instead we pass Reference of A class into Class B. this is known as Inversion of control.
. Setter Injection: The IOC container will inject the dependent bean object into the target bean object by calling the setter method.
. Constructor Injection: The IOC container will inject the dependent bean object into the target bean object by calling the target bean constructor.
. Field Injection: The IOC container will inject the dependent bean object into the target bean object by Reflection API.
9). What is Spring boot Scopes ? or Scopes in Spring boot. (Most Imp Question)
Scope can be defined using spring bean @Scope annotation.Scope means what type of Bean object you have creating.
Note: ByDefault bean contains singleton type scope. Like as: @scope(scopeName=”singleton”)
Six inbuilt bean scopes in spring boot:
Bean Scope |
Description |
1. singleton |
This |
2. prototype |
It is |
3. request |
It |
4. session |
It |
5. global-session |
It |
10). How do you create a Spring Boot application using Maven?
There are different approaches to create a Spring Boot application using maven. Here are they:
. Spring Boot CLI
. Spring Starter Project Wizard
. Spring Initializr
. Spring Maven Project
11). What is the purpose of @Value Annotation in Spring Boot? o how to dynamically read or add value from property file in Spring boot ?
The purpose of @Value Annotation in Spring Boot is to read the value of the environment or application property in Java code. The following is the syntax for this:
@Value(“${property_key_name}”)
12). What is the way to reload changes on Spring boot without server restart?
Any changes are reloaded in spring boot without starting the server by using dev tools.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
Spring Boot provides a module called DevTools, which can enhance the productivity of a spring boot developer.
It can auto-deploy the changes to the server, with auto-restarting the server.
13). What is thymeleaf and how is it used?
Thymeleaf is a server-side java template engine used for web applications. The main purpose of thymeleaf is to bring natural templates for web applications which integrate well with Spring Framework and HTML5 java web applications. For using thymeleaf the following code must be added in the pom.xml file.
org.springframework.boot
spring-boot-starter-thymeleaf
14). What is the Spring Boot Actuator? (Imp Question)
Spring Boot provides the actuator to monitor and manage our application. Actuator contains the HTTP endpoints. When the application is pushed to production then by using HTTP endpoints you can choose to manage and monitor your application.
Example:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
15). What are Spring Boot Profiles and how are they useful? (Most Imp Question)
Spring Boot profiles are a way of managing configuration for different environments.
Suppose you might have one profile for development, one profile for testing, and one profile for production. To create a profile, you can create a file with the .profile extension in the config directory of your classpath.
Example: To create a development profile, you would create a file called development.profile in the config directory.
the following code shows a development profile that configures the database connection URL:
spring.datasource.url=jdbc:postgresql://localhost:5432/my_database
To activate the profile, you can add the –spring.profiles.active=profile-name parameter to the command line when you start your application. For example, to activate the development profile, you would start your application with the following command:
java -jar my-app.jar –spring.profiles.active=development
You can also activate the profile using the environment variable SPRING_PROFILES_ACTIVE.
16). How do you handle exceptions in the Spring boot? (Most Imp Question)
There are two main ways to handle exceptions in this framework:
Spring Boot Exception Handling: Spring Boot puts forth many options to handle exceptions, such as @ExceptionHandler annotations and global exception handlers.
Spring MVC Exception Handling: Spring MVC provides several ways to handle exceptions in a Spring MVC app, such as ModelAndView objects and ExceptionHandlerExceptionResolver beans. Spring Boot Exception Handling is the recommended way to handle exceptions in application code. It is more flexible and easier to use than Spring MVC Exception Handling.
@ExceptionHandler annotation: To handle an exception using Spring Boot Exception Handling, you can add this annotation,with a method of your controller class. The method that you annotate with @ExceptionHandler will be executed when an exception of the specified type occurs.
Example: In below we to handle a SQLException using Spring Boot Exception Handling:
@Controller
public class MyController {
@ExceptionHandler(SQLException.class)
public String handleSQLException(SQLException e) {
return “error”;
}
}
17). How to configure the spring security in spring boot? or What do you know about Spring Boot Security?
Spring Boot Security is a framework that has a range of features for securing the application. These features include authentication, authorization, and session management.
To configure security for your SpringBoot application, you need to create a WebSecurityConfigurerAdapter class. This class comes with methods to tune up the security of your application.
Example: To configure basic authentication for your SpringBoot application:
public class MyWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.anyRequest().authenticated()
.and()
.httpBasic();
}
}
18). How can I change server in spring boot application? or how to change tomcat server by jetty server in spring boot ?
(Most Imp Question)
You will need to update pom.xml, add the dependency for spring-boot-starter-jetty. Also, you will need to exclude
default added spring-boot-starter-tomcat dependency.
Example:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
//To add jetty dependency
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
19). How to configure two database or DB in spring boot ? (Most Imp Question)
In Spring Boot, configuring and using two data sources can be achieved by defining multiple DataSource beans and specifying the appropriate EntityManagerFactory and TransactionManager for each data source. Here’s a step-by-step guide:
Add Dependencies: Ensure that you have the necessary dependencies in your pom.xml or build.gradle file.
Example: if you’re using Maven:
Add Dependecy in POM file :
<!– Spring Boot Starter Data JPA –>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!– H2 Database (Example) – Replace with your database dependencies –>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
Configure application.properties : Define properties for each data source in your application.properties:
in properties file add both two database
# First Data Source
spring.datasource.first.url=jdbc:mysql://localhost:3306/firstdb
spring.datasource.first.username=root
spring.datasource.first.password=root
spring.datasource.first.driver-class-name=com.mysql.cj.jdbc.Driver
# Second Data Source
spring.datasource.second.url=jdbc:mysql://localhost:3306/seconddb
spring.datasource.second.username=root
spring.datasource.second.password=root
spring.datasource.second.driver-class-name=com.mysql.cj.jdbc.Driver
Adjust the properties according to your database configurations.
Create Configuration Classes: Create two configuration classes, each representing a data source. Annotate them with @Configuration and @EnableTransactionManagement:
java
Copy code
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
basePackages = “com.example.firstdb.repository”,
entityManagerFactoryRef = “firstEntityManagerFactory”,
transactionManagerRef = “firstTransactionManager”
)
public class FirstDataSourceConfig {
// Define first data source beans (DataSource, EntityManagerFactory, TransactionManager)
}
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
basePackages = “com.example.seconddb.repository”,
entityManagerFactoryRef = “secondEntityManagerFactory”,
transactionManagerRef = “secondTransactionManager”
)
public class SecondDataSourceConfig {
// Define second data source beans (DataSource, EntityManagerFactory, TransactionManager)
}
Define Data Source Beans: In each configuration class, define the data source bean, EntityManagerFactory, and TransactionManager:
java
Copy code
@Bean(name = “firstDataSource”)
@ConfigurationProperties(prefix = “spring.datasource.first”)
public DataSource firstDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = “firstEntityManagerFactory”)
public LocalContainerEntityManagerFactoryBean firstEntityManagerFactory(
EntityManagerFactoryBuilder builder,
@Qualifier(“firstDataSource”) DataSource dataSource) {
// Configure first EntityManagerFactory
}
@Bean(name = “firstTransactionManager”)
public PlatformTransactionManager firstTransactionManager(
@Qualifier(“firstEntityManagerFactory”) EntityManagerFactory entityManagerFactory) {
// Configure first TransactionManager
}
Repeat the same process for the second data source.
Use Data Sources in Repository Classes: In your repository classes, use the @Qualifier annotation to specify which EntityManager to use:
java
Copy code
@Repository
public class FirstRepository {
@PersistenceContext(unitName = “firstEntityManagerFactory”)
private EntityManager entityManager;
// Your repository methods
}
@Repository
public class SecondRepository {
@PersistenceContext(unitName = “secondEntityManagerFactory”)
private EntityManager entityManager;
// Your repository methods
}
Now, Spring Boot should be configured to use two data sources. Adjust the configurations based on your specific requirements and database choices.
More info on spring boot : spring boot guide
————————————————————————————————————————————————————————————————————————————————————————————-People also ask- @transactional spring boot | Thymeleaf Spring Boot | Spring Boot Security | Spring Boot Interview Questions For Experienced | Spring Boot Application | swagger in spring boot | @bean in spring boot | crud operation in spring boot | one to many mapping in spring boot | dependency injection in spring boot | spring and spring boot difference | spring boot annotations list | rest template spring boot | spring boot interview questions for 10 years experience | h2 database spring boot | pagination in spring boot | jwt authentication spring boot | @service in spring boot | jwt token spring boot | spring boot w3schools | transaction management in spring boot | @transactional in spring boot | spring boot annotations interview questions | one to one mapping in spring boot | spring boot logo | spring boot interview questions for 2 years experience | @restcontroller in spring boot | @component in spring boot | spring boot interview questions for 3 years experience | @value spring boot | autowired in spring boot | @query in spring boot | actuator spring boot | responseentity in spring boot | | | | resttemplate spring boot | | | | spring mvc vs spring boot | | | | spring boot download | | | | spring boot exception handling | | | | @requestbody in spring boot | | | | responseentity spring boot | | | | aop in spring boot | | | | application.properties spring boot | | | annotation in spring boot | | | spring boot course | | | how to handle exception in spring boot | | | | spring boot vs spring | | | spring boot features | | | global exception handling in spring boot | | | | webclient spring boot | | | | redis cache spring boot | | interview questions on spring boot | | | | dto in spring boot | | | | @entity annotation in spring boot | | | | oauth2 spring boot | | | | jpa in spring boot | | | | spring boot microservices interview questions | | | | javatpoint spring boot | | 700 | | 5 days | | spring boot rest api | | | spring boot rest api example | | | | caching in spring boot | | | | advantages of spring boot | | | | spring boot tutorial for beginners | | | | bean in spring boot | | | interceptor in spring boot | | | mapstruct spring boot | | spring boot 3 | | @value annotation in spring boot | | | java spring boot tutorial | | resttemplate in spring boot | | | crud operations in spring boot | | | | logger in spring boot | | | features of spring boot | | | Want to see all SEO metrics?
| resilience4j with spring boot 3 | resilience4j with spring boot | resilience4j retry with spring boot | resilience4j-spring-boot2 | resilience4j-spring-boot3 maven | spring boot | spring boot framework | Spring boot example | resilience4j-spring boot fallback example | jwt with spring boot example | spring boot jwt simple example | spring jwt example | what is the use of jwt token in spring boot | spring boot jwt client examp | what are annotations used in spring boot | annotations used in spring boot microservices | annotations used in spring boot rest api | common annotations used in spring boot | all annotations used in spring boot application and purpose of each | what annotations are used to create an interceptor in spring boot | different annotations used in spring boot | what are some common spring boot annotations | basic annotations used in spring boot | jwt with spring boot example | jwt spring boot example github | jwt oauth2 spring boot example | jwt filter spring boot example | jwt spring boot sample | jwt spring boot 3 example | jwt refresh token spring boot example | implementing jwt authentication in spring boot | implementing jwt authentication using spring boot | how to implement jwt refresh token in spring boot | spring boot scopes | What is Features of Spring boot ? | What is the different between Spring and Spring Boot? | What are the Spring Boot Annotations? or What are the annotation used in Spring Boot? | @Lazy | What is the difference between POST and PUT annotation? | What is dependency Injection? | What is Spring boot Scopes ? | What is the Spring Boot Actuator? | What are Spring Boot Profiles ? | How do you handle exceptions in the Spring boot? | How to configure two database or DB in spring boot ?