Summary of java interview questions (7)-The most comprehensive Spring Boot interview questions in history (including answers) After reading it, you will be a Spring Boot expert!

Original link

table of Contents

1. What is the difference between Spring Boot, Spring MVC and Spring?

2. What is automatic configuration?

3. What is Spring Boot Stater?

4. Can you give an example to explain more of the contents of Statusers?

5. What other Starter Project Options does Spring Boot provide?

6. How does Spring quickly create product-ready applications?

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

7. What is the easiest way to create a Spring Boot Project?

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

Nine. Why do we need spring-boot-maven-plugin?

10. How to use SpringBoot to automatically reinstall my application?

11. What is an embedded server? Why should we use an embedded server?

12. How to add general JS code in Spring Boot?

13. What is Spring Date?

14. What is Spring Data REST?

15. How to use path=”users”, collectionResourceRel=”users” with Spring Data Rest?

16. When a Spring Boot application runs as a Java application, what happens in the background?

17. Can we use jetty instead of tomcat in spring-boot-starter-web?

18. How to use Spring Boot to deploy to different servers?

19. What is the difference between RequestMapping and GetMapping?

Twenty. Why do we not recommend using Spring Data Rest in actual applications?

Spring Boot extended interview questions

Interview questions finishing


The most complete Spring Boot interview questions (including answers) in history is a Spring Boot expert after reading it!

1. What is the difference between Spring Boot, Spring MVC and Spring?

SpringFrame

The most important feature of SpringFramework is dependency injection. All Spring Modules are either dependency injection or IOC inversion of control.

When we use DI or IOC appropriately, we can develop loosely coupled applications. Unit testing of loosely coupled applications can be easily performed.

SpringMVC

Spring MVC provides a separate method to develop Web applications. By using some simple concepts like DispatcherServelet, MoudlAndView and ViewResolver, the development of Web applications will become very simple.

SpringBoot

The problem with Spring and SpringMVC lies in the need to configure a large number of parameters.

<pre style="box-sizing: inherit; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; font-size: 13px; word-break: initial; word-wrap: initial; white-space: pre; overflow: auto; margin: 16px 0px 14px; padding: 14px 15px 12px; border-radius: 3px; border: none; display: block; line-height: 1.6; background: rgb(246, 246, 246); color: rgb(61, 70, 77); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"><bean
 class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
 <value>/WEB-INF/views/</value>
 </property>
 <property name="suffix">
 <value>.jsp</value>
 </property>
 </bean>
 <mvc:resources mapping="/webjars/**" location="/webjars/"/></pre>

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

2. What is automatic configuration?

The problem with Spring and SpringMVC lies in the need to configure a large number of parameters.

<pre style="box-sizing: inherit; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; font-size: 13px; word-break: initial; word-wrap: initial; white-space: pre; overflow: auto; margin: 16px 0px 14px; padding: 14px 15px 12px; border-radius: 3px; border: none; display: block; line-height: 1.6; background: rgb(246, 246, 246); color: rgb(61, 70, 77); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"><bean
 class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
 <value>/WEB-INF/views/</value>
 </property>
 <property name="suffix">
 <value>.jsp</value>
 </property>
 </bean>
 <mvc:resources mapping="/webjars/**" location="/webjars/"/></pre>

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 the existing application (framework available on the CLASSPATH). On this basis, Spring Boot provides the basic configuration required to configure applications and frameworks. This is automatic configuration.

3. What is Spring Boot Stater?

The launcher is a convenient set of dependencies and descriptors, which can be placed in your own program. You can get the Spring and related technologies you need in a one-stop shop, instead of relying on descriptors to search and copy and paste the load through sample codes.

For example, if you want to use Sping and JPA to access the database, you only need to include the spring-boot-starter-data-jpa dependency in your project, and you can proceed perfectly.

4. Can you give an example to explain more of the contents of Statusers?

Let us consider an example of Status-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 Initializr to create a quick project for Spring Boot Start Web.

Spring Boot Start Web dependencies

