This article has participated in the third phase of the Gold Digger Creators Camp. For details, see:The third phase of the Diggability Program | Creators’ camp is underway to “write” about your personal impact.

preface

Holding fireworks for a living and poetry for love

I wish the boys all over the world face no blain blain, hair no oil, king does not drop star, eat chicken is not killed, alliance take S, through all nod, have a super warm, super listen, super good girlfriend.

Today, the blogger will share 35 SpringBoot interview questions and their answers. If you don’t like it, don’t spray it.

Spring BootIt’s the best of microservicesJavaWe suggest that you can become aSpring BootThe experts. This article has selected 35 common onesSpring BootKnowledge points, I wish you a helping hand!


Problem a:

What are the differences between Spring Boot, Spring MVC, and Spring?
  1. Spring

The most important feature of Spring is dependency injection. All SpringModules are either dependency injection or IOC inversion. When we use DI or IOC properly, we can develop loosely coupled applications. Unit testing of loosely coupled applications can be done easily.

  1. Spring MVC

Spring MVC provides a separate approach to developing Web applications. Using simple concepts like DispatcherServet, MoudlAndView, and ViewResolver makes developing Web applications much easier.

  1. SpringBoot

The problem with Spring and SpringMVC is that you need to configure a lot of parameters. 支那Spring Boot addresses this problem with an automatic configuration and startup item. To build production-ready applications faster, Spring Boot provides a number of nonfunctional features.


Problem two:

What is autoconfiguration?

The problem with Spring and SpringMVC is that you need to configure a lot of parameters.

Can we bring more intelligence? Can we automatically configure some beans when an MVC JAR is added to the application?

Spring looks at the configuration of an existing application (a framework available on the CLASSPATH). On top of that, Spring Boot provides the basic configuration needed to configure the application and framework. This is auto-configuration.


Question 3:

What is autoconfiguration?

What is Spring Boot Stater?

Initiators are a convenient set of dependency descriptors that can be placed in your own program. You can get all the Spring and related technologies you need in one stop shop without the load of searching and copying through sample code that relies on descriptors.

For example, if you want to use Sping and JPA to access databases, you can do it perfectly if your project includes the Spring-boot-starter – data-JPA dependencies.


Problem four:

Can you give an example to explain more about Staters?

Let’s consider a Stater example -Spring Boot Stater Web.

If you want to develop a Web application or an application that exposes REST services. Spring Boot Start Web is preferred. Let’s use Spring Initializr to create a quick project for Spring Boot Start Web.

Spring Boot Start Web dependencies.

Below is a screenshot of 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 would use all of these dependencies. The Spring Boot Starter Web prepackaged these dependencies.

As a developer, I no longer need to worry about these dependencies and their compatible versions.


Question 5:

What other Starter Project Options does Spring Boot provide?
  • Spring-boot-starter – web-services-SOAP Web services;

  • Spring-boot-starter – Web-Web and RESTful applications;

  • Spring-boot-starter – test-unit and integration tests;

  • Spring-boot-starter – JDBC – traditional JDBC;

  • Spring-boot-starter -hateoas – adds hateoas functionality to the service;

  • Spring-boot-starter -security – Use SpringSecurity for authentication and authorization.

  • Spring-boot-starter -data- jPA – Spring data jPA with Hibeernate;

  • Spring-boot-starter -data-rest – Use spring Data REST to expose simple REST services.


Question 6:

How does Spring quickly create production-ready applications?

Spring Boot is dedicated to fast production-ready applications. To this end, it provides non-functional features such as caching, logging, monitoring, and embedded servers out of the box.

  • Spring-boot-starter – Uses advanced functions 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 – Logs using the logback

  • Spring-boot-starter -cache – Enables the caching support of the Spring Framework

  • What is the minimum Java version required for Spring2 and Spring5?

  • Spring Boot 2.0 requires Java8 or later. Java6 and Java7 are no longer supported.

Recommended Reading:

Github.com/spring-proj…


Question 7:

What’s the easiest way to create a Spring Boot Project?

Spring Initializr is a great tool for starting Spring Boot Projects.

  • Log in to Spring Initializr and perform the following operations:

  • Select com. In28minutes. Springboot for groups

  • Select studet-services as the component

  • Select the following dependencies

  • Web

  • Actuator

  • DevTools

  • Click on the raw GenerateProject

  • Import the project into Eclipse. File – Import – existing Maven project


Question 8:

Is Spring Initializr the only way to create Spring Boot Projects?

