0% found this document useful (0 votes)
6 views20 pages

Spring Boot

Spring Boot is a Java framework that simplifies the creation and running of applications by providing features like auto-configuration and embedded servers. It offers advantages over the traditional Spring framework, such as reduced boilerplate code and production-ready applications. Key components include Spring Boot starters for dependency management, annotations for configuration, and tools for testing and monitoring applications.

Uploaded by

Ashlesha Karande
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views20 pages

Spring Boot

Spring Boot is a Java framework that simplifies the creation and running of applications by providing features like auto-configuration and embedded servers. It offers advantages over the traditional Spring framework, such as reduced boilerplate code and production-ready applications. Key components include Spring Boot starters for dependency management, annotations for configuration, and tools for testing and monitoring applications.

Uploaded by

Ashlesha Karande
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 20

1.What is the spring boot?

 Spring boot is a java framework that makes it easier to create and run java
applications.
 It simplifies the configuration and setup process,allowing developers to
focus more on writing code for their applications.
 Spring Boot ,a module of the spring framework,facilitates rapid application
development capabilities.
 Spring Boot solve many developers problems-
1.configurations problems
2.dependency management
3.providing the embedded server.

2.why spring boot over spring?


 Spring boot provides many advantages over normal spring framework.
1.Easy to use-Remove boilerplate codes
2.providing production ready applications such as Metrix,Health checkers.
3.providing rapid application developments using opinionated approach
and auto-configurations.
4.also providing the dependency management,auto-
configurations,embedded servers.

3.working of Spring Boot


 Spring Boot starts by scanning the starter dependencies in pom.xml file.
Then download and auto-configure the module as you included in pom.xml
 For example we have to create web application then we have to put spring-
boot-starter-web dependency in pom.xml.
1.when we start the project spring boot downloads all the dependencies
required for web and configure the things like Spring MVC.
4.How Spring Boot Starts?
 Spring boot starts by calling main method of main class().
 The run() method of SpringApplication is called.This method starts the
application by creating an application context and initializing it.
 Once the application context is initialized.the run() method starts the
application’s embedded web server.

5.Top Spring Boot Annotations?


1.@springBootApplication: It combines three annotations-
@Configuration,@EnableAutoConfiguration,and @ComponentScan.it is typically
placed on the main class of the application.
2.@component:It is used to mark a class as a spring bean that will be managed by
the spring container
3.@Autowired:This annotations automatically injects the dependencies.
4.@Service:This annotations is used to indicate that a class represents a service
component in the application.
5.@RestController:Mark class as rest controller.it is a specialized version of the
@Controller annotation that includes the @ResponseBody annotation by default.
6.@RequestMapping: used to map specific url to method.
7.@Repository:mosly used the class that has database persistent logic.

6.What are the Spring Boot Starters?


 Starters are a collection of pre-configured dependencies that make it easier
to develop particular kinds of applications.
 These starter includes all the dependencies ,version controls and
configuration.
7.what are the key dependencies of spring boot?
 Spring-boot-starter-parent=providingconfigurations,dependency
management
 Spring-boot-maven-plugin= managing the spring boot maven
 Spring-boot-starter-test=unit testing,integration testing
 Spring-boot-starter-security= providing Authentication,security.
 Spring-boot-Starter-actuator=metrics,health check
 Spring-boot-Starter-web=web related dependencies.

8.Can we use Only spring Boot Dependency feature and configure maven plugin
manually?
 Yes
 We don’t’ inherit from the spring-boot-starter-parent pom.

9.what are the spring boot CLI and what are its benefits?
 Spring Boot CLI (Command Line Interface) is a command-line tool that
allows you to quickly develop Spring applications using a simplified syntax.
 Benefits
1.Rapid prototyping.
2.simplified Syntax.

10.what is the thymeleaf?


 Java-based server-side templating engine used in java web applications to
render dyanamic web pages.

11.what is the IOC or Inversion of Control?


 Inverting the control of creating objects using new keyword manually to
container or framework.
12.explain the spring Bean-Life cycle
 Bean-simple plain java object.
 Spring bean life cycle is managed by the inversion of controls container.
