Durgesh Imp SB Questions

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

Springboot durgesh notes

Q1) Is it possible to change the port of the embedded Tomcat server in Spring Boot?

Answer:

A) Yes, it is possible. By using the server.port in the application.properties.

Application.properties

Server.port=9090

Q2) Can we override or replace the Embedded tomcat server in Spring Boot?

Answer:

A) Yes, we can replace the Embedded Tomcat server with any server by using the Starter
dependency in the pom.xml file.

<dependency>

<groupId>org.springframework.boot</groupId>

<exclusions>

<artifactId>spring-boot-starter-web</artifactId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>

<exclusion>

</exclusion> </exclusions>

</dependency>

And now you need to include new server as a dependency i.e.:

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-jetty</artifactId>

</dependency>

Q3) What is auto-configuration in Spring boot? How does it help? Why Spring Boot is called
opinionated?

Answer:
Autoconfiguration is spring boot magic features, it automatically configures a lot of things based upon
what is present in the class path.

<dependencies>

<dependency>

<groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>

</dependency>

</dependencies>

Mysql dependency when you it will autoconfigure and when you add spring jpa it will also automatically
configure

Opinionated means enable and disable we can do do.

Q4) What is the difference between @SpringBootApplication and @EnableAutoConfiguration


annotation?

A) @EnableAutoConfiguration is used to enable auto-configuration but @SpringBootApplication


does a lot more than that.

It also combines @Configuration and @ComponentScan annotations to enable Java-based configuration


and component scanning in your project.

@SpringBootApplication= @Configuration + @EnableAutoConfiguration+@ComponentScan


@SpringBootApplication

Public class SpringBootAnnotationsApplication implements CommandLineRunner {

Opinionated is a software design pattern that decides or guides you into their way of doing things. Spring
Boot is opinionated because it follows the opinionated default configuration that reduces developer
efforts to configure the application.

Q5) How to disable a specific auto-configuration class?

A) We can use exclude attribute of @EnableAutoConfiguration annotation to exclude specific class.

//use of exclude

@EnableAutoConfiguration (exclude={className})

Q6) What is the difference between @RestController and @Controller in Spring Boot?

A) @Controller Map of the model object to view or template and make it human readable

@RestController simply returns the object and object data is directly written in HTTP response as JSON
or XML

@RestController = @Controller + @ResponseBody


Q8) What is the difference between @RequestMapping and @GetMapping?

A) @RequestMapping can be used with GET, POST, PUT, and many other request methods using the
method attribute on the annotation.

Whereas @GetMapping is only an extension of @RequestMapping with GET method which helps you to
improve on clarity on request.

@RequestMapping (value = “/user/{userId}”, method. RequestMethod.GET)

@GetMapping (value = “/user/{userId}”

Q9) What is the difference between an embedded container and a WAR?

A) The main difference between an embedded container and a WAR file is that you can Spring Boot
application as a JAR from the command prompt without setting up a web server.

But to run a WAR file, you need to first set up a web server like Tomcat which has Servlet container and
then you need to deploy WAR there.

Q10) What is the use of Profiles in spring boot?

A) Spring has the provision of Profiles to keep the separate configuration of environments.

Q11) What is Spring Actuator? What are its advantages?

A) Provides special feature to monitor and manage your application when you push it to
production.
<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-actuator </artifactId>

</dependency>

Through spring actuator we can enable and disable spring boot properties

Q12) How to get the list of all the beans in your Spring boot application?

A) Spring Boot actuator “/Beans” is used to get the list of all the spring beans in your application
B) “/env” returns the list of all the environment properties of running the spring boot application.

You might also like