Spring Boot interview questions (rpm)

Spring Boot is the best Java micro-services framework. We suggest that you can become an expert in a Spring Boot.

Issue a  Spring Boot, Spring MVC and Spring What is the difference?

SpringFrame

The most important feature is SpringFramework dependency injection. Dependency injection is not all SpringModules IOC inversion of control.

When we properly use DI or the IOC, we can develop loosely coupled applications. Loosely coupled application unit testing can be easily performed.

SpringMVC

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

SpringBoot

Spring and SpringMVC problem is that you need to configure a number of parameters.

<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/"/>

Spring Boot entry by automatically configure and start a project to solve the problem. In order to quickly build application-ready products, Spring Boot provides some non-functional characteristics.

 

Second problem  What is auto configuration?

Spring and SpringMVC problem is that you need to configure a number of parameters.

<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/"/>

Can we bring more intelligence? When a MVC JAR added to the application, we can automatically configure some beans?

See Spring (available on the CLASSPATH frame) configuration of existing applications. On this basis, Spring Boot Configuration provides the basic framework and the application required. This is automatically configured.

 

Question three  What is Spring Boot Stater?

Initiator is a convenient descriptor is not dependent, it can be placed in their own procedures. You can get the related art Spring and you need to stop, by way of example without relying on code search descriptors and paste the copied load.

For example, if you want to use Sping and JPA access the database , only need your project consists of spring-boot-starter-data- jpa dependencies, you can be perfect.

Question four  Can you give an example to explain the contents of more Staters?

Let us consider an example of a Stater -Spring Boot Stater Web.

If you want to develop an application or a web application is open REST services. Spring Boot Start Web is preferred. Let's create a Spring Boot Start Web project quickly using Spring Initializr.

Spring Boot Start Web dependencies

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
</dependency>

The following screenshot is added to our application of different dependencies

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 applications will use all of these dependencies. Spring Boot Starter Web prepackaged these dependencies.

As a developer, I do not need to worry about these dependencies and their compatible version.

 

Five questions  Spring Boot which also offers other Starter Project Options?

Spring Boot also offers other items comprises a launcher, typically including dependencies for developing specific types of applications.

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

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

spring-boot-starter-test - unit and integration test

spring-boot-starter-jdbc - traditional JDBC

spring-boot-starter-hateoas - added functionality for service HATEOAS

spring-boot-starter-security - SpringSecurity use for authentication and authorization

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

spring-boot-starter-data-rest - simply use the Spring Data REST REST service announcement

 

Question six  Spring is how to quickly create product-ready applications?

Spring Boot committed to rapid product-ready applications. For this purpose, it provides a non-functional characteristics such as cache, logging, monitoring, and embedded servers out of the box.

spring-boot-starter-actuator - such as monitoring and tracking the use of some of the advanced features of the application

spring-boot-starter-undertow, spring-boot-starter-jetty, spring-boot-starter-tomcat - Choose your specific Embedded Servlet Container

spring-boot-starter-logging - logging using logback

spring-boot-starter-cache - the cache is enabled support for the Spring Framework

What are the minimum Java version ### Spring2 and Spring5 need is?

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

Recommended reading:

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

 

Seven questions  to create a Spring What is the easiest way to Boot Project is?

Spring Initializr start Spring Boot Projects is a good tool.

  • Like the figure above shows, we need to do some steps:

  • Log Spring Initializr, selected in the following manner:

  • Select the group com.in28minutes.springboot

  • Select studet-services component is

  • Select the following dependencies

  • Web

  • Actuator

  • DevTools

  • Click raw GenerateProject

  • Import project into Eclipse. File - Import - existing Maven project

 

Question eight  Spring Initializr is the only way to create a Spring Boot Projects do?

no.

Spring Initiatlizr make creating Spring Boot project becomes very easy, but you can also set up a maven project and add the correct dependencies to start a project.

Spring in our courses, we use two methods to create a project.

The first method is start.spring.io.
Another method is to manually set to "Basic Web Application" in the title of the project.

Manually set a maven project

Here are a few important steps:

  • In Eclipse, use the File - New Project to create a new Maven project

  • Add dependencies.

  • Add maven plugin.

  • Add Spring Boot application class.

Here, the preparatory work already done!

 

Nine on  why we need spring-boot-maven-plugin?

spring-boot-maven-plugin package provides a number of different jar or run applications like commands.

  • spring-boot: run run your SpringBoot the y-app.
  • spring-boot: repackage repackage your bag or jar is war make it executable package

  • spring-boot: start and spring-boot: Lifecycle Management Spring Boot stop application (also can be said for integration testing).

  • spring-boot: build-info actuator generating construction information may be used.

 

Question 10  How to use SpringBoot auto-reload my application?

Use Spring Boot development tools.

Add into your project is simple Spring Boot development tools.

Add the following dependency to your Spring Boot Project pom.xml in

<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-devtools</artifactId>
     <scope>runtime</scope>
</dependency>

Restart the application, then it.

Similarly, if you want to automatically load a page, you can have a look at FiveReload
  • http://www.logicbig.com/tutorials/spring-framework/spring-boot/boot-live-reload/.

