Interview questions for spring boot

Time:2020-10-22

Spring boot is the best Java framework in microservices. We suggest that you become an expert in spring boot. This article selects 35 common spring boot knowledge points, I wish you a hand!

Question 1

 

What is the difference between spring boot, spring MVC and spring?

1、Spring

The most important feature of spring is dependency injection. All spring modules are either dependency injection or IOC inversion of control.

When we use di or IOC properly, we can develop loosely coupled applications. Unit testing of loosely coupled applications can be done easily.

2、Spring MVC

Spring MVC provides a separate method to develop web applications. By using some simple concepts like dispatcher servlet, moudlandview and viewresolver, developing web applications will become very simple.

3、SpringBoot

The problem with spring and spring MVC is that a large number of parameters need to be configured.

Spring boot solves this problem through an automatic configuration and startup item. In order to build product ready applications faster, spring boot provides some non functional features.

 

Question 2

 

What is automatic configuration?

The problem with spring and spring MVC is that a large number of parameters need to be configured.

Can we bring more intelligence? When an MVC jar is added to the application, can we automatically configure some beans?

Spring looks at the configuration of an existing application (the framework available on classpath). On this basis, spring boot provides the basic configuration needed to configure applications and frameworks. This is automatic configuration.

 

Question 3

 

What is spring boot stater?

The launcher is a convenient set of dependencies without descriptors, which can be placed in your own program. You can get all the spring and related technologies you need in a one-stop shop, without relying on descriptors to search and copy pasted loads through sample code.

For example, if you want to use spring and JPA to access databases, you can do it perfectly if your project contains spring boot starter data JPA dependencies.

Question 4

 

Can you give an example to explain more about staters?

Let’s consider an example of stater – spring boot stater web.

If you want to develop a web application or an application that exposes rest services. Spring boot start web is the first choice. Let’s use spring initializer to create a quick project for spring boot start web.

Dependencies for spring boot start Web

The following screenshot shows the different dependencies added to our application

Dependencies can be divided into:

  • Spring – core,beans,context,aop

  • Web MVC – (Spring MVC)

  • Jackson – for JSON Binding

  • Validation – Hibernate,Validation API

  • Enbedded Servlet Container – Tomcat

  • Logging – logback,slf4j

Any classic web application uses all of these dependencies. Spring boot starter web Prepackages these dependencies.

As a developer, I don’t need to worry about these dependencies and their compatible versions.

 

Question 5

 

What other starter project options does spring boot provide?

Spring boot also provides other starter projects, including typical dependencies for developing specific types of applications.

  • spring-boot-starter-web-services – SOAP Web Services;

  • Spring boot starter web web web and restful applications;

  • Spring boot starter test unit test and integration test;

  • Spring boot starter JDBC – traditional JDBC;

  • Spring boot starter HATEOAS – add HATEOAS to the service;

  • Spring boot starter security – uses spring security for authentication and authorization;

  • Spring boot starter data JPA – spring data JPA with hibernate;

  • Spring boot starter data rest – use spring data rest to publish simple rest services;

 

Question 6

 

How does spring quickly create product ready applications?

Spring boot is dedicated to fast product ready applications. For this reason, it provides some non functional features such as cache, logging, monitoring and embedded server out of the box.

Spring boot starter actor – uses advanced features such as monitoring and tracking applications

Spring boot starter undertow, spring boot starter jetty, spring boot starter Tomcat – select your specific embedded servlet container

Spring boot starter logging – logging using logback

Spring boot starter cache – enables caching support for the spring framework

###What is the minimum java version required for spring 2 and spring 5?

Spring boot 2.0 requires Java 8 or later. Java 6 and Java 7 are no longer supported.

Recommended reading:

https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.0.0-M1-Release-Notes

 

Question 7

 

What is the easiest way to create a spring boot project?

Spring initializer is a good tool to start spring boot projects.