1.container get started.
2.container creates objects of bean as per requests.
3. bean Dependencies is created.
4. bean dependencies are injected.
5.destroyed when container closed.

13.@PostConstruct=The postConstruct annotations is used on a method


that needs to be executed after dependency injection is done to perform
any initialization.

14.@preDestroy:The predestroy annotations is used on a method as


callback notifications to signal that the instance is in the process of being
removed by the container.

15.what is bean factory,have you used XMLBeanFactory?


 This is the root interface for accessing a spring bean container.
 It is the actual container that instantiates,configures and manages a number
of beans.
16.what is the difference between BeanFactory and ApplicationContext in spring?
 applicationContext provides the additional features –
1.Automatic BeanpostProcessor registration
2.Automatic BeanFactoryPostProcessor registration.
3.ApplicationEventpublications

17.difference between the setter and constructor injection in spring?


 Constructor injection is important to remember type and order of the
constructor parameters.
 Constructor injection is the mandatory dependency and setter method is
optional dependency.
18.what are the different modules in spring?
 spring has the seven core modules-
1.The core container module-involving the core concepts like ioc
container,dependency injection.
2.Application Context module
3.AOP module(Aspect Oriented Programming)
4.JDBC abstraction and DAO module
5.ORM module(Object/Relational)
6.Web Module
7.Test Module

19.Difference between @AutoWired and @Inject annotation in spring?


 The @Inject annotations also servers the same purpose as
@Autowired.
 The main difference between them is that @Inject is a Standard
annotations for dependency injection and @Autowired is spring
specific.
20.difference between the @Bean and @Component annotations in spring?
 @component Annotations can be used as component Scanning and
automatic wiring.
 @Bean annotations returns an object that spring should registers as bean in
application context.
21.what is autowiring in spring?what are the autowiring modes?
 Autowiring injects the bean automatically.we don’t need to write explicit
injection logic.
 Modes-
1.no-this is the default mode,it means autowiring is not enabled.
2.byName-injects the bean based on the propery name.it uses setter
method.
3.byType-injects the bean based on the propery type.it uses setter method.
4.constructor-it injects the bean using constructor.
22.what are the different bean scopes in spring?
 Singleton-The bean instance will be created only once and same instance
will be returned by the Ioc Container.
 Prototype-the bean instance will be created each time when requested.
 Request-the bean instance will be created per HTTP request.
 Session-The bean instance will be created per http session.
 Globalsession-The bean instance will be created per HTTP global session.
23.why it is need to connect the spring boot app to database?
 Because spring boot app stores the temporary data it not stores the
permanent data .There is a need to stores the data into the databases.
 That’s why it is need to connect the spring boot app to the databases.
24.what is the connection pool?
 The connection pool is the collection of all the connections.
 Whenever we want to establish the connection between spring boot app
and mysql databases we using the connections in connection pools.
 Because every time creating the new connections are costly.

25.how to connect spring boot app to the mysql database?


 Firstly we installing MYSQL and creating new databases.
 Then we opening spring boot projects pom.xml file and adding dependency
of mysql connector.
 Then opening the application.properties file in your spring boot projects.
 And Adding the database connection properties such as URL, username,
password.
 Then run your springboot applications.
26. what is the Hikari Connection pool?
 HikariCP is a high-performance JDBC connection pool used in Spring Boot
applications to manage database connections efficiently.
27.what is jdbcClient?How it is Different from jdbcTemplate?perform Curd
Operation using JDBCClient.
 Jdbc clien is added to spring 6.1 version and spring boot 3.2 version.
 It simplifies the JDBC operations
 It provides chaining like way for doing jdbcoperations.
jdbcClient.sql().param().query().list()

28.what is the jdbc template?


 The JDBC Template is a part of the Spring Framework that simplifies the use
of JDBC (Java Database Connectivity) for database operations. It provides a
set of methods and abstractions to handle common tasks such as:
 Connection Management: Automatically handles the opening and closing of database
connections.
 Query Execution: Simplifies the execution of SQL queries.
 Error Handling: Provides standardized handling of SQL exceptions.
 Mapping Results: Converts SQL query results into Java objects.

29.what is the servlet?


 A servlet is a Java programming language class used to extend the