When I tested and found LiveReload loophole, if you test also found, please be sure to let us know.

 

Question 11  What is an embedded server? Why do we want to use the embedded server?

Consider deploying an application on a virtual machine what your needs.

Step 1: Install Java

Part II: Installing the Web or application server (Tomat / Wbesphere / Weblogic, etc.)

Part III: Deploying the application war package

If we want to simplify these steps, how?

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

You only need one of the Java virtual machine installed, you can deploy applications directly on top of,
is not it cool?

The idea is the origin of the embedded server.

When we create an application that can be deployed, we will put a server (eg, tomcat) embedded server deployment.

For example, for a Spring Boot application, you can build an application that contains jar Embedded Tomcat. You may want to run a normal Java application to run the web application.

Our server is embedded executable binary file that contains the server unit (for example, tomcat.jar).

 

Problem 12  how to add a generic JS code in Spring Boot in?

In the source folder, create a file called static folder. Then you can put your static content on the inside.

For example, the path is myapp.js resources \ static \ js \ myapp.js

You can refer to it in the jsp use of

<csript src="/js/myapp.js"></script>

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

How to fix this mistake?

{
  "timestamp": 1488656019562,
  "status": 401,
  "error": "Unauthorized",
  "message": "Full authentication is required to access this resource.",
  "path": "/beans"
}

Two ways:

Method 1: Close Security verification

application.properties

management.security.enabled:FALSE
Method two: the search for the password in the log and delivered to the request headers

 

Question thirteen  What is Spring Date?

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

Spring Data's mission is to ensure that the premise of the special nature of the underlying data store, providing a familiar data access, consistency, Spring-based programming model. This makes the use of data access technology, relational databases and non-relational databases, map-reduce framework and cloud-based data services become very easy.

To make it simpler, Spring Data Abstractions provides an interface from the underlying data source limitation.

Here an example to

interface TodoRepository extends CrudRepository<Todo, Long> {

You can define a simple library to insert, update, delete, and retrieve agency matters without the need to write a lot of code.

 

Problem fourteen  What is Spring Data REST?

Spring Data TEST can be used to publish HATEOAS RESTful resources on Spring database.

Here is an example of the use of JPA

@RepositoryRestResource(collectionResourceRel = "todos", path = "todos")
public interface TodoRepository
        extends PagingAndSortingRepository<Todo, Long> {

You do not need to write too much code, we can publish RESTful API on Spring database.

Here are some examples of display on the server TEST

POST

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

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

  • Request Content

code show as below

{
  "user": "Jill",
  "desc": "Learn Hibernate",
  "done": false
}

Response content

{
  "user": "Jill",
  "desc": "Learn Hibernate",
  "done": false,
  "_links": {
"self": {
  "href": "http://localhost:8080/todos/1"
},
"todo": {
  "href": "http://localhost:8080/todos/1"
}
  }
}

Href response contains the newly created resource.

 

Question fifteen  path = "users", collectionResourceRel = "users" How to use with Spring Data Rest?

@RepositoryRestResource(collectionResourceRel = "users", path = "users")

public interface UserRestRepository extends
PagingAndSortingRepository<User, Long> 
  • path- this resource to be exported path segment.

  • collectionResourceRel- rel values ​​used to generate a link to a collection of resources. Used when generating HATEOAS link.

 

Question sixteen  when Spring Boot application runs as a Java application, what happens backstage?

If you use the Eclipse IDE, Eclipse maven plug-in ensures changed dependency or class files are added - will be compiled and ready in the target file! After that, you and other Java applications same.

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

  • When Spring Boot application detects that you're developing a web application when it starts tomcat.

 

The 17-  Can we jetty tomcat instead of the spring-boot-starter-web with?

Remove the existing dependency in spring-boot-starter-web, and these added to it below.

<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>

Eighteen questions  on how to use Spring Boot generate a WAR file?

Recommended reading:

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

Here are spring documentation directly link address:

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

 

Nineteen question  of how to use Spring Boot deployed 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 One: Getting Started This should help:
https://spring.io/guides/gs/convert-jar-to-war/

Step two: depending on your server.

 

The twenty  RequestMapping and differences GetMapping Where at?

  • RequestMapping class having attributes, and can be requested method GET, POST, PUT, or have other comments.

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

 

The twenty-first  why we do not recommend using Spring Data Rest in practical applications?

We believe that the Spring Data Rest is suitable for rapid prototyping! In large applications need to use caution.

You can put your data entities as RESTful services directly released by Spring Data REST.

When you design a RESTful server, best practices suggest that you should take into account the interface of two important things:

  • Your model range.

  • your customer.

By With Spring Data REST, you do not need to consider these two aspects, just as the issuing entity TEST service.

This is why we recommend using Spring Data Rest in rapid prototyping above, or as a method of solving the initial project. For a complete evolution of the project, this is not a good note.

 

Twenty-two problem  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 the full version." You can configure the package name you want to modify!

 

Twenty-three problem  can configure a complete list of properties application.propertierde can be found where?

Here is the complete guide:

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

 

Problem twenty-four  What are the differences JPA and Hibernate have?

in short

  • JPA is a specification or interface

  • Hibernate is an implementation of JPA

When we use the JPA, we use javax.persistence package annotations and interfaces without using hibernate import packages.

We recommend using the JPA annotations, oh because we do not have to bind it to Hibernate as the implementation. Later (I know - less than one percent chance), we can use another JPA implementation.

 

Problem twenty-five  business boundaries Where should one start?

We recommend the service layer management obligations. Commercial business logic in the business layer or service layer, at the same time, you want to perform management operations also in this layer.

 

Problem twenty-six  use Spring Boot initiate a connection to the JPA H2-memory database applications which require a dependency?

In Spring Boot project when you make sure the following items are dependent on the class path inside, you can load H2 console.

  • web launcher

  • h2

  • jpa data Launcher

Other dependencies in the following:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <scope>runtime</scope>
</dependency>

Some caveats:

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

  • This is not the way the world is really the application you want.

  • In the question "how to connect an external database?", We explain how to connect a database of your choice.

 

Twenty-seven question  of how not to choose Hibernate as the default JPA implementation by any configuration?

Because Spring Boot is automatically configured.

Here is our dependency added

<dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-data-jpa</artifactId>
      </dependency>

spring-boot-stater-data-jpa for Hibernate and JPA transitional dependent.

When Spring Boot detected classpath Hibernate, it will be automatically configured as the default JPA implementation.

 

Problem twenty-eight  where specified database connection information? How does it know to automatically connect to the H2?

This is the Spring Boot automatically configured magic.

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

Spring Boot auto-configuration attempt to automatically configure you have added Spring-based application jar dependencies. For example, if there is HSQLDBis your class path, and database connection manually configure the bean has not, then we can automatically configure a memory database.

Further reading:

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

 

Twenty-nine question  of how we connect a like MSSQL or orcale same external database?

Let us take MySQL to think about this as an example:

Step - adding dependency mysql connector to pom.xml
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>
The second step - removing H2 from the pom.xml dependency

Or at least use it as the test range.

<!--
<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
-->   
The third step - installing your MySQL database

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

Step Four - Configure your MySQL database connection

Configuration 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, you're ready!

It's that simple!

 

Questions on the  default H2 database name Spring Boot configuration is the above? Why is the default database name is testdb?

In application.properties which lists all of the default values

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

Locate the following attributes

spring.datasource.name=testdb # Name of the datasource.

If you use an H2 internal memory database, which it identifies Spring Boot H2 to install the name of your database.

 

Thirty-one problem  if H2 is not in the class path which will appear above situation?

We will report the following error

Cannot determine embedded database driver class for database type NONE

H2 added to the pom.xml, then restart your server

<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>

 

Problem thirty-two  Can you give an example to ReadOnly as a transaction management?

  • When you read the content from the database, things you want other users described or described in read-only mode, so that no change in the entity to check again in Hebernate. This is very efficient.

 

Question thirty-three  What is the best way to publish Spring Boot application user custom configuration is?

@Value problem is that you can assign values to configure your application. Better operation is to take centralized approach.
You can use @ConfigurationProperties define a configuration components.

@Component
@ConfigurationProperties("basic")
public class BasicConfiguration {
    private boolean value;
    private String message;
    private int number;

You can configure the parameters in application.properties.

basic.value: true
basic.message: Dynamic Message
basic.number: 100

 

Question thirty-four  What is the demand profile?

Developing enterprise applications are complex, you need a mixed environment:

  • Dev

  • QA

  • Stage

  • Production

In each environment, you want a different application configuration.

Profile helps you configure the different applications in different environments.

Spring and Spring Boot provides functions that you can develop.

  • Different configuration files, configuring different environment is what?

  • Environmental set of activities to develop a profile.

Spring Boot configure the application will be selected according to the active profile specific environment settings.

 

Question thirty-five  how to use the configuration file to configure specific configuration environment through Spring Boot?

Profiles do not critical environmental design.

In the following example, we will use two configuration files

  • dev

  • prod

The default application configuration in application.properties in. Let us look at the following example:

application.properties

basic.value= true
basic.message= Dynamic Message
basic.number= 100

We want to dev file application.properties custom attributes. We need to create a file named application-dev.properties, and we want to rewrite the custom attributes.

application-dev.properties

basic.message: Dynamic Message in DEV

Once you have configured the specific configuration files, you need to set a profile of activity in the environment.

There are several ways to do this:

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

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

Source:  http://www.3xmq.com/article/1522809264295

[Programmer interview] is the IT job artifact, providing massive C ++, JAVA, front-end and other professional written exam, conducted online first-line Internet famous enterprises written examination and interview Exam practice every day to brush the question, and cattle were discussing classic questions together, to enhance your programming ability, massive high-paying jobs waiting for you! Always ready to leave for the next month! Annual salary of millions of farmers have been concerned about the code!

www.wityx.com

Guess you like

Origin www.cnblogs.com/w4ctech/p/11754026.html