What is Spring Boot?
Spring Boot is an open source and light-weight framework for development of Java based Applications. It is built on the top of the Spring framework. It is developed and maintained by Pivotal team. It is mainly used for Web and command line applications. It configures all the features automatically and you can run the application with one click.
What is the difference between spring Framework and Spring Boot?
Spring framework is the most widely used Java Framework for building Applications. the main feature of spring framework is dependency injection. It helps make things simpler by allowing us to develop loosely coupled applications.
Spring Boot is a Module of Spring framework. The main feature of Spring Boot is Autoconfiguration. It automatically configures a class based on that requirement. Spring Boot makes it easy to crate stand lone spring-based application that you run.
The main difference is: spring framework used several configurations for development applications. Spring Boot allows for automatic configurations using predefined class paths.
What are the features of Spring Boot?
- The main feature is Auto Configuration
- Spring Boot CLI
- Starter POMs
- Actuator
- Spring initializer
- Type safe configuration
- YAML Support
- Microservices based Architecture
- It includes embedded Tomcat server
- Better spring Boot security
- Admin Support
- Logging files
- Spring Applications
What are the minimum system requirements for Spring Boot?
Java latest version
Spring Boot 2.X (above) will no longer to support Java 7 or below versions, Being Java 8 is the minimum requirement.
- Java 8+ or above
- Spring Framework 5.2.4+
Build tools:
- Gradle 5.x and 6.x above versions (also support 4.4 +)
- Maven 3.3 + and above
Embedded servlet containers:
- Servlet 3.x + compatible container
- Tomcat 9.0- Servlet v4.0+
- Jetty9.4-servletv3.1
- Underflow 2.0-servletv 4.0
What are the Spring Boot Key Components?
- Spring Boot Auto Configuration
- Spring Boot CLI
- Spring Boot Starter POMs
- Spring Boot Actuator
What are some of the starter dependencies for spring modules?
Spring Boot provides a number of starter dependencies for Spring modules. Here, some of the most commonly used one are:
- ‘Data JPA Starter
- Test starter
- Security Starter
- Web starter
- Web Services Starter
- Mail starter
- Thymeleaf starter
For More:
What is a Spring Boot Actuator?
Actuator is a sub project of Spring Boot. It provides several endpoints in order to monitor and manage with your application. It does so by providing built-in endpoints, but you are also able to build your own endpoints. Also provides easy way to access the production ready REST points and fetch all the information from the web. Actuator make it very easy to integrate your application with any external system with a very minimal configuration.
How to enable spring Boot actuator?
Check the following maven dependency in your existing POM.XML file:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
What is Spring Boot CLI?
Spring Boot CLI is a Command line interface tool for spring Boot application. It uses Groovy Script.It is a powerful tool for create /manger your applications.
List the Spring Boot CLI commands?
- -run command
- -test command
- -grab command
- -jar command
- -war command
- -install command
- -uninstall command
- –init command
- -shell command
- -help command
What are the common Spring Boot Annotations?
Some common Spring Boot Annotations are:
@SpringBootApplication
- @SpringBootconfiguration
- @ComponentScan
- @enableAutoconfiguration
@importAutoConfiguration
@AutoConfigureBefore or After or Order
Conditional Annotations
- @ConditionalOnBean and @ConditionalOnMissingBean
- @ConditionalOnNotWebApplication and @ConditionalOnWebApplication
- @ConditionalOnProperty
- @ConditionalOnResource
- .@ConditionalOnExpression
- @ConditionalOnCloudPlatform
What are the most used Spring Actuator End Points?
/beans: this endpoint returns a complete list of all the spring beans in your application
/dump: It performs a thread dump
/env: This endpoint returns list of properties in current environment
/health: For your application health information
/trace: for trace logs
/info: displays arbitrary application information
/auditevents: Audit events information from the current application
/mappings: It display the list of all @RequestMapping paths.
/metrics: shows the metrics information-e.g: JVM,system CPU,openfiles
What is use of Relaxed Binding in Spring Boot?
Spring Boot uses some relaxed rules for binding environment properties to @configuartionproperties beans, so there does not need to be an exact match between the environment property name and the bean property name.
Name some of the spring sub projects?
- Spring Data
- Spring Batch
- Spring Integration
- Spring Security
- Spring Social
How to add custom JS code in Spring Boot?
In Spring Boot, first you create a folder called static under resources folder. You can put your static content in that folder.
For example, the path to interviewgigapp.js would be resources\static\js\interviewgigapp.js
You can refer it your code with
<script src="/js/interviewgigapp.js"></script>
Can you explain Auto Configuration in Spring Boot?
Autoconfiguration is an important feature to automatically configure an application based on dependencies that are present on the class path as jars, beans, properties, etc., requiring no efforts from the developer. Spring Boot auto-configuration attempts to automatically configure your Spring application based on the jar dependencies that you have added.
Can you explain, how to Register a Custom Auto-Configuration?
To register an auto-configuration class, we must have its fully-qualified name listed under the EnableAutoConfiguration key in the META-INF/spring.factories file:=
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.baeldung.autoconfigure.CustomAutoConfiguration
If we build a project with Maven, that file should be placed in the resources/META-INF directory, which will end up in the mentioned location during the package phase.
What is the difference between an embedded container and a WAR?
You can run spring boot application as a JAR file from the command prompt without setting up a web Server. But to run a WAR file, you need to setup a webserver like Tomcat, or underflow, or jetty which has servlet container and then you need to deploy WAR file there.
Why we use Spring Boot Maven plugin?
It is Maven Plugin built by Spring Boot team to make packaging your applications easier. It provides a few commands which enable you to package the code as ajar or run the application. The plugin provides several goals to work with a Spring Boot application:
spring-boot:repackage: create a jar or war file that is auto-executable. It can replace the regular artifact or can be attached to the build lifecycle with a separate classifier.
spring-boot:run: run your Spring Boot application with several options to pass parameters to it.
spring-boot:start and stop: integrate your Spring Boot application to the integration-test phase so that the application starts before it.
spring-boot:build-info: generate a build information that can be used by the Actuator.
Explain, how to enable HTTP/2 support in Spring Boot?
Adding below property to your properties (or. yml) file:
Server.http/2.enabled=true.
What is Spring Boot Initilizr?
It is a Spring Boot tool to Bootstrap Boot or Spring Applications very easily.
What are the Spring Boot starters?
A starter is just a Gradle or Maven module with the sole purpose of supplying all the necessary dependencies to “get started” with specific functionality. Spring Boot Starters make the bootstrapping process much easier and faster. The starter brings you required Maven dependencies as well as some predefined configuration bits.
What are the Pros of Spring Boot?
- The main Aim of spring boot is Reduce the LOC.
- It provides a lot of default configurations which helps in bootstrapping spring application faster.
- It is mainly used for create to stand loan applications.
- Not require XML configuration
- Easy to Create Spring applications.
- It comes with embedded TOMCAT or Jetty Server
- It provides a lot of Plugins.
- It provides a CLI applications.
What is use of Spring Boot dependency management?
It is used to manage dependencies and configure automatically without you specifying the version for any of that dependencies. Spring upgrades all dependencies automatically in a consistent way when we update the spring boot version.
What is use of Thymeleaf?
Thymeleaf is one of the java-based libraries used to create web app. It provides a support for serving a XHTML/HTML5 in web apps. It is powerful template processing engine for the spring framework.
What is the use of Spring Boot Devtools?
It provides an ensemble of very useful tools that enhance the development experiences a lot. Such as automatic upon saving and much more.
What is use of a Spring Data JPA?
Spring Data JPA makes it easy to implement JPA based repositors and build spring powered applications that use data access technologies.
What is H2 in Spring Boot?
H2 is an opensource RDBM (Rational Database Management) system created entirely in java. It can be embedded in java applications or run in the client server mode. Or It is a lightweight database that can be run in memory.
What are @SpringBootApplication annotations?
@SpringBootApplication annotation is a Combination of following three spring annotations and provides the functionality of all three with just one line of code.
@configuration or @springBootConfiguration (In version 2): It indicates that a class provides spring Boot application @configuration.
@componentScan: It provides support parallel with Spring XMLs context:component-scan element.
@enableAutoconfiguration: It is used to enable spring Boot’s auto configuration feature.
Explain, how can you control logging with Spring Boot?
we can control logging with Spring Boot by specifying log levels on application.properties 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.
Spring Boot uses Commons Logging for all internal logging and you can change log levels by adding following lines in the application.properties file:
logging.level.org.springframework=DEBUG
logging.level.com.demo=INFO
Why Spring Boot?
- Spring Boot is java-based application framework.
- It avoids writing lots of boilerplate code, annotations, and XML configuration.
- To reduce Development, Unit Test & Integration Test time by providing some defaults.
- Autoconfiguration, no manual configuration needed.
- Easy to use but powerful database transaction management capabilities
- To increase productivity
- Eases dependency management
- It includes embedded servlet container.
- It allows admin support
Can you explain why is request method POST recommended compared to GET for sensitive data?
Check the steps that happen when you interact with a web page
Enter the details
Browser: Creates a POST request where details are put into the request (and encrypted if you are using https)
Routers and Network: The POST request is sent through multiple routers before it reaches the destination
In the case of a get request and post request, in steps a and b you can see the details on the browser. The real change is in step c.
Can you explain why do we use Hibernate validator?
Hibernate Validator is a validation framework. It is not related to Database. It is the reference implementation Bean Validation 1.1! Actually, you can use any implementation of Bean Validation API. However, there are really no other popular options.
Can you explain how to configure Spring Boot to show Hibernate SQL Query in logs?
Add below lines into your application.properties file:
application.properties
#this line shows the sql statement in the logs
logging.level.org.hibernate.SQL=debug
#this line shows sql values in the logs
logging.level.org.hibernate.type.descriptor.sql=trace
How to replacing default converters (HttpMessageConverters)
@SpringBootApplication
public class AppMain {
@Bean
public HttpMessageConverters converters() {
return new HttpMessageConverters(
false, Arrays.asList(new TheCustomConverter()));
}
}
Output
com.logicbig.example.TheCustomConverter@305e1d50
Can you explain, how to deploy to a different server with Spring Boot?
To deploy a different server with Spring Boot, follow the below steps:
Generate a WAR from the project
Then, deploy the WAR file onto your selected server.
Can you explain, how to connect an external database like MySQL or Oracle?
Follow the below steps:
- Start by adding the dependency for MySQL Connector to pom.xml
- Then remove H2 Dependency from pom.xml
- Now, set up your MySQL database and configure your connection to the MySQL database
- Restart your project.
Can you explain, how to enable HTTPS/SSL support in Spring boot?
The SSL support in spring boot project can be added via application.properties and by adding the below entries.
application.properties
server.port=8443
server.ssl.key-alias=selfsigned_localhost_sslserver
server.ssl.key-password=changeit
server.ssl.key-store=classpath:ssl-server.jks
server.ssl.key-store-provider=SUN
server.ssl.key-store-type=JKS
How can you enable auto reload of application with Spring Boot?
First, you have to add the following mapping in your pom.xml configuration file:
<-- Spring Boot DevTools for the developer friendly options like Auto Restart of the server -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
Can you explain CommandLineRunner in Spring Boot?
CommandLineRunner will execute run() method, just after applicationcontext is created and before springboot application starts up.
It accepts the argument, which are passed at time of server startup.
@Component
public class CommandLineAppStartupRunner implements CommandLineRunner {
private static final Logger logger = LoggerFactory.getLogger(CommandLineAppStartupRunner.class);
@Override
public void run(String...args) throws Exception {
logger.info("Application started with command-line arguments: {} . \n To kill this application, press Ctrl + C.", Arrays.toString(args));
}
}
How to create an executable jar using spring boot?
If you want to create an executable jar in spring boot then you must be adding the following in your pom.xml file.
pom.xml:-
<packaging>jar</packaging>
Use “mvn clean” package to create the executable jar file.
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
What is the default H2 database configured by Spring Boot?
The default H2 database is testdb.
Refer below: spring.datasource.name=testdb # Name of the datasource.
What is Spring DAO support?
Spring DAO(Data Access Object) support in Spring makes it easy to work with data access technologies like JDBC, Hibernate or JDO in a consistent way.
What embedded containers does Spring Boot support?
It supports three embedded containers: Tomcat (default server), Jetty, and Undertow. By default, it uses Tomcat as embedded containers but you can change it to Jetty or Undertow.
How do you add a Servlet, Filter or Listener to an application?
There are two ways to add Servlet, Filter, ServletContextListener and the other listeners supported by the Servlet spec to your application. You can either provide Spring beans for them or enable scanning for Servlet components.
Can you explain, how do you Configure Log4j for logging?
It supports Log4j 2 for logging configuration if it is on the classpath. If you are using the starters for assembling dependencies that means you have to exclude Log back and then include log4j 2 instead.
Can you explain, how to execute Spring Batch jobs on start-up?
Spring Batch auto-configuration is enabled by adding @EnableBatchProcessing (from Spring Batch) somewhere in your context. By default, it executes all Jobs in the application context on start-up.
Can you explain relaxed binding?
Spring Boot uses some relaxed rules for resolving configuration properties name such that a simple property name can be written in multiple ways.
For example, a simple property “log.level.our-package” can be written in following ways and all are correct and will be resolved by framework for its value based on property source.
- level.our-package = debug //Kebab case
- level.our_package = debug //Underscore notation
- level.ourPackage = debug //Camel case
- LEVEL.OUR-PACKAGE = debug //Upper case format
Some relaxed binding rules per property source:
- Properties Files: Camel case, kebab case, or underscore notation
- YAML Files: Camel case, kebab case, or underscore notation
- Environment Variables: Upper case format with underscore as the delimiter. _ should not be used within a property name
- System properties: Camel case, kebab case, or underscore notation.
Which annotations are used to perform Unit testing in Spring Boot?
@JdbcTest : It can be used for a typical JDBC test when a test focuses only on JDBC-based components.
@JsonTest : It is used when a test focuses only on JSON serialization.
@RestClientTest : It is used to test REST clients. It auto-configures different dependencies such as Jackson, GSON, and Jsonb support; configures a RestTemplateBuilder; and adds support for MockRestServiceServer by default.
@WebMvcTest : It used for Spring MVC tests with configuration relevant to only MVC tests.
Can you explain how to enable debug logging?
First, start the application with the –debug switch.
Next, set the logging.level.root=debug property in application.properties file.
Final,Set the logging level of root logger in supplied logging configuration file.
Can you explain,how do you run and stop spring boot executable jar?
Great internet site! It looks very professional! Keep up the helpful work!