For Example: Suppose We Have An Object Employee and It Has A Dependency On

You might also like

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

Question 1. What Is Spring Boot?

Spring Boot uses Commons Logging for all internal logging and you can change log levels by
First of all Spring Boot is not a framework, it is a way to ease to create stand-alone application adding following lines in the application.properties file:
with minimal or zero configurations. It is approach to develop spring based application with very logging.level.org.springframework=DEBUG
less configuration. It provides defaults for code and annotation configuration to quick start new
spring projects within no time. logging.level.com.demo=INFO

Spring Boot automatically configures required classes depending on the libraries on its
classpath. Suppose your application want to interact with DB, if there are Spring Data libraries on What are safe REST operations? 
class path then it automatically sets up connection to DB along with the Data Source class. REST API uses HTTP methods to perform operations. Some of the HTTP operations which
doesn't modify the resource at the server is known as safe operations e.g. GET and HEAD. On
the other hand, PUT, POST, and DELETE are unsafe because they modify the resource on the
Question 2. What Are The Advantages Of Using Spring Boot? server.
o It is very easy to develop Spring Based applications with Java
o It reduces lots of development time and increases productivity.
o It avoids writing lots of boilerplate Code, Annotations and XML Configuration. What are idempotent operations? Why is idempotency important?
o It is very easy to integrate Spring Boot Application with its Spring Ecosystem like There are some HTTP methods e.g. GET which produce same response no matter how many
Spring JDBC, Spring ORM, Spring Data, Spring Security etc. times you use them e.g. sending multiple GET request to the same URI will result in same
o It follows “Opinionated Defaults Configuration” Approach to reduce Developer effort response without any side-effect hence it is known as idempotent.
o It provides Embedded HTTP servers like Tomcat, Jetty etc. to develop and test our
web applications very easily. On the other hand, the POST is not idempotent because if you send multiple POST request, it will
result in multiple resource creation on the server, but again, PUT is idempotent if you are using it
Question 3. What do Dev Tools in Spring boot mean? to update the resource.
Spring boot accompanies Dev Tools, so You don’t have to redeploy your application each time
you influence the changes. The developer can reload the progressions without restart of the Even, multiple PUT request to update a resource on a server will give same end result. You can
server. <artifactId>spring-boot-devtools</artifactId> further take HTTP Fundamentals course by Pluralsight to learn more about idempotent methods
of HTTP protocol and HTTP in general.
Question 4. What Is Actuator In Spring Boot?

What are the advantages of the RestTemplate?


adds several production grade services to your application with little effort on your part. There
are also has many features added to your application out-of-the-box for managing the service in The RestTemplate class is an implementation of Template method pattern in Spring framework.
a production (or other) environment. They’re mainly used to expose different types of it also simplifies the interaction with RESTful Web Services on the client side. You can use it to
information about the running application – health, metrics, info, dump, env et consume a RESTful Web Servicer very easily

private static void getEmployees()


Question 5. How To Configure Datasource Using Spring Boot? {
Use either spring-boot-starter-jdbc or spring-boot-starterdata-jpa and include a JDBC driver on     final String uri = "http://localhost:8080/springrestexample/employees";
classpath     RestTemplate restTemplate = new RestTemplate();
     
Declare properties:
    EmployeeListVO result = restTemplate.getForObject(uri, EmployeeListVO.class);
spring.datasource.url=jdbc:mysql://localhost/test
     
spring.datasource.username=dbuser     System.out.println(result);
spring.datasource.password=dbpass }
Depencendy Injection and IOC Container
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
o Spring Boot will create a DataSource with properties set  Spring helps in the creation of loosely coupled applications because of Dependency Injection.
o Will even use a connection pool if the library is found on the classpath.  In Spring, objects define their associations (dependencies) and do not worry about how they
will get those dependencies. It is the responsibility of Spring to provide the required dependencies
for creating objects.
Question 6. Can you control logging with Spring Boot? How? For example: Suppose we have an object Employee and it has a dependency on
Yes, we can control logging with Spring Boot by specifying log levels on application.properties object Address. We would define a bean corresponding to Employee that will define its
dependency on object Address.
file. Spring Boot loads this file when it exists in the classpath and it can be used to configure
both Spring Boot and application code.
When Spring tries to create an Employee object, it will see that Employee has a dependency occurs when you create more than one bean of the same type and want to wire only one of them
on Address, so it will first create the Address object (dependent object) and then inject it into with a property.
the Employee object.
 Inversion of Control (IOC) and Dependency Injection (DI) are used interchangeably. IOC is @Configuration