Spring Initiatlizr 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 use two approaches to creating projects.

The first is start.spring. IO.

The alternative is to set it up manually in the project title “Basic Web Application.”

Manually set up a Maven project

There are several important steps:

  • In Eclipse, use file-New Maven project to create a new project

  • Add dependencies.

  • Add maven plugin.

  • Add a Spring Boot application class.

At this point, the preparations have been made!


Question 9:

Why do we needspring-boot-maven-plugin ?

spring-boot-maven-pluginSeveral commands are provided to package or run the application like a JAR.

  • Spring-boot :run Runs your SpringBooty application.

  • Spring-boot: repackage to repackage your JAR or WAR package to make it executable

  • Spring-boot: Start and Spring-boot: Stop manage the life cycle of spring Boot applications (in other words, for integration testing).

  • Spring-boot :build-info Generates build information that can be used by the executor.


Question 10:

How do I use SpringBoot to automatically reinstall my application?

Use Spring Boot development tools.

Adding Spring Boot development tools to your project is easy.

Add the following dependencies to your Spring Boot Project pom.xml

Restart the application, and you’re good to go.

Also, if you want to load pages automatically, check out FiveReload

www.logicbig.com/tutorials/s….

In my testing, I found the LiveReload bug. If you find it in your testing, please let us know.


Question 11:

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

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 three: Deploy the application WAR package

If we want to simplify these steps, how do we do it?

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

Wouldn’t it be nice if you just had a Virtual machine with Java installed and you could deploy your applications on it?

This idea was the origin of the embedded server.

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 that includes Embedded Tomcat. You can run your Web application just like you would a normal Java application.

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


Question 12:

How to add generic JS code to Spring Boot?

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

For example, the myapp.js path is resources\static\js\myapp.js, and you can refer to how it is used in JSP:

HAL Browser gives me unauthorized error – Full authenticaition is required to access this resource. What can I do to fix this error?

Two ways:

  • Method 1: Disable security authentication

application.properties management.security.enabled:FALSE

  • Method 2: Search for the password in the log and pass it to the request header

Question 13:

What is Spring Data?

From: HTTPS / / projects. Spring. IO/spring – data /

The mission of Spring Data is to provide a familiar, consistent, Spring-based programming model for Data access without compromising the specificity of the underlying Data storage. This makes it easy to use data access technologies, relational and non-relational databases, the Mapreduce framework, and cloud-based data services.

To make it easier, Spring Data provides an Abstractions interface that is not constrained by the underlying Data source.

You can define a simple library for inserting, updating, deleting, and retrieving items without having to write a lot of code.


Question 14:

What is Spring Data REST?

Spring Data TESTCan be used to post aboutSpringThe databaseHATEOAS RESTfulResources.

Without writing much code, we can publish RESTful apis for the Spring database.

Some examples of the TEST server are shown below

POST:

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

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

  • Request Content


Question 15:

Path = “users”, collectionResourceRel= “users”

  • Path – The path segment of the resource to be exported.

  • CollectionResourceRel – Rel value used to generate links to collection resources. Used when generating HATEOAS links.


Question 16:

What happens in the background when a Spring Boot application is run as a Java application?

If you are using the Eclipse IDE, the Eclipse Maven plugin ensures that changes to dependencies or class files are compiled and ready in the object files as soon as they are added! After that, it’s just like any other Java application.

When you launch your Java application, the Spring Boot autoconfiguration file is magically enabled.

  • When the Spring Boot application detects that you are developing a Web application, it launches Tomcat.

Question 17:

Can we use Jetty instead of Tomcat in spring-boot-starter-web?

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


Question 18:

How to generate a WAR file using Spring Boot?

  • Recommended Reading:

  • Spring. IO/guides/gs/c…

  • The following is a direct link to the Spring documentation:

  • Docs. Spring. IO/spring – the boot…


Question 19:

How do I deploy to different servers using Spring Boot?

  • Generate a WAR file in a project.

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

Step 1: This how-to guide should help:

  • Spring. IO/guides/gs/c…

Step 2: It depends on your server.


Question 20:

What’s the difference between RequestMapping and GetMapping?

  • RequestMapping has class attributes that can be used to GET,POST,PUT, or any other request method in the annotation.

  • GetMapping is a special case of the GET request method. It’s just an extension of ResquestMapping to improve clarity.


Question 21:

Why don’t we recommend using Spring Data Rest in real applications?

We think Spring Data Rest is great for rapid prototyping! Use with caution in large applications.

With Spring Data REST you can publish your Data entities directly as RESTful services.