<pre style="box-sizing: inherit; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; font-size: 13px; word-break: initial; word-wrap: initial; white-space: pre; overflow: auto; margin: 16px 0px 14px; padding: 14px 15px 12px; border-radius: 3px; border: none; display: block; line-height: 1.6; background: rgb(246, 246, 246); color: rgb(61, 70, 77); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"><dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-web</artifactId>
</dependency></pre>

The screenshot below is the different dependencies added to our application

The most complete Spring Boot interview questions (including answers) in history is a Spring Boot expert after reading it!

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 will use all these dependencies. Spring Boot Starter Web pre-packages these dependencies.

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

5. What other Starter Project Options does Spring Boot provide?

Spring Boot also provides other starter projects, including typical dependencies used to develop specific types of applications.

spring-boot-starter-web-services - SOAP Web Services

spring-boot-starter-web-Web and RESTful applications

spring-boot-starter-test-Unit testing and integration testing

spring-boot-starter-jdbc-Traditional JDBC

spring-boot-starter-hateoas-Add HATEOAS functionality to the service

spring-boot-starter-security-Use Spring Security for authentication and authorization

spring-boot-starter-data-jpa - 带有 Hibeernate 的 Spring Data JPA

spring-boot-starter-data-rest-Use Spring Data REST to publish simple REST services

6. How does Spring quickly create product-ready applications?

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

spring-boot-starter-actuator-Use some advanced features such as monitoring and tracking applications

spring-boot-starter-undertow, spring-boot-starter-jetty, spring-boot-starter-tomcat-choose your specific embedded servlet container

spring-boot-starter-logging-Use logback for logging

spring-boot-starter-cache-Enable Spring Framework caching support

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

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

7. What is the easiest way to create a Spring Boot Project?

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

The most complete Spring Boot interview questions (including answers) in history is a Spring Boot expert after reading it!

  • As shown in the figure above, we need to do a few steps:
  • Log in to Spring Initializr and choose according to the following methods:
  • Select com.in28minutes.springboot as the group
  • Select student-services as the component
  • Select the dependencies below
  • Web
  • Actuator
  • DevTools
  • Click on Generate Project
  • Import the project into Eclipse. File-Import-Existing Maven Project

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

no.

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 correct dependencies.

In our Spring course, we use two methods to create projects.

The first method is start.spring.io.

Another method is to manually set the item titled "Basic Web Application".

Manually set up a maven project

There are a few important steps here:

  • In Eclipse, use File-New Maven Project to create a new project
  • Add dependencies.
  • Add the maven plugin.
  • Add the Spring Boot application class.

At this point, the preparations are done!

Nine. Why do we need spring-boot-maven-plugin?

spring-boot-maven-plugin provides some commands for packaging or running applications like jars.

  • spring-boot:run runs your Spring Booty application.
  • spring-boot: repackage to repackage your jar package or war package to make it executable
  • spring-boot: start and spring-boot: stop manage the life cycle of Spring Boot applications (or for integration testing).
  • spring-boot:build-info generates construction information that can be used by the actuator.

10. How to use SpringBoot to automatically reinstall my application?

Use Spring Boot development tools.

It is easy to add Spring Boot development tools into your project.

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

<pre style="box-sizing: inherit; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; font-size: 13px; word-break: initial; word-wrap: initial; white-space: pre; overflow: auto; margin: 16px 0px 14px; padding: 14px 15px 12px; border-radius: 3px; border: none; display: block; line-height: 1.6; background: rgb(246, 246, 246); color: rgb(61, 70, 77); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"><dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-devtools</artifactId>
 <scope>runtime</scope>
</dependency></pre>

Restart the application, and then you are good to go.

During my test, I found a LiveReload vulnerability. If you find it during the test, please let us know.

11. What is an embedded server? Why should we use an embedded server?

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

Step 1: Install Java

Part 2: Install Web or application server (Tomat/Wbesphere/Weblogic, etc.)

Part 3: Deploy the 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 only need a virtual machine with Java installed to 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 that contains Embedded Tomcat. You can run web applications like normal Java applications.