achieved through DI. DI is the process of providing the dependencies and IOC is the end result of
DI. (Note: DI is not the only way to achieve IOC. There are other ways as well.)
 By DI, the responsibility of creating objects is shifted from our application code to the Spring This annotation is used on classes which define beans. @Configuration is an analog for XML
container; this phenomenon is called IOC. configuration file – it is configuration using Java class. Java class annotated
 Dependency Injection can be done by setter injection or constructor injection. with @Configuration is a configuration by itself and will have methods to instantiate and
configure the dependencies.

Spring Bean Scopes @Bean

There are five types of spring bean scopes:


This annotation is used at the method level. @Bean annotation works with @Configuration to
singleton – only one instance of the spring bean will be created for the spring container. This is create Spring beans. As mentioned earlier, @Configuration will have methods to instantiate and
the default spring bean scope. While using this scope, make sure bean doesn’t have shared configure dependencies. Such methods will be annotated with @Bean. The method annotated
instance variables otherwise it might lead to data inconsistency issues. with this annotation works as bean ID and it creates and returns the actual bean.

prototype – A new instance will be created every time the bean is requested from the spring @Lazy
container.
This annotation is used on component classes. By default all autowired dependencies are
request – This is same as prototype scope, however it’s meant to be used for web applications.
created and configured at startup. But if you want to initialize a bean lazily, you can
A new instance of the bean will be created for each HTTP request. use @Lazy annotation over the class. This means that the bean will be created and initialized
only when it is first requested for
session – A new bean will be created for each HTTP session by the container.
@EnableAutoConfiguration
global-session – This is used to create global session beans for Portlet applications.

Some Annotations This annotation is usually placed on the main application class.
The @EnableAutoConfiguration annotation implicitly defines a base “search package”. This
@Required annotation tells Spring Boot to start adding beans based on classpath settings, other beans, and
various property settings.

This annotation is applied on bean setter methods. Consider a scenario where you need to @SpringBootApplication
enforce a required property. The @Required annotation indicates that the affected bean must be
populated at configuration time with the required property. Otherwise an exception of
This annotation is used on the application class while setting up a Spring Boot project. The class
type BeanInitializationException is thrown.
that is annotated with the @SpringBootApplication must be kept in the base package
@Autowired
@ControllerAdvice

This annotation is applied on fields, setter methods, and constructors.


This annotation is applied at the class level. As explained earlier, for each controller you can
The @Autowired annotation injects object dependency implicitly.
use @ExceptionHandler on a method that will be called when a given exception occurs. But this
handles only those exception that occur within the controller in which it is defined. To overcome
this problem you can now use the @ControllerAdvice annotation. This annotation is used to
define @ExceptionHandler, @InitBinder and @ModelAttribute methods that apply to
@Qualifier all @RequestMapping methods. Thus if you define the @ExceptionHandler annotation on a
method in @ControllerAdvice class, it will be applied to all the controllers.
This annotation is used along with @Autowired annotation. When you need more control of the
@RestController
dependency injection process, @Qualifier can be used. @Qualifier can be specified on individual
constructor arguments or method parameters. This annotation is used to avoid confusion which
This annotation is used at the class level. The @RestController annotation marks the class as a
controller where every method returns a domain object instead of a view. By annotating a class
with this annotation you no longer need to add @ResponseBody to all the RequestMapping
method. It means that you no more use view-resolvers or send html in response. You just send
the domain object as HTTP response in the format that is understood by the consumers like
JSON.

You might also like