As shown in the figure above, we need to take the following steps:

  • Log in to spring initializr and choose as follows:

  • choice com.in28minutes . springboot is a group

  • Select student services as the component

  • Select the dependencies below

  • Web

  • Actuator

  • DevTools

  • Click generate project

  • Import the project into eclipse. File import existing Maven project

 

Question 8

 

Is spring initializer the only way to create spring boot projects?

no, it isn’t.

Spring initiationisr makes it easy to create a spring boot project, but you can also start a project by setting up a maven project and adding the right dependencies.

In our spring course, we used two methods to create projects.

The first method is start.spring.io 。
Another way is to manually set it in the project heading “basic web application.”.

Set up a maven project manually

Here are several important steps:

  • In eclipse, create a new project using file new Maven project

  • Add a dependency.

  • Add Maven plug-in.

  • Add the spring boot application class.

Here, the preparations are ready!

 

Question 9

 

Why do we need spring boot Maven plugin?

Spring boot Maven plugin provides commands to package or run applications like jars.

  • spring- boot:run Run your springbooty application.

  • Spring boot: repackage repackage repackage your jar or war package to make it executable

  • Spring boot: start and spring boot: stop manage the life cycle (or integration testing) of spring boot applications.

  • spring- boot:build-info Build the construction information that the actuator can use.

 

Question 10

 

How to use springboot to automatically reload my application?

Use spring boot development tools.

It’s easy to add spring boot development tools to your project.

Add the following dependencies to your spring boot project pom.xml in

Restart the application, and then it’s OK.

Similarly, if you want to load pages automatically, check out five reload

  • http://www.logicbig.com/tutorials/spring-framework/spring-boot/boot-live-reload/.

When I tested, I found the livereload vulnerability. If you also found it during the test, please let us know.

 

Question 11

 

What is an embedded server? Why do we use embedded servers?

Think about what it takes to deploy an application on your virtual machine.

Step 1: install Java

Part 2: install the web or application server (tomat / wbesphere / Weblogic, etc.)

Part 3: deploy application war package

If we want to simplify these steps, what should we do?

Let’s think about how to make the server part of the application?

You just need a virtual machine with Java installed, and you can deploy applications directly on it,
Isn’t it cool?

This idea is the origin of embedded servers.

When we create a deployable application, we will embed the server (for example, Tomcat) into the deployable server.

For example, for a spring boot application, you can generate an application jar containing embedded Tomcat. You can run web applications just like normal Java applications.

An embedded server is our executable unit that contains the server’s binaries (for example, tomcat.jar )。

 

Question 12

 

How to add common JS code in spring boot?

Under the source folder, create a folder called static. Then, you can put your static content in there.

For example, myapp.js The path of is resources / static / JS\ myapp.js

You can refer to its usage in jsp:

Error: Hal browser gives me unauthorized error – full authentication is required to access this resource

How to fix this error?

There are two methods

Method 1: turn off security verification

application.properties

management.security.enabled:FALSE

Method 2: search the log for the password and pass it to the request header

 

Question 13

 

What is spring data?

From:// projects.spring.io/spring – data/

The mission of spring data is to provide a familiar, consistent and spring based programming model for data access on the premise of ensuring the particularity of underlying data storage. This makes it easy to use data access technology, relational and non relational databases, map reduce framework and cloud based data services.

To make it simpler, spring data provides abstractions interfaces that are independent of the underlying data sources.

Here is an example:

You can define a simple library to insert, update, delete and retrieve to-do items without having to write a lot of code.

 

Question 14

 

What is spring data rest?

Spring data test can be used to publish HATEOAS restful resources about spring database.

Here is an example of using JPA:

Instead of writing too much code, we can publish restful APIs for spring databases.

Here are some examples of the test server

POST:

  • URL:http://localhost:8080/todos

  • Use Header:Content-Type:Type:application/json

  • Request Content

The code is as follows:

Response content:

The response contains the new created resource.

 

Question 15

 

How can path = “users”, collectionresourcerel = “users” be used with spring data rest?

  • Path – the path segment to export for this resource.

  • Collectionresourcerel – the rel value to use when generating links to collection resources. Used when generating HATEOAS links.

 

Question 16

 

What happens in the background when a spring boot application runs as a Java application?

If you use the eclipse IDE, the eclipse Maven plug-in ensures that changes to dependency or class files are compiled and ready in the target file as soon as they are added! After that, it’s like any other Java application.

When you start a Java application, the spring boot autoconfiguration file is magically enabled.

  • When the spring boot application detects that you are developing a web application, it starts Tomcat.

 

Question 17

 

Can we replace Tomcat with jetty in spring boot starter web?

Remove the existing dependencies in spring boot starter web and add the following.

Question 18

 

How to use spring boot to generate a war file?

Recommended reading:

  • https://spring.io/guides/gs/convert-jar-to-war/

The following is a direct link to the spring documentation:

  • https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#build-tool-plugins-maven-packaging

 

Question 19

 

How to use spring boot to deploy to different servers?

You need to do the following two steps:

  • Generate a war file in a project.

  • Deploy it to your favorite server (WebSphere or Weblogic or Tomcat and so on).

Step 1: this getting started guide should help:
https://spring.io/guides/gs/convert-jar-to-war/

Step 2: it depends on your server.

 

Question 20

 

What are the differences between requestmapping and getmapping?

  • If the requestmapping has class properties, it can be used to get, post, put or other request methods in comments.

  • Getmapping is a special case of get request method. It’s just an extension of requestmapping to improve clarity.

 

Question 21

 

Why don’t we recommend using spring data rest in a real application?

We think spring data rest is very suitable for rapid prototyping! Use with caution in large applications.

With spring data rest, you can publish your data entities directly as restful services.

When you design restful servers, best practices show that your interface should consider two important things:

  • Your model range.

  • Your clients.

With spring data rest, you don’t need to consider these two aspects any more, you just need to be the test service publishing entity.

That’s why we recommend using spring data rest on rapid prototyping, or as an initial solution to the project. This is not a good idea for a complete evolution project.

 

Question 22

 

In spring initializer, how to change the package name of a project?

The good news is that you can customize it. Click on the link “go to full version”. You can configure the package name you want to modify!

 

Question 23

 

Can be configured application.propertierde Where can I find a complete list of properties for?

Here is a complete guide:

  • https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html

 

Question 24

 

What are the differences between JPA and Hibernate?

In short

  • JPA is a specification or interface

  • Hibernate is an implementation of JPA

When we use JPA, we use javax.persistence You do not need to use hibernate’s import package for comments and interfaces in the package.

We recommend using JPA annotations because, oh, we didn’t bind it to hibernate as an implementation. Later (I know – less than one percent chance), we could use another JPA implementation.

 

Question 25

 

What level should business boundaries start from?

We recommend managing obligations at the service level. Business business logic is in the business layer or service layer, and at the same time, the business management you want to perform is in that layer.

 

Question 26

 

What dependencies do you need to start a JPA application connected to the in memory database H2 using spring boot?

In the spring boot project, you can load the H2 console when you make sure that the following dependencies are in the class path.

  • Web starter

  • h2

  • JPA data starter

Other dependencies are as follows:

Some points need to be noted:

  • An internal data memory exists only during application execution. This is an effective way to learn frameworks.

  • It’s not the way you want it to be really a world app.

  • In the question “how do I connect to an external database?” We explained how to connect to a database of your choice.

 

Question 27

 

How to choose hibernate as the default implementation of JPA without any configuration?

Because spring boot is automatically configured.

Here are the dependencies we added:

Spring boot state data JPA has a transitional dependency on Hibernate and JPA.

When spring boot detects hibernate in the classpath, it will be automatically configured as the default JPA implementation.

 