The embedded server is our executable unit containing the server's binary file (for example, tomcat.jar).

12. How to add general JS code in Spring Boot?

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

For example, the path of myapp.js is resourcesstaticjsmyapp.js

You can refer to how it is used in jsp

<pre style="box-sizing: inherit; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; font-size: 13px; word-break: initial; word-wrap: initial; white-space: pre; overflow: auto; margin: 16px 0px 14px; padding: 14px 15px 12px; border-radius: 3px; border: none; display: block; line-height: 1.6; background: rgb(246, 246, 246); color: rgb(61, 70, 77); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"><csript src="/js/myapp.js"></script></pre>

错误:HAL browser gives me unauthorized error - Full authenticaition is required to access this resource.

How can I fix this error?

<pre style="box-sizing: inherit; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; font-size: 13px; word-break: initial; word-wrap: initial; white-space: pre; overflow: auto; margin: 16px 0px 14px; padding: 14px 15px 12px; border-radius: 3px; border: none; display: block; line-height: 1.6; background: rgb(246, 246, 246); color: rgb(61, 70, 77); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">{
 "timestamp": 1488656019562,
 "status": 401,
 "error": "Unauthorized",
 "message": "Full authentication is required to access this resource.",
 "path": "/beans"
}</pre>
两种方法:

Method 1: Turn off security verification

application.properties

<pre style="box-sizing: inherit; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; font-size: 13px; word-break: initial; word-wrap: initial; white-space: pre; overflow: auto; margin: 16px 0px 14px; padding: 14px 15px 12px; border-radius: 3px; border: none; display: block; line-height: 1.6; background: rgb(246, 246, 246); color: rgb(61, 70, 77); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">management.security.enabled:FALSE</pre>

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

13. What is Spring Date?

The mission of Spring Data is to provide a familiar, consistent, and Spring-based programming model for data access while ensuring the particularity of the underlying data storage. This makes it easy to use data access technologies, relational and non-relational databases, map-reduce frameworks, and cloud-based data services.

To make it simpler, Spring Data provides Abstractions interface that is not restricted by the underlying data source.

Let's give an example

