Springboot

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

SPRINGBOOT

1. What are the advantages of using Spring Boot?


The advantages of Spring Boot are listed below:
 Easy to understand and develop spring applications.
 Spring Boot is nothing but an existing framework with the addition of an embedded HTTP server
and annotation configuration which makes it easier to understand and faster the process of
development.
 Increases productivity and reduces development time.
 Minimum configuration.
 We don’t need to write any XML configuration, only a few annotations are required to do the
configuration.
2. What are the Spring Boot key components?
Below are the four key components of spring-boot:
 Spring Boot auto-configuration.
 Spring Boot CLI.
 Spring Boot starter POMs.
 Spring Boot Actuators.

3. What is the starter dependency of the Spring boot module?


Spring boot provides numbers of starter dependency, here are the most commonly used -
 Data JPA starter.
 Test Starter.
 Security starter.
 Web starter.
 Mail starter.
 Thymeleaf starter.

4. How does Spring Boot works?


Spring Boot automatically configures your application based on the dependencies you have added to
the project by using annotation. The entry point of the spring boot application is the class that contains
@SpringBootApplication annotation and the main method.

Spring Boot automatically scans all the components included in the project by using @ComponentScan
annotation.

5. What does the @SpringBootApplication annotation do internally?


The @SpringBootApplication annotation is equivalent to using @Configuration,
@EnableAutoConfiguration, and @ComponentScan with their default attributes. Spring Boot enables
the developer to use a single annotation instead of using multiple. But, as we know, Spring provided
loosely coupled features that we can use for each annotation as per our project needs.
SPRINGBOOT
6. What is the purpose of using @ComponentScan in the class files?
Spring Boot application scans all the beans and package declarations when the application initializes.
You need to add the @ComponentScan annotation for your class file to scan your components added
to your project.

7. Can we override or replace the Embedded tomcat server in Spring Boot?


Yes, we can replace the Embedded Tomcat server with any server by using the Starter dependency in
the pom.xml file. Like you can use spring-boot-starter-jetty as a dependency for using a jetty server in
your project.

6. Can we disable the default web server in the Spring boot application?
Yes, we can use application.properties to configure the web application type i.e spring.main.web-
application-type=none.

9. How to disable a specific auto-configuration class?


You can use exclude attribute of @EnableAutoConfiguration if you want auto-configuration not to
apply to any specific class.

//use of exclude
@EnableAutoConfiguration(exclude={className})

10. Explain @RestController annotation in Sprint boot?


It is a combination of @Controller and @ResponseBody, used for creating a restful controller. It
converts the response to JSON or XML. It ensures that data returned by each method will be written
straight into the response body instead of returning a template.

11. What is the difference between @RestController and @Controller in Spring Boot?
@Controller Map of the model object to view or template and make it human readable but
@RestController simply returns the object and object data is directly written in HTTP response as JSON
or XML.

12. What is the use of Profiles in spring boot?


While developing the application we deal with multiple environments such as dev, QA, Prod, and each
environment requires a different configuration. For eg., we might be using an embedded H2 database
for dev but for prod, we might have proprietary Oracle or DB2. Even if DBMS is the same across the
environment, the URLs will be different.

To make this easy and clean, Spring has the provision of Profiles to keep the separate configuration of
environments.
SPRINGBOOT
13. What is Spring Actuator? What are its advantages?
An actuator is an additional feature of Spring that helps you to monitor and manage your application
when you push it to production. These actuators include auditing, health, CPU usage, HTTP hits, and
metric gathering, and many more that are automatically applied to your application.

14. What are the actuator-provided endpoints used for monitoring the Spring boot application?
Actuators provide below pre-defined endpoints to monitor our application -
 Health
 Info
 Beans
 Mappings
 Configprops
 Httptrace
 Heapdump
 Threaddump
 Shutdown

15. How to enable debugging log in the spring boot application?


Debugging logs can be enabled in three ways -

We can start the application with --debug switch.


We can set the logging.level.root=debug property in application.property file.
We can set the logging level of the root logger to debug in the supplied logging configuration file.

16. What is dependency Injection?


The process of injecting dependent bean objects into target bean objects is called dependency
injection.
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.

17. What is an IOC container?


IoC Container is a framework for implementing automatic dependency injection. It manages object
creation and its life-time and also injects dependencies into the class.
SPRINGBOOT
18.Different Annotations
 @Required: It applies to the bean setter method. It indicates that the annotated bean must be
populated at configuration time with the required property, else it throws an exception
BeanInitilizationException.
 @Autowired: Spring provides annotation-based auto-wiring by providing @Autowired