When designing RESTful servers, best practices suggest that your interfaces should take into account two important things:

  • Your model range.

  • Your client.

With Spring Data REST, you don’t have to worry about either of these things anymore, just publish as a TEST service entity.

This is why we recommend using Spring Data Rest for rapid prototyping, or as an initial solution for a project. This is not a good idea for a fully evolving project.


Question 22:

inSpring InitializerHow do I change the package name of a project?

The good news is you can customize it. Click the link to “Go to full version”. You can configure the package name you want to change!


Question 23:

Where can I find the complete list of properties where I can configure application.propertierde?

  • Docs. Spring. IO/spring – the boot…

Question 24:

What are the differences between JPA and Hibernate?

  • JPA is a specification or interface

  • Hibernate is an implementation of JPA

When we use JPA, we do not need to use hibernate’s import package when we use the annotations and interfaces in the Javax. persistence package.

We recommend using JPA annotations because we are not binding them to Hibernate as an implementation. Later (I know – less than one percent of the time), we can use another JPA implementation.


Question 25.

At what level should the business boundary begin?

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


Question 26:

What dependencies do I need to start a JPA application connected to in-memory database H2 using Spring Boot?

In a 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 initiator

Some points to note:

  • An internal data memory exists only for the duration of application execution. This is an effective way to learn about frameworks.

  • This is not the way you want real world applications to be.

  • In the question “How do I connect to an external database?” In, we explain how to connect to a database of your choice.


Question 27:

How do I choose Hibernate as the default implementation of JPA without any configuration?

Spring-boot-stater – data-JPA has a transitional dependency on Hibernate and JPA.

When Spring Boot detects Hibernate in the classpath, it will automatically configure it 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.

From:Docs. Spring. IO/spring – the boot…

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

  • Further reading: www.springboottutorial.com/spring-boot…

When Spring Boot detects Hibernate in the classpath, it will automatically configure it as the default JPA implementation.


Question 29:

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

Step 1 – Add the mysql connector dependencies to POM.xml

Step 2 – Remove H2 dependencies from POM.xml

Or at least consider it the scope of the test.

Step 3 – Install your MySQL database

  • More on this at github.com/in28minutes…

  • Further reading: www.springboottutorial.com/spring-boot…

Step 4 – Configure your MySQL database connection

Configure the application. The 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  
Copy the code

Step 5 – Reboot and you’re ready!


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, all the default values are listed

  • Docs. Spring. IO/spring – the boot…

Find the following properties

#Name of the datasource.

spring.datasource.name=testdb 
Copy the code

If you use an H2 internal storage database, it specifies the name Spring Boot will use to install your H2 database.


Question 31:

What happens if H2 is not in the classpath?

  • The following error will be reported

    Cannot determine embedded database driver class for database type NONE

Add H2 to pom.xml and restart your server


Question 32:

Can you give an example of using ReadOnly as transaction management?

  • When you read content from the database, you want to set the user description or other description in the item to read-only so that Hebernate doesn’t have to check again for entity changes. It’s very efficient.

Question 33:

What is the best way to publish custom configurations for Spring Boot user applications?

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

You can configure the parameters in application.properties.

basic.value: true 

basic.message: Dynamic Message 

basic.number: 100
Copy the code

Question 34:

What are the requirements for the profile?

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

  • Dev

  • QA

  • Stage

  • Production

In each environment, you want a different application configuration.

Spring and Spring Boot provide functionality that you can customize.

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

  • Set up the active profile for a specified environment.

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


Question 35:

How do I use configuration files to configure environment-specific configurations through Spring Boot?

Configuration files are not the key to setting different environments.

In the following example, we will use two configuration files

  • dev

  • prod

The default application configuration is in application.properties. Let’s look at the following example:

application.properties

basic.value= true 

basic.message= Dynamic Message 

basic.number= 100
Copy the code

We want to customize the application.properties property for the dev file. We need to create a file called application-dev.properties and override the properties we want to customize.

application-dev.properties

basic.message: Dynamic Message in DEV
Copy the code

Once you have configured the profile specifically, you need to set up an active profile in the environment.

There are several ways to do this:

  • Use dspring.profiles. Active =prod in the VM parameters

  • Use spring.profiles. Active =prod in application.properties


Thirty-five SpringBoot interview questions and their answers are shared here!


🎉 finally

  • For more references, see here:Chen Yongjia’s blog
  • Like the little friend of the blogger can add a concern, point a like oh, continue to update hey hey!