1. What is the Spring Boot? Why should we use it?
Answer: Spring Boot is another Java framework from the Sring umbrella which aims to simplify the use of the Spring Framework for Java development. It removes most of the pain associated with dealing with Spring e.g. a lot of configuration and dependencies and a lot of manual setups.
Why should we use it? - Spring Boot not only provides a lot of convenience by auto-configuration a lot of things for you but also improves productivity because it lets you focus only on writing your business logic.
For example, you don't need to set up a Tomcat server to run your web application. You can just write code and run it as a 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.
In short, there are many reasons to use Spring Boot. In fact, it's now the standard way to develop Java application with the Spring framework.
2. What are the important features of Spring Boot?
Answer: Following are some of the important features of the Spring Boot framework:
1. Starter dependency: This feature aggregates common dependencies together. For example, if you want to develop Spring MVC based RESTful services then instead of including Spring MVC JAR and Jackson JAR file into classpath you can just specify spring-boot-web-starter and it will automatically download both those JAR files. Spring Boot comes with many such starter dependencies to improve productivity.
2. Auto-Configuration: This is another awesome feature of Spring Boot which can configure many things for you. For example, If you are developing Spring web application and Thymeleaf.jar is present on the classpath then it can automatically configure Thymeleaf template resolver, view resolver, and other settings. Good knowledge of auto-configuration is required to become an experienced Spring Boot developers.
3. Spring Initializer: A web application that can create an initial project structure for you. This simplifies the initial project setup part.
4. Spring Actuator: This feature provides a lot of insights into a running Spring boot application. For example, you can use Actuator to find out which beans are created in Spring's application context and which request path is mapped to controllers.
5. Spring CLI: This is another awesome feature of Spring Boot which really takes Spring development into the next level. It allows you to use Groovy for writing Spring boot application which means a lot more concise code.
3. What is auto-configuration in Spring boot? how does it help? Why Spring Boot is called opinionated?
Answer: As explained in the previous example, it automatically configures a lot of things based upon what is present in the classpath.
For example, it can configure JdbcTemplate if its present and a DataSource bean are available in the classpath. It can even do some basic web security stuff if Spring security is present in the classpath.
The point is auto-configuration does a lot of work for you with respect to configuring beans, controllers, view resolvers, etc, hence it helps a lot in creating a Java application.
Now, the big questions come, why it's considered opinionated? Well because it makes a judgment on its own. Sometimes it imports things which you don't want, but don't worry, Spring Boot also provides ways to override auto-configuration settings.
It's also disabled by default and you need to use either @SpringBootApplication or @EnableAutoConfiguration annotations on the Main class to enable the auto-configuration feature.
4. What is starter dependency in Spring Boot? How does it help?
Answer: After examining several Spring-based projects Spring guys notice that there is always some set of libraries that are used together e.g. Spring MVC with Jackson for creating RESTful web services. Since declaring a dependency in Maven's pom.xml is the pain, they combined many libraries into one based upon functionality and created this starter package.
This not only frees you from declaring many dependencies but also frees you from compatibility and version mismatch issues. Spring Boot starter automatically pulls a compatible version of other libraries so that you can use them without worrying about any compatibility issue.
5. What is the difference between @SpringBootApplication and @EnableAutoConfiguration annotation?
Answer: Even though both are essential Spring Boot annotations and used in the Main class or Bootstrap class there is a subtle difference between them. The @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.
The @SpringBootApplication is in fact combination of @Configuration, @ComponentScan and @EnableAutoConfiguration annotations.
6. What does the @SpringBootApplication annotation do internally?
Answer: As per the Spring Boot doc, 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 individual annotation as per our project needs.
7. How to exclude any package without using the basePackages filter?
Answer: There are different ways you can filter any package. But Spring Boot provides a trickier option for achieving this without touching the component scan. You can use the exclude attribute while using the annotation @SpringBootApplication. See the following code snippet:
@SpringBootApplication(exclude= {Employee.class}) public class MyAppConfiguration { }
8. How to disable a specific auto-configuration class?
Answer: You can use the exclude attribute of @EnableAutoConfiguration, if you find any specific auto-configuration classes that you do not want are being applied.
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
On the other foot, if the class is not on the classpath, you can use the excludeName attribute of the annotation and specify the fully qualified name instead.
Also, Spring Boot provides the facility to control the list of auto-configuration classes to exclude by using the spring.autoconfigure.exclude property. You can add into the application.properties. And you can add multiple classes with comma-separated.
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
9. What is Spring Initializer? why should we use it?
Answer: One of the difficult things to start with a framework is the initial setup, particularly if you are starting from scratch and you don't have a reference setup or project. Spring Initializer addresses this problem in Spring Boot.
It's nothing but a web application that helps you to create the initial Spring boot project structure and provides Maven or Gradle build file to build your code.
10. What is Spring Actuator? What are its advantages?
Answer: It allows you to see inside an application. Since Spring Boot is all about auto-configuration it makes debugging difficult and at some point in time, you want to know which beans are created in Spring's Application Context and how Controllers are mapped. Spring Actuator provides all that information.
It provides several endpoints e.g. a REST endpoint to retrieve this kind of information over the web. It also provides a lot of insight and metrics about application health e.g. CPU and memory usage, number of threads, etc.
It also comes with a remote shell that you can use to securely go inside the Spring Boot application and run some commands to expose the same set of data. You can even use JMX to control this behavior at runtime.
Btw, it's important to secure your Spring Actuator endpoints because it exposes a lot of confidential information and a potentially dangerous one-two. For example, by using /showdown endpoint you can kill a Spring Boot application.
But, don't worry. You can use Spring Security to secure Spring Actuator endpoints.
11. How to enable/disable the Actuator?
Answer: Enabling/disabling the actuator is easy; the simplest way is to enable features to add the dependency (Maven/Gradle) to the spring-boot-starter-actuator, i.e. Starter. If you don't want the actuator to be enabled, then don't add the dependency.
Maven dependency:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> </dependencies>
12. What is a shutdown in the actuator?
Answer: Shutdown is an endpoint that allows the application to be gracefully shutdown. This feature is not enabled by default. You can enable this by using management.endpoint.shutdown.enabled=true in your application.properties file. But be careful about this if you are using this.
13. What is Spring Boot CLI? What are its benefits?
Answer: Spring Boot CLI is a command-line interface that allows you to create Spring-based Java application using Groovy. Since it's used Groovy, it allows you to create Spring Boot application from the command line without ceremony e.g. you don't need to define getter and setter method, or access modifiers, return statements, etc.
It's also very powerful and can auto-include a lot of libraries in Groovy's default package if you happen to use it.
For example, if you use JdbcTempalte, it can automatically load that for you.
14. Where do you define properties in the Spring Boot application?
Answer: You can define both application and Spring boot related properties into a file called application.properties. You can create this file manually or you can use Spring Initializer to create this file, albeit empty.
You don't need to do any special configuration to instruct Spring Boot to load this file. If it exists in classpath then Spring Boot automatically loads it and configure itself and application code accordingly.
For example, you can use to define a property to change the embedded server port in Spring Boot.
15. Is this possible to change the port of the Embedded Tomcat server in Spring boot?
Answer: Yes, it's possible to change the port. by adding a property called server.port (i.e. server.port=8081) in the application.properties file we can change the port of the embedded Tomcat server. Make sure you have application.properties in your project classpath; Spring framework will take care of the rest.
If we want to set the server port as the random port (Generally used when working with micro-services) the server. port should be assigned with '0' (Zero) (i.e. server.port=0).
16. Can we override or replace the Embedded Tomcat server in Spring Boot?
Answer: Yes, we can replace the Embedded Tomcat with any other servers by using the Starter dependencies. You can use spring-boot-starter-jetty or spring-boot-starter-undertow as a dependency for each project as you need.
No comments:
Post a Comment