annotation. It is used to autowire spring bean on setter methods, instance variable, and
constructor. When we use @Autowired annotation, the spring container auto-wires the bean by
matching data-type.
 @Configuration: It is a class-level annotation. The class annotated with @Configuration used by
Spring Containers as a source of bean definitions.
 @ComponentScan: It is used when we want to scan a package for beans. It is used with the
annotation @Configuration. We can also specify the base packages to scan for Spring
Components.
 @Bean: It is a method-level annotation. It is an alternative of XML <bean> tag. It tells the
method to produce a bean to be managed by Spring Container.
 @Component: It is a class-level annotation. It is used to mark a Java class as a bean. A Java class
annotated with @Component is found during the classpath. The Spring Framework pick it up
and configure it in the application context as a Spring Bean.
 @Controller: The @Controller is a class-level annotation. It is a specialization of @Component. It
marks a class as a web request handler. It is often used to serve web pages. By default, it returns
a string that indicates which route to redirect. It is mostly used with @RequestMapping
annotation.
 @Service: It is also used at class level. It tells the Spring that class contains the business logic.
 @Repository: It is a class-level annotation. The repository is a DAOs (Data Access Object) that
access the database directly. The repository does all the operations related to the database.
 @GetMapping: It maps the HTTP GET requests on the specific handler method. It is used to
create a web service endpoint that fetches It is used instead of using:
@RequestMapping(method = RequestMethod.GET)
 @PostMapping: It maps the HTTP POST requests on the specific handler method. It is used to
create a web service endpoint that creates It is used instead of using:
@RequestMapping(method = RequestMethod.POST)
 @PutMapping: It maps the HTTP PUT requests on the specific handler method. It is used to
create a web service endpoint that creates or updates It is used instead of using:
@RequestMapping(method = RequestMethod.PUT)
 @DeleteMapping: It maps the HTTP DELETE requests on the specific handler method. It is used
to create a web service endpoint that deletes a resource. It is used instead of using:
@RequestMapping(method = RequestMethod.DELETE)
 @PatchMapping: It maps the HTTP PATCH requests on the specific handler method. It is used
instead of using: @RequestMapping(method = RequestMethod.PATCH)
SPRINGBOOT
 @RequestBody: It is used to bind HTTP request with an object in a method parameter. Internally
it uses HTTP MessageConverters to convert the body of the request. When we annotate a
method parameter with @RequestBody, the Spring framework binds the incoming HTTP request
body to that parameter.
 @ResponseBody: It binds the method return value to the response body. It tells the Spring Boot
Framework to serialize a return an object into JSON and XML format.
 @PathVariable: It is used to extract the values from the URI. It is most suitable for the RESTful
web service, where the URL contains a path variable. We can define multiple @PathVariable in a
method.
 @RequestParam: It is used to extract the query parameters form the URL. It is also known as a
query parameter. It is most suitable for web applications. It can specify default values if the
query parameter is not present in the URL.
 @RequestHeader: It is used to get the details about the HTTP request headers. We use this
annotation as a method parameter. The optional elements of the annotation are name,
required, value, defaultValue. For each detail in the header, we should specify separate
annotations. We can use it multiple time in a method
 @RestController: It can be considered as a combination of @Controller and @ResponseBody
annotations. The @RestController annotation is itself annotated with the @ResponseBody
annotation. It eliminates the need for annotating each method with @ResponseBody.
 @RequestAttribute: It binds a method parameter to request attribute. It provides convenient
access to the request attributes from a controller method. With the help of @RequestAttribute
annotation, we can access objects that are populated on the server-side.
 @CacheConfig:It is a class-level annotation that provides a common cache-related setting. It
tells the Spring where to store cache for the class. When we annotate a class with the
annotation, it provides a set of default settings for any cache operation defined in that class.
Using the annotation, we need not to declare things multiple times.
 @EnableCaching: It is a class-level annotation. We can enable caching in the Spring Boot
application by using the annotation @EnableCaching. It is defined in
org.springframework.cache.annotation package. It is used together with @Configuration class.
 @Cacheable: It is a method level annotation. It defines a cache for a method's return value. The
Spring Framework manages the requests and responses of the method to the cache that is
specified in the annotation attribute. The @Cacheable annotation contains more options. For
example, we can provide a cache name by using the value or cacheNames attribute.

19.What is dependency injection in spring?


Dependency Injection is a fundamental aspect of the Spring framework, through which the Spring
container “injects” objects into other objects or “dependencies”.

Simply put, this allows for loose coupling of components and moves the responsibility of managing
components onto the container.

You might also like