<pre style="box-sizing: inherit; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; font-size: 13px; word-break: initial; word-wrap: initial; white-space: pre; overflow: auto; margin: 16px 0px 14px; padding: 14px 15px 12px; border-radius: 3px; border: none; display: block; line-height: 1.6; background: rgb(246, 246, 246); color: rgb(61, 70, 77); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">interface TodoRepository extends CrudRepository<Todo, Long> {</pre>

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

14. What is Spring Data REST?

Spring Data TEST can be used to publish HATEOAS RESTful resources about Spring Database.

The following is an example using JPA

<pre style="box-sizing: inherit; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; font-size: 13px; word-break: initial; word-wrap: initial; white-space: pre; overflow: auto; margin: 16px 0px 14px; padding: 14px 15px 12px; border-radius: 3px; border: none; display: block; line-height: 1.6; background: rgb(246, 246, 246); color: rgb(61, 70, 77); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">@RepositoryRestResource(collectionResourceRel = "todos", path = "todos")
public interface TodoRepository
 extends PagingAndSortingRepository<Todo, Long> {</pre>

Without writing too much code, we can publish a RESTful API about Spring Database.

Shown below are some examples of TEST server

POST

  • URL: http : // localhost : 8080 / all
  • Use Header:Content-Type:Type:application/json
  • Request Content

code show as below

<pre style="box-sizing: inherit; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; font-size: 13px; word-break: initial; word-wrap: initial; white-space: pre; overflow: auto; margin: 16px 0px 14px; padding: 14px 15px 12px; border-radius: 3px; border: none; display: block; line-height: 1.6; background: rgb(246, 246, 246); color: rgb(61, 70, 77); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">{
 "user": "Jill",
 "desc": "Learn Hibernate",
 "done": false
}</pre>

Response content

<pre style="box-sizing: inherit; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; font-size: 13px; word-break: initial; word-wrap: initial; white-space: pre; overflow: auto; margin: 16px 0px 14px; padding: 14px 15px 12px; border-radius: 3px; border: none; display: block; line-height: 1.6; background: rgb(246, 246, 246); color: rgb(61, 70, 77); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">{
 "user": "Jill",
 "desc": "Learn Hibernate",
 "done": false,
 "_links": {
"self": {
 "href": "http://localhost:8080/todos/1"
},
"todo": {
 "href": "http://localhost:8080/todos/1"
}
 }
}</pre>

The response contains the href of the newly created resource.

15. How to use path=”users”, collectionResourceRel=”users” with Spring Data Rest?

<pre style="box-sizing: inherit; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; font-size: 13px; word-break: initial; word-wrap: initial; white-space: pre; overflow: auto; margin: 16px 0px 14px; padding: 14px 15px 12px; border-radius: 3px; border: none; display: block; line-height: 1.6; background: rgb(246, 246, 246); color: rgb(61, 70, 77); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">@RepositoryRestResource(collectionResourceRel = "users", path = "users")
public interface UserRestRepository extends
PagingAndSortingRepository<User, Long></pre>
  • path- The path segment to be exported from this resource.
  • collectionResourceRel- The rel value used when generating links to collection resources. Used when generating HATEOAS links.

16. When a Spring Boot application runs as a Java application, what happens in the background?

If you use Eclipse IDE, the Eclipse maven plug-in ensures that changes to dependencies or class files will be compiled and ready in the target file as soon as they are added! After this, it is the same as other Java applications.

When you start the java application, the spring boot automatic configuration file will be magically enabled.

  • When the Spring Boot application detects that you are developing a web application, it will start tomcat.

17. Can we use jetty instead of tomcat in spring-boot-starter-web?

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

<pre style="box-sizing: inherit; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; font-size: 13px; word-break: initial; word-wrap: initial; white-space: pre; overflow: auto; margin: 16px 0px 14px; padding: 14px 15px 12px; border-radius: 3px; border: none; display: block; line-height: 1.6; background: rgb(246, 246, 246); color: rgb(61, 70, 77); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"><dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-web</artifactId>
 <exclusions>
 <exclusion>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-tomcat</artifactId>
 </exclusion>
 </exclusions>
</dependency>
<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-jetty</artifactId>
</dependency></pre>

18. 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 2: Depends on your server.

19. What is the difference between RequestMapping and GetMapping?

  • If RequestMapping has class attributes, it can perform GET, POST, PUT or other request methods in comments.
  • GetMapping is a special case of the GET request method. It is just an extension of RequestMapping, the purpose is to improve clarity.

Twenty. Why do we not recommend using Spring Data Rest in actual applications?

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

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

When you design a RESTful server, best practices indicate that your interface should consider two important things:

  • The scope of your model.
  • your customer.

With With Spring Data REST, you don't need to consider these two aspects anymore, you only need to publish the entity as a TEST service.

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

Spring Boot extended interview questions

21. How to change the package name of a project in Spring Initializer?

22. What are the differences between JPA and Hibernate?

23. Where should the business boundary start?

24. What dependencies are needed to start a JPA application connected to the in-memory database H2 using Spring Boot?

25. How to choose Hibernate as the default implementation of JPA without any configuration?

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

27. How do we connect to an external database like MSSQL or orcale?

28. What is the name of the default H2 database configured by Spring Boot? Why is the default database name testdb?

29. If H2 is not in the classpath, the above situation will occur?

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

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

Thirty-two. What are the requirements for configuration files?

Thirty-three. How to configure the configuration of a specific environment through Spring Boot using a configuration file?

Interview questions finishing

Due to the length of the space, in order not to affect everyone's reading effect, not all the answers are given in the article. I have compiled it here in the form of files, and programmers who need to borrow can get it for free.

How to obtain interview documents:

If you need, you can comment and leave a message and receive interview materials

Collected other interview questions that you don’t know (springboot, mybatis, concurrency, java intermediate and advanced interview summary, etc.)

Guess you like

Origin blog.csdn.net/lsx2017/article/details/114005541