capabilities of servers that host applications.
 servlets are used to handle the logic for web applications, processing
requests from clients (typically web browsers), performing backend
operations, and sending responses back to the clients.

30.What is the spring controllers?


 Spring controllers handles the multiple requests.

31.what is the maven?


Maven is a build automation and project management tool primarily used for Java
projects. It provides a comprehensive framework for managing a project's build,
reporting, and documentation from a central piece of information.

32. What is an embedded server in Spring Boot?

 Answer: An embedded server allows you to package your application along with the
server, eliminating the need to deploy your application on an external server. Examples
include embedded Tomcat.

33. How do you configure a Spring Boot application?


Answer: Configuration can be done using application properties or YAML files
(application.properties or application.yml). You can also use
annotations such as @Configuration and @Value to configure beans.

34. What is application.properties and application.yml?

 Answer: These files are used for externalizing configuration in a Spring


Boot application. application.properties is a key-value format,
while application.yml uses a hierarchical data format.

35. How do you create a Spring Boot application using Spring Initializr?

 Answer: Spring Initializr is a web-based tool to bootstrap a Spring Boot project. You can
specify project metadata, dependencies, and generate a ZIP file with the initial project
structure.

36. How can you change the default port of the embedded server in Spring Boot?

 Answer: You can change the default port by setting server.port in


application.properties or application.yml.

37. What is Spring Boot DevTools?

 Answer: Spring Boot DevTools provides development-time features such as automatic


restart, live reload, and configurations for improved development experience.

38. How do you manage dependencies in Spring Boot?

 Answer: Dependencies are managed using Maven or Gradle build files. Spring Boot
provides dependency management through starters, which are pre-configured sets of
dependencies for different functionalities.
39. What is Spring Boot Actuator?

 Answer: Spring Boot Actuator provides production-ready features like monitoring,


metrics, health checks.

40. How do you secure a Spring Boot application?

 Answer: Security can be added using Spring Security, which can be configured through
dependencies and annotations such as @EnableWebSecurity and @Configuration.

41. What are the different ways to externalize configuration in Spring Boot?

 Answer: Configuration can be externalized using:


o application.properties or application.yml
o Environment variables
o Command-line arguments
o Configuration files in different environments

42. How can you run a Spring Boot application?

 Answer: You can run a Spring Boot application using:


o The main method in the main class annotated with @SpringBootApplication
o Using Maven/Gradle commands (mvn spring-boot:run or gradle bootRun)

43. How do you profile a Spring Boot application?

 Answer: You can define different profiles using the @Profile annotation and activate
them using the spring.profiles.active property.

44. How do you test a Spring Boot application?

 Answer: Testing can be done using Spring Boot Test which includes various features
like @SpringBootTest, @WebMvcTest, @DataJpaTest, etc., to provide support for
different types of testing.

45. What is @SpringBootTest?

 Answer: @SpringBootTest is an annotation used to create an application context for


integration tests. It loads the complete Spring application context for the test.

46. What is @MockBean?

 Answer: @MockBean is used to add or replace beans with mock objects in the Spring
application context. It is commonly used in tests to mock dependencies.
47. How do you connect to a database using Spring Boot?

 Answer: You can connect to a database by including the relevant dependencies (e.g.,
spring-boot-starter-data-jpa) and configuring the data source properties in
application.properties or application.yml.

48. What is Spring Data JPA?

 Answer: Spring Data JPA is a part of Spring Data that makes it easy to implement JPA-
based repositories. It provides a set of interfaces and annotations to manage relational
data.

49. How do you use Spring Data JPA in a Spring Boot application?

 Answer: Add the spring-boot-starter-data-jpa dependency, create entity classes,


repositories extending JpaRepository, and configure the data source.

50. What is @Entity?

 Answer: @Entity is an annotation that specifies a class as an entity and is mapped to a


database table.

51. What is JpaRepository?

 Answer: JpaRepository is an interface provided by Spring Data JPA that extends


CrudRepository and PagingAndSortingRepository and adds JPA-specific methods
for data access.

52. How do you create a RESTful web service using Spring Boot?

 Answer: By using spring-boot-starter-web dependency, annotating classes with