Question 28

 

Where is the specified database connection information? How does it know to automatically connect to H2?

This is the magic of spring boot auto configuration.

come from: https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-auto-configuration.html

Spring boot auto configuration attempts to automatically configure spring applications that you have added based on jar dependencies. For example, if hsqldbis exists in your classpath and the database connection bean has not been configured manually, then we can automatically configure an in memory database.

Further reading:

http://www.springboottutorial.com/spring-boot-auto-configuration

 

Question 29

 

How do we connect to an external database like MySQL or oracle?

Let’s take MySQL as an example

Step 1 – add the dependency of the MySQL connector to the pom.xml

Step 2 – from pom.xml Remove H2 dependency from

Or at least use it as the scope of the test.

Step 3 – install your MySQL database

More here- https://github.com/in28minutes/jpa-with-hibernate#installing -and-setting-up-mysql

Step 4 – configure your MySQL database connection

to configure application.properties

spring.jpa.hibernate.ddl-auto=none spring.datasource.url=jdbc:mysql://localhost:3306/todo_example spring.datasource.username=todouser spring.datasource.password=YOUR_PASSWORD  

Step 5 – restart and you’re ready!

That’s it!

 

Question 30

 

What is the name of the default H2 database configured by spring boot? Why is the default database name testdb?

In application.properties It lists all the default values

  • https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html

Find the following properties

# Name of the datasource.

spring.datasource.name=testdb 

If you use the H2 internal storage database, it identifies the name of the H2 database that spring boot will use to install your H2 database.

 

Thirty one questions

 

If H2 is not in the classpath, what happens?

The following error will be reported

Cannot determine embedded database driver class for database type NONE

Add H2 to pom.xml Then restart your server

 

Question 32

 

Can you give an example of transaction management with readonly?

  • When you read content from a database, you want to set user descriptions or other descriptions in things to read-only mode, so that hibernate does not need to check the changes of entities again. It’s very efficient.

 

Question 33

 

What is the best way to publish custom configuration for spring boot user applications?

@The problem with value is that you can assign your configuration values through the application. A better approach is to take a centralized approach.
You can define a configuration component using @ configurationproperties.

You can application.properties Configuration parameters in.

basic.value: true 

basic.message: Dynamic Message 

basic.number: 100

 

Question 34

 

What are the requirements for configuration files?

Enterprise application development is complex and you need a mixed environment:

  • Dev

  • QA

  • Stage

  • Production

In each environment, you want different application configurations.

Configuration files help you configure applications differently in different environments.

Spring and spring boot provide functions that you can develop.

  • What is the configuration of different environments in different configuration files?

  • Set the active profile for a specified environment.

Spring boot will select the configuration of the application based on the active profile set up in the specific environment.

Question 35

 

How to use the configuration file to configure the configuration of a specific environment through spring boot?

Configuration files are not the key to the configuration environment.

In the following example, we will use two configuration files

  • dev

  • prod

The default application configuration is in the application.properties Medium. Let’s take a look at the following examples:

application.properties

basic.value= true 

basic.message= Dynamic Message 

basic.number= 100

We want to customize for the dev file application.properties Property. We need to create an application called application- dev.properties And override the properties we want to customize.

application-dev.properties

basic.message: Dynamic Message in DEV

Once you have specific profiles configured, you need to set an active profile in the environment.

