Spring Boot Interview Question 2

Question Thirteen

What is Spring Date?
From: //projects.spring.io/spring-data/

The mission of Spring Data is to provide a familiar, consistent, Spring-based programming model for data access while ensuring the specificity 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 a little easier, Spring Data provides an Abstractions interface that is not restricted by the underlying data source.

Here's an example

interface TodoRepository extends CrudRepository<Todo, Long> { Copy You can define a simple library for inserting, updating, deleting and retrieving to-do items without writing a lot of code.

Question Fourteen

What is Spring Data REST?
Spring Data TEST can be used to publish HATEOAS RESTful resources about Spring Database.

Here is an example using JPA

@RepositoryRestResource(collectionResourceRel = “todos”, path = “todos”)
public interface TodoRepository
extends PagingAndSortingRepository<Todo, Long> { Copy Without writing too much code, we can publish RESTful API about Spring database.

Shown below are some examples on the TEST server

POST
URL: http://localhost:8080/todos
Use Header: Content-Type: Type: application/json
Request Content
code is as follows

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





{ "user": "Jill", "desc": "Learn Hibernate", "done": false, "_links": { "self": { "href": "http://localhost:8080/todos/1 } , "todo": { "href": "http://localhost:8080/todos/1" } } } Copy The response contains the href of the newly created resource.













Question fifteen

How does path="users", collectionResourceRel="users" work with Spring Data Rest?
@RepositoryRestResource(collectionResourceRel = “users”, path = “users”) public interface UserRestRepository extends
PagingAndSortingRepository<User, Long>
Copy
path- the path segment to export from this resource.
collectionResourceRel - The rel value to use when generating links to collection resources. Used when generating HATEOAS links.
Question sixteen

What happens behind the scenes when a Spring Boot application runs as a Java application?
If you use the Eclipse IDE, the Eclipse maven plugin ensures that as soon as a dependency or class file change is added, it is compiled and ready in the target file! After that, it's like any other Java application.

When you start the java application, the spring boot auto-configuration file is magically enabled.

When the Spring Boot application detects that you are developing a web application, it starts tomcat.
Question seventeen

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.

org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-tomcat org.springframework.boot spring-boot-starter-jetty

How to generate a WAR file using Spring Boot?

Recommended reading:

https://spring.io/guides/gs/convert-jar-to-war/
There are direct links to spring documentation below:

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

How to deploy to different servers using Spring Boot?
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).
First step: this getting started guide should help:
https://spring.io/guides/gs/convert-jar-to-war/

Step Two: Depends on your server.

Question twenty

What is the difference between RequestMapping and GetMapping?
RequestMapping has class attributes and can perform GET, POST, PUT or other request methods in annotations.
GetMapping is a special case of the GET request method. It's just an extension of RequestMapping for clarity.
Question twenty one

Why do we not 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 you're designing a RESTful server, best practices suggest that your interface should take into account two important things:

your model scope.
your customer.
With Spring Data REST, you don't need to consider these two aspects anymore, you only need to publish entities as TEST services.

That's 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 full evolution project.

Question twenty two

How to change the package name of a project in Spring Initializer?
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 twenty three

Where can I find the complete list of properties that can configure application.propertierde?
Here is the complete guide:

https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.htmlQuestion
twenty-four

What is the difference between JPA and Hibernate?
in short

JPA is a specification or interface
Hibernate is an implementation of JPA
When we use JPA, we don't need to use hibernate import package when we use annotations and interfaces in javax.persistence package.

We recommend using JPA annotations because oh we don't have it tied to Hibernate as an implementation. Later (I know - less than one percent chance), we can use another JPA implementation.

Question twenty five

At which layer should the business boundary start?
We recommend managing obligations at the service layer. Business business logic is in the business layer or service layer, and at the same time, the business management you want to perform is also in this layer.

Question twenty six

What dependencies are required to start a JPA application connected to an in-memory database H2 using Spring Boot?
In a Spring Boot project, you can load the H2 console when you ensure that the following dependencies are in the classpath.

web enabler
h2
jpa data enabler
Other dependencies are below:

org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-data-jpa com.h2database h2 runtime Some things to pay attention to when copying:

An internal data memory exists only during application execution. This is an effective way to learn the framework.
This is not the way you want real world apps to work.
In the question "How to connect to an external database?", we explain how to connect to a database of your choice.
Question Twenty Seven

How can I choose Hibernate as the default implementation of JPA without any configuration?
Because Spring Boot is auto-configured.

Here are the dependencies we added

org.springframework.boot spring-boot-starter-data-jpa replicates spring-boot-stater-data-jpa Has transitional dependencies on Hibernate and JPA.

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

Question Twenty Eight

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: https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-auto-configuration.html

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

Further reading:

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

Question Twenty Nine

How do we connect to an external database like MSSQL or orcale?
Let's think about this using MySQL as an example:

Step 1 - Add mysql connector dependency to pom.xml

mysql
mysql-connector-java

Copy
Step 2 - Remove H2 dependency from pom.xml
or at least make it test scope.

Step 3 - Installing
your MySQL database
See more here - https://github.com/in28minutes/jpa-with-hibernate#installing-and-setting-up-mysql

Step 4 - Configure your MySQL database connection
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
Copy
Step 5 - Restart, You are ready to go!
It's that simple!

Question Thirty

What is the name of the default H2 database configured by Spring Boot? Why is the default database name testdb?
In application.properties, all default values ​​are listed

https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html
find the following properties

spring.datasource.name=testdb # Name of the datasource.
Copy
If you use the H2 internal storage database, it determines the name of the H2 database that Spring Boot uses to install you.

Question Thirty One

What happens if H2 is not in the classpath?
will report the following error

Cannot determine embedded database driver class for database type NONE
Copy
Add H2 to pom.xml and restart your server

com.h2database h2 runtime replication problem 32

Can you give an example with ReadOnly as transaction management?
When you read from the database, you want to set the user description or other description in the transaction to read-only mode, so that Hebernate does not need to check the entity changes again. This is very efficient.
Question Thirty Three

What's the best way to publish custom configuration for a Spring Boot user application?
The problem with @Value is that you can assign your configuration value through the application. A better course of action is to take a centralized approach.
You can define a configuration component using @ConfigurationProperties.

@Component
@ConfigurationProperties(“basic”)
public class BasicConfiguration { private boolean value; private String message; private int number; Copy You can configure parameters in application.properties.




basic.value: true
basic.message: Dynamic Message
basic.number: 100
Copy
Question 34

What is the need 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 facilitate different application configurations in different environments.

Spring and Spring Boot provide functionality that you can specify.

What is the configuration of different environments in different configuration files?
Sets the active profile for a specified environment.
Spring Boot will choose the configuration for the application based on the active profile set in the particular environment.

Question Thirty Five

How to configure environment-specific configuration with Spring Boot using configuration files?

The configuration file is not the key to setting up the environment.

In the following example, we will use two configuration files

The default application configuration of dev
prod is in application.properties.
Let's look at the following example:

application.properties

basic.value= true
basic.message= Dynamic Message
basic.number= 100
Copy
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
Once you have configured a profile specifically, you need to set an active profile in the environment.

There are multiple ways to do this:

Use Dspring.profiles.active=prod in VM parameters
Use spring.profiles.active=prod in application.properties

おすすめ

転載: blog.csdn.net/m0_54861649/article/details/126665853