@RestController, and defining request mappings with @RequestMapping or
@GetMapping, @PostMapping, etc.

53. What is @RestController?

 Answer: @RestController is a combination of @Controller and @ResponseBody. It is


used to create RESTful web services where the response body is directly written to the
HTTP response as JSON or XML.

54. What is @RequestMapping?

 Answer: @RequestMapping is used to map web requests to specific handler methods in a


controller. It can be used at the class or method level.

55. What is the difference between @GetMapping and @RequestMapping?


 Answer: @GetMapping is a composed annotation that acts as a shortcut for
@RequestMapping(method = RequestMethod.GET).

56. What is @PathVariable?

 Answer: @PathVariable is used to extract values from the URI template and bind them
to method parameters.

57. What is Spring Cloud?

 Answer: Spring Cloud is a framework that provides tools for building distributed
systems and microservices, integrating with other services like Netflix OSS, and
managing configuration, service discovery

58. What is Netflix Eureka?

 Answer: Netflix Eureka is a service registry and discovery tool used in Spring Cloud for
locating services for the purpose of load balancing and failover.

59.  How do you enable a Spring Boot application as a Eureka client?

 Answer: Add the spring-cloud-starter-netflix-eureka-client dependency and


annotate the main class with @EnableEurekaClient.

60. What is Spring Cloud Config?

 Answer: Spring Cloud Config provides server and client-side support for externalized
configuration in a distributed system. It allows applications to retrieve configuration from
a central configuration server.

61. How do you use Feign in a Spring Boot application?

 Answer: Add the spring-cloud-starter-openfeign dependency, annotate the main


class with @EnableFeignClients, and define Feign clients using the @FeignClient
annotation.

62. What is the Spring Boot Actuator and what are its advantages?

 Answer: Spring Boot Actuator provides production-ready features like monitoring and
metrics for Spring Boot applications. It helps in managing and monitoring applications by
providing endpoints to gather metrics, understand traffic, and the state of the database.
63. How do you monitor Spring Boot applications?

 Answer:spring Boot actuators.

64. How do you handle exceptions in Spring Boot?

 Answer: You can handle exceptions globally using @ControllerAdvice and


@ExceptionHandler annotations or by implementing HandlerExceptionResolver.

65. What is @EnableAutoConfiguration?

 Answer: @EnableAutoConfiguration tells Spring Boot to automatically configure your


application based on the dependencies present in the classpath.

66. How do you create a custom starter in Spring Boot?

 Answer: Create a project with the required dependencies and configurations, and
package it as a JAR file. This JAR can then be used as a dependency in other Spring Boot
projects.

67. How do you optimize Spring Boot application performance?

 Answer: Performance can be optimized by:


o Monitoring spring boot application using Actuator.
o Using proper indexes in the database
o Caching frequently accessed data

68.  How do you handle caching in Spring Boot?

 Answer: Caching can be implemented using Spring's @Cacheable, @CachePut, and


@CacheEvict annotations along with a cache manager configuration.

69. What is @Async?

 Answer: @Async is an annotation used to mark methods that should run asynchronously.
It allows methods to run in a separate thread without blocking the main thread.

70. How do you implement pagination in Spring Boot?

 Answer: Pagination can be implemented using Spring Data JPA's Pageable interface
and Page object in repository methods.
71. How do you use profiles in Spring Boot?

 Answer: Profiles can be used to define different configurations for different


environments. You can specify active profiles using spring.profiles.active property
or by passing --spring.profiles.active argument when running the application.

72. How do you deploy a Spring Boot application to a cloud platform?

 Answer: Spring Boot applications can be deployed to cloud platforms like AWS, Google
Cloud by creating a JAR or WAR file and using the platform-specific tools or services to
deploy.

73. What is @ComponentScan?

 Answer: @ComponentScan is used to specify the packages to scan for annotated


components (e.g., @Component, @Service, @Repository).

74. How do you configure logging in Spring Boot?

 Answer: Logging can be configured using the application.properties or


application.yml files. You can specify log levels, log formats.

75. What is spring-boot-starter-parent?

 Answer: spring-boot-starter-parent is a special starter that provides default


configurations for a Spring Boot application, including dependency management and
plugin configurations.

76.What is Spring Boot's opinionated approach?

 Answer: Spring Boot's opinionated approach means it provides default configurations


and setups that are considered best practices, reducing the need for developers to make
many decisions.

77. How do you create a custom health indicator in Spring Boot?

 Answer: You can create a custom health indicator by implementing the


HealthIndicator interface and overriding the health method.

78. What are Spring Boot banners?

 Answer: Spring Boot banners are displayed in the console when the application starts.
You can customize the banner by placing a banner.txt file in the src/main/resources
directory.
79.How do you consume a RESTful service in Spring Boot?

 Answer: You can consume RESTful services using RestTemplate or WebClient in


Spring Boot.

80.What is @RequestBody?

 Answer: @RequestBody is used to map the body of the HTTP request to a Java object.

81.What is @ResponseBody?

 Answer: @ResponseBody is used to indicate that the return value of a method should be
written directly to the HTTP response body.

82. What is @RequestParam?

 Answer: @RequestParam is used to extract query parameters from the request URL and
bind them to method parameters.

83. How do you handle form submissions in Spring Boot?

 Answer: Form submissions can be handled using @Controller with methods annotated
with @PostMapping or @RequestMapping.

84. How do you handle errors in Spring Boot?

 Answer: Errors can be handled using @ControllerAdvice and @ExceptionHandler


annotations.

85. What is CSRF and how do you handle it in Spring Boot?

 Answer: CSRF (Cross-Site Request Forgery) is a security vulnerability. In Spring Boot,


it can be handled using Spring Security by enabling CSRF protection, which is enabled
by default.
86. What is the purpose of @EnableWebSecurity?

 Answer: @EnableWebSecurity is used to enable Spring Security's web security support


and provide the Spring MVC integration.

87. How do you secure RESTful web services in Spring Boot?

 Answer: RESTful web services can be secured using Spring Security, JWT (JSON Web
Tokens)and role-based access controls.

88.How do you deploy a Spring Boot application as a JAR?

 Answer: You can deploy a Spring Boot application as a JAR by packaging it using
Maven or Gradle and running it using java -jar.

89.What is Spring Boot's support for Docker?

 Answer: Spring Boot applications can be easily containerized using Docker by creating a
Dockerfile that includes the application JAR and specifying the base image and entry
point.

90. How do you create a Spring Boot application Docker image?

 Answer: Create a Dockerfile in the root of your project, specifying the base image ,
copying the JAR file, and defining the entry point.

91. How do you scale a Spring Boot application?

 Answer: Scaling can be done using:


o Load balancers
o Horizontal scaling by running multiple instances

92. What are some best practices for deploying Spring Boot applications in production?

 Answer: Best practices include:


o Using externalized configuration
o Implementing monitoring and logging
o Using a reverse proxy (e.g., Nginx)
o Implementing health checks

93. What is the use of @Value in Spring Boot?

 Answer: @Value is used to inject values into fields from property files or environment
variables.
94. How do you implement internationalization (i18n) in Spring Boot?

 Answer: Internationalization can be implemented using MessageSource to provide


messages for different locales and configuring LocaleResolver.

95. What is Spring Boot's opinionated view layer?

 Answer: Spring Boot provides default view configurations and templates for Thymeleaf,
FreeMarker, and other view technologies, simplifying the setup of the view layer.

96. How do you create and use custom annotations in Spring Boot?

 Answer: Custom annotations can be created by defining a new annotation with


@interface and using meta-annotations to specify its behavior.

97. What is @Scheduled?

 Answer: @Scheduled is used to schedule tasks in Spring. It can be used to run methods
at fixed intervals, or with fixed delays.

98. How do you handle large file uploads in Spring Boot?

 Answer: Large file uploads can be handled using MultipartFile in controller methods
and configuring multipart settings in application.properties.

99. What is @Conditional?

 Answer: @Conditional is used to conditionally include beans in the application context


based on certain conditions. Custom conditions can be defined by implementing the
Condition interface.

100. How do you handle validation in Spring Boot?

 Answer: Validation can be handled using JSR-303/JSR-380 annotations (e.g., @NotNull,


@Size) and Spring's @Valid annotation.

101.What is the difference between @RequestBody and @ModelAttribute?

 Answer: @RequestBody binds the HTTP request body to a Java object.


@ModelAttribute binds request parameters to a model object, typically used for form
data.

102. How do you implement OAuth2 authentication in Spring Boot?

 Answer: OAuth2 authentication can be implemented using Spring Security OAuth2 by


configuring authorization and resource servers, and defining security policies.
103. How do you set up continuous integration for a Spring Boot project?

 Answer: Continuous integration can be set up using tools like, GitLab CI, or Travis CI
by defining build pipelines, running tests.

104 How do you create a REST API documentation for a Spring Boot application?

 Answer: REST API documentation can be created using Swagger/OpenAPI.

105.How do you debug a Spring Boot application?

 Answer: Debugging can be done using IDE tools (e.g., breakpoints, watch expressions)
and by adding logging statements.

106. What are some common challenges faced when developing Spring Boot applications?

 Answer: Common challenges include:


o Dependency management
o Configuration management
o Handling large data sets
o Security implementation

107. How do you integrate Spring Boot with Hibernate?

 Answer: Integration with Hibernate can be done using the spring-boot-starter-


data-jpa dependency, configuring the data source, and defining entity classes.

108.What is @Data in Lombok?

 Answer: @Data is a Lombok annotation that generates getter, setter, toString, equals, and
hashCode methods for a class.

109. How do you handle transactions in Spring Boot?

 Answer: Transactions can be handled using the @Transactional annotation, which can
be applied to methods or classes to manage transaction boundaries.

110.What is the purpose of spring-boot-starter-test?

 Answer: spring-boot-starter-test is a starter that provides dependencies for testing


Spring Boot applications, including JUnit,and Spring Test.
111.How do you integrate Spring Boot with a messaging system like RabbitMQ?

 Answer: Integration can be done using the spring-boot-starter-amqp dependency


and configuring RabbitTemplate, message listeners, and queues.

112. How do you implement a batch process in Spring Boot?

 Answer: Batch processes can be implemented using Spring Batch, which provides
features like declarative I/O, and transaction management.

113.How do you handle API versioning in Spring Boot?

 Answer: API versioning can be handled using URI versioning, request parameter
versioning, or header versioning.

114. How do you implement rate limiting in Spring Boot?

 Answer: Rate limiting can be implemented using filters, interceptors, or by integrating


with external libraries.

115. How do you create a multi-module Spring Boot project?

 Answer: A multi-module project can be created using Maven or Gradle by defining


parent and child modules with shared dependencies and configurations.

116.How do you handle circular dependencies in Spring Boot?

 Answer: Circular dependencies can be resolved by refactoring the code to eliminate


circular references.

117. What is the difference between-

@Controller and @RestController? –

Answer: @Controller is used to define a controller in Spring MVC, and typically returns views.
@RestController is a combination of @Controller and @ResponseBody, and is used to create
RESTful web services returning JSON or XML.

118.

Feature Spring Framework Spring Boot


Configuration Requires extensive manual Provides auto-configuration for rapid
Feature Spring Framework Spring Boot
configuration. setup.
Needs external server setup (e.g.,
Standalone Setup Includes embedded servers (e.g., Tomcat).
Tomcat).
Simplifies with built-in starter
Dependency Mgmt Requires manual dependency handling.
dependencies.

119.

Aspect JDBC Hibernate


A low-level API for database communication A high-level ORM framework that maps Java
Definition
using SQL queries directly. objects to database tables.
Requires manual SQL handling and object Automates SQL generation and object-relational
Complexity
mapping. mapping.

120. which methods are available in the hibernate?

 Session Methods: save(), update(), delete(), get(), load(), merge(), and persist() for CRUD
operations.
  Transaction Management: beginTransaction(), commit(), and rollback().
  Query Execution: createQuery(), createSQLQuery(), and createNamedQuery() to execute HQL
or SQL.
 121.path variable=when we want to get the parameterized data from server. then we use path
variable.
 Request body =when we want to get the complete data from server. then we use request body.

You might also like