Spring boot project construction tutorial and notes

Spring Boot

Spring boot is a new framework provided by pivot team. It is designed to simplify the initial construction and development process of new spring applications. The framework uses a specific way to configure, so that developers no longer need to define templated configurations. In this way, spring boot is committed to becoming a leader in the booming field of rapid application development

characteristic:

1. Create an independent spring application

2. Embedded tomcat, no need to deploy war file

3. Simplify Maven configuration

4. Automatically configure spring

5. Provide production ready functions, such as indicators, health check and external configuration

6. Absolutely no code generation and no configuration requirements for XML

Construction of Web project

Build environment and tools: idea Tool + JDK environment + Maven environment

Step 1: first create a new project, select spring initializr, and select the SDK on the right, that is, your JDK version. By default, the spring boot template will be downloaded from the official website and directly next

Step 2: These are some details of the project, and roughly talk about the meaning

Group: corresponding to POM The groupid in the XML file, the unique identifier of the project organization, and the corresponding java package structure

Artifact: corresponds to POM The artifact ID of the XML file, the unique ID of the project, and the corresponding project name

Type: we are built by maven, so choose the first Maven project

Language: development language, select Java

Packaging: packaging type, which is packaged into a jar file

Java version: JDK version, select 1.8

Version: project version, corresponding to the version of POM file

Name: project name

Description: item description, corresponding to the description of POM file

Package: package name

I've probably understood it. Fill it in according to your own needs. If you test, it's OK by default. Next

Step 3: according to your own needs, what functions you need to add, I think it is very complete here. Select web and check web. If you want SQL, you can click on some databases such as mysql, JDBC and mybatis, and select the desired direct next

Step 4: it's also the most important. If you don't have anything, just find it directly

The completed project structure directory: we can delete the irrelevant files I selected. They are all Maven files, which are useless

Explain the role of some of these files

Finally, create a hellocontroller in this directory Java class

code:

Then run the project and enter: localhost: 8080 in your browser to access it

Here I explain the meaning of the annotation:

List of annotations

@ResponseBody

The function modified with this annotation will directly fill the result into the HTTP response body, which is generally used to build a restful API;

@Controller

It is used to define the controller class. In the spring project, the controller is responsible for forwarding the URL request sent by the user to the corresponding service interface (service layer).

@RestController

@Collection of ResponseBody and @ controller

@RequestMapping

Provides routing information and is responsible for mapping URLs to specific functions in the controller.

@EnableAutoConfiguration

Spring boot auto configuration: try to automatically configure your spring application according to the jar dependency you added. For example, if HSQLDB exists in your classpath and you do not manually configure any database connection beans, we will automatically configure a memory type (in memory) database ". You can add the @ enableautoconfiguration or @ springbootapplication annotation to a @ configuration class to select automatic configuration. If you find that specific automatic configuration classes you don't want are applied, you can disable them by using the exclusion property of the @ enableautoconfiguration annotation. The example code is as follows:

@ComponentScan

Indicates that this class is automatically discovered (scanned) and registered as a bean, which can automatically collect all spring components, including the @ configuration class. We often use the @ componentscan annotation to search beans and import them in combination with the @ Autowired annotation.

@Configuration

It is equivalent to the traditional XML configuration file. If some third-party libraries need XML files, it is recommended to still use the @ configuration class as the main configuration class of the project -- you can use the @ importresource annotation to load the XML configuration file.

@SpringBootApplication

It is equivalent to the collection of @ enableautoconfiguration, @ componentscan and @ configuration.

@Import

Used to import other configuration classes.

@ImportResource

Used to load XML configuration files.

@Autowired

Automatically import dependent beans

@Service

It is generally used to decorate the components of the service layer

@Repository

Using the @ repository annotation can ensure that Dao or repositories provide exception translation. The Dao or repositories classes modified by this annotation will be discovered and configured by componetscan, and there is no need to provide XML configuration items for them.

The above spring boot project construction tutorial and notes are all the contents shared by Xiaobian. I hope they can give you a reference and support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>