SpringBoot (1)-project construction and quick start

Before introducing Spring Boot, we first recall the steps we are using to build a project using SSM. First of all, we definitely need to create a Maven project, then we need to introduce Spring-related dependencies, and then we also need to introduce MyBatis-related dependencies Configure related configuration files. In addition, we also need to introduce Spring MVC dependencies. It is also necessary to configure related configuration files. Finally, we need to configure Tomcat for our project. So far we have completed the construction of a project.


After reading the above steps, is it very troublesome, and there are too many dependencies, and there are version problems, then the benefits of using Spring Boot? SpringBoot is designed to simplify the initial construction and development of new Spring applications. The framework uses a specific way to configure, so that developers no longer need to define a sample configuration. In short, the original intention of Spring Boot is to simplify the configuration of spring, make it faster when integrating new functions in development, and simplify or reduce the related configuration.




Here we build the SpringBoot project, using the most basic way, first we build a basic Maven project, as follows:
Insert picture description here
Then we enter some information of the project, select the project path, etc., you can complete.


The following is actually very simple, we can do it according to the introduction on Spring Boot , in fact, we first need Spring Boot's parent dependency, as follows:
Insert picture description here

In the picture above, we also specify the project code and jdk version number, we can also use one of the following methods, can also be configured.
Insert picture description here


In fact, our SpringBoot project has been initially completed. Is it super simple? Here is a small example we copied from the SpringBoot official website, as follows:
Insert picture description here

@EnableAutoConfiguration
@RestController
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @GetMapping("/hello")
    public String hello(@RequestParam(value = "name", defaultValue = "World") String name) {
        return String.format("Hello %s!", name);
    }
}

Then we right-click to start the project, access the above pathlocalhost:8080/hello
Insert picture description here

Among them, @RestController, @GetMapping and @RequestParam, we should all be relatively familiar, have already learned in SpringMVC, here we take a look @EnableAutoConfiguration, this annotation tells Spring Boot that you need to guess how you want based on the added jar dependency Configure Spring, for example, we added the spring-boot-starter-web dependency above, which includes Tomcat and Spring MVC, so SpringBoot will set up Spring according to the requirements of developing a web application. ,
Insert picture description here


We can see that the AutoConfigurationImportSelector class was introduced through the @Import annotation. The role of @Import annotation has been introduced in Srping
Insert picture description here


We can see that through the AutoConfigurationImportSelector class, META-INF / spring.factories is introduced, which is the component loaded by default in Spring Boot
Insert picture description here
Insert picture description here




Here we have to change our test method above again. We have just accessed the hello method and wrote it separately in the DemoController class and placed it under the controller package. This is the standard project in our daily development, as follows:
Insert picture description here

@EnableAutoConfiguration
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}
@RestController
public class DemoController {

    @GetMapping("/hello")
    public String hello(@RequestParam(value = "name", defaultValue = "World") String name) {
        return String.format("Hello %s!", name);
    }
}

Then we are running this project, and then still visit localhost:8080/hello, and found an error
Insert picture description here

Why is this? In fact, we can understand by recalling Spring. We did not scan the corresponding classes or packages. Our @EnableAutoConfiguration is only used to introduce some third-party components, but it does not have the scanning function, so we need to use it here @SpringBootApplication, as follows :
Insert picture description here
Insert picture description here

Here we need to pay attention that this @SpringBootApplicationannotation scans all the same-level folders of the startup class, so we must not misplace the startup class
Insert picture description here

At this time, we can start the project again, and we can visit it as follows:
Insert picture description here




Modify the port number

Here we take another look at how to modify the default port number of Spring Boot: 8080, which is actually very simple, we only need to modify it in the configuration file, as follows:
Insert picture description here

server.port=8081

Insert picture description here


We used the .properties file to modify the above, we can also use yml to modify, in fact, the same is as follows:
Insert picture description here

server:
  port: 8082

Insert picture description here

Note: If the properties file and the yml file exist at the same time, the properties file will take priority



Add project name

In addition, we can also add the name of the project to the access path of Spring Boot. It turns out that in the Spring project, we can configure it in Tomcat, here we can directly modify it in the configuration file:

server.servlet.context-path=/demo

Insert picture description here
Note: Don't ignore the previous " / " Oh, in addition, yml has a similar configuration.




Finally, when we build a Spring Boot project, it can be even simpler. We can directly use IDEA to help us build one, as follows:
Insert picture description here

Here we can choose the version of Spring Boot, and we can also add some components we need, as follows:
Insert picture description here

After completion, the project structure is as follows
Insert picture description here

We can view its pom file, in fact, it is almost the same as using our own Maven project to build SpringBoot

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.2.5.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.kimi</groupId>
	<artifactId>springboot2</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>springboot2</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
	</properties>

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

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
			<exclusions>
				<exclusion>
					<groupId>org.junit.vintage</groupId>
					<artifactId>junit-vintage-engine</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>
286 original articles published · Liked12 · Visitors 10,000+

Guess you like

Origin blog.csdn.net/newbie0107/article/details/105083383