There are many ways to do this:

    • Use in VM parameters Dspring.profiles.active=prod

    • In application.properties Use in spring.profiles.active=prod

    • 1. What is springboot
      It is used to simplify the initial construction and development process of spring applications, and use specific methods to configure (properties or YML files)
      Create a separate spring reference program to run the main method
      Embedded Tomcat does not need to deploy war files
      Simplify Maven configuration
      Automatic configuration spring add corresponding function starter automatic configuration
      A: spring boot is used to simplify the development of spring applications. Conventions are greater than configuration, and the complexity is simplified. Just run can create an independent, product level application

      2、What are the advantages of spring boot? 
      A: – quickly create independently running spring projects to integrate with mainstream frameworks
      -Using the embedded servlet container, the application does not need to be packaged into a war package
      -Starters automatic dependency and version control
      -A large number of automatic configuration, simplify development, can also modify the default value
      -Application monitoring of quasi production environment
      -Natural integration with cloud computing

      3. How do I reload changes on spring boot without having to restart the server? 
      This can be done using the dev tool. With this dependency, you can save any changes and the embedded Tomcat will restart.  
      Spring boot has a devtools module that helps improve developer productivity. A major challenge for Java developers is to automatically deploy file changes to the server and restart the server automatically.  
      Developers can reload changes on spring boot without having to restart the server. This eliminates the need for each manual deployment change. Spring boot did not have this feature when it was released in its first version.  
      This is what developers need most. The devtools module fully meets the needs of developers. The module will be disabled in the production environment. It also provides an H2 database console to better test the application.  

      org.springframework.boot 
      spring-boot-devtools 
      true 
      4. What are the monitors in spring boot? 
      Spring boot actor is one of the important functions of spring startup framework. The spring boot monitor helps you access the current state of running applications in a production environment.  
      There are several indicators that must be checked and monitored in the production environment. Even some external applications may be using these services to trigger alert messages to relevant people. The monitor module exposes a set of rest endpoints that can be accessed directly as HTTP URLs to check status.

      5. What is yaml? 
      Yaml is a human readable data serialization language. It is usually used for configuration files.  
      Compared with property files, yaml files are more structured and less confusing if we want to add complex properties to configuration files. You can see that yaml has hierarchical configuration data.

      6. Principle of automatic configuration of springboot
      Add @ springbootapplication or @ enableautoconfiguration to the main method of spring program
      It will automatically go to Maven to read the spring.factories This file is configured with all the beans that need to be created in the spring container

      7. How springboot reads configuration files
      The default read configuration file of springboot is application.properties Or is it application.yml

      8. The process of integrating mybatis in springboot 
      Add starter Maven dependency for mybatis

      org.mybatis.spring.boot 
      mybatis-spring-boot-starter 
      1.2.0 

      Add @ mapper annotation to mybatis interface
      stay application.yml Configure data source information

      9. What are the core annotations for spring boot? What kind of annotations does it consist of?

      The annotation above the startup class is @ springbootapplication, which is also the core annotation of spring boot. The main combination includes the following three annotations:

      @Springbootconfiguration: combines the @ configuration annotation to realize the function of configuration file.

      @Enable autoconfiguration: turn on the auto configuration function or turn off an auto configuration option. For example, turn off the data source auto configuration function: @ springbootapplication (exclude = {datasou rceAutoConfiguration.class })。

      @Componentscan: spring component scan.

      10. What are the ways to turn on the spring boot feature?

      1) Inherit the spring boot starter parent project

      2) Import spring boot dependencies project dependencies

      11. Does spring boot need to run in a separate container?

      No need. There are built-in containers such as Tomcat / jetty.

      12. How many ways to run spring boot?

      1) Packing is run with commands or in containers

      2) Running with Maven / gradle plug-in

      3) Run the main method directly

      13. How do you understand starters in spring boot?

      Starters can be understood as starters. It contains a series of dependency packages that can be integrated into the application. You can integrate spring and other technologies in a one-stop manner without looking for sample code and dependency packages everywhere. If you want to use spring JPA to access the database, just add the spring boot starter data JPA starter dependency.

      14. What logging frameworks does spring boot support? What is the recommended and default logging framework?

      Spring boot supports Java util logging, log4j2 and lockback as logging framework. If you use starters launcher, spring boot will use logback as the default logging framework

      15. How many ways does springboot implement hot deployment?

      There are mainly two ways:

      • Spring Loaded
      • Spring-boot-devtools