SpringBoot

1. SpringBoot profile

When you first get to know the SSM framework, you must be bothered by the configuration file of the framework. You need to configure a lot of things manually, even if you do not need some things, you need to configure them. And SpringBoot came into being, reducing the configuration process. You’ve all heard the phrase “convention over configuration”, which means that systems, libraries, and frameworks should assume reasonable defaults rather than requiring unnecessary configuration, but there are still a lot of things that need to be configured using Spring or SpringMVC. This not only increases the workload but also causes problems when deploying across platforms. Simply put, using SpringBoot allows us to quickly build a running Spring project.

1.1 SpringBoot advantages

  • Ability to quickly build a Spring project that can run on its own
  • The Tomcat container is embedded, and Tomcat is not required
  • A number of starters are provided to simplify a lot of configuration
  • No code generation, no XML configuration

2. The first SpringBoot project is started

2.1 Project Construction

With Springboot introduced, let’s start building a simple “Hello World!” SpringBoot project. A journey of a thousand miles begins with a single step, and I believe that the first Java project also started from “Hello, World! In the first place. Don’t underestimate this first step, how many people are frustrated in the first step and give up. But SpringBoot’s first boot step is simple and in keeping with its mission. Build projects quickly and easily.

IO/This is the website provided by SpringBoot to create a project, click on it, select the required version, language, function and click on Generate to generate a project package, if you want to start a simple Web project, then type Web in the function area, The SpringBoot Startup Web package is automatically introduced when the project is created. Then, when you open the project with the compiler, you’ll find that SpringBoot has already created the file directory.

Can be seen clearly SpringBoot generated file structure, including FirstSpringBootApplication is and your project name is consistent, it is equivalent to the entrance of the program, and the application documents under the resource folder is the configuration file, For example, you can configure the Tomcat port number you want to start. Build. gralde is a file generated because I chose Gradle. This file is where I can write the jar packages I want to reference. Gradle automatically downloads jars from the web or locally.

2.2 Project Startup

Add this line of code to your project’s startup file, or you can create your own Java file. But you do annotate @restController. If at this time

@RequestMapping("/")
	String home() {
		return "Hello";
	}
Copy the code

Why at sign RestController and not at sign Controller, at this point we can click into at sign RestController and see that it’s a annotation made up of at sign Controller and at sign ResponseBody. It makes a difference if you add @responseBody or not, if you add @responseBody then it’s going to return your value as data, if you don’t add it then it’s going to be the name of the page. So an example of code like the one above is that if it has @responseBody it will return a Hello string to the page, if it doesn’t it will go to hello.html or hello.jsp, JSP or HTML depending on SpringMvc configuration, I won’t explain it in detail here. Then run directly FirstSpringBootApplication. Java is the main method can start inside. If you open up the startup file, you’ll see that it has an @springBootapplocation annotation on it. This annotation will be explained later. Then type localhost:8080 on the page to see Hello.

2.3 Project Structure

In general, the boot classes for SpringBoot are recommended to be placed in the root directory of the code. Since the @enableAutoConfiguration annotation is scanned based on your startup class, this means scanning the startup class folder and all of the following files.

com +- example +- myproject +- Application.java | +- domain | +- Customer.java | +- CustomerRepository.java | +- service  | +- CustomerService.java | +- web +- CustomerController.javaCopy the code

3.@SpringBootApplocation full explanation of notes

When you go into the @SpringBootapplocation annotation, there are other annotations

3.1 @ EnableAutoConfiguration

This is a class-level annotation, and the purpose of this annotation is to guess, why guess. Because it will guess what the configuration will be based on your JAR dependencies. For example, spring-boot-starter-web, when you add the jar package that contains Tomcat and SpringMVC, this annotation will guess that the project will be a Web project, so it will automatically configure Spring.

Of course, you can customize your own configuration. For example, if you want to add your own DateSource, write a class annotated with @configure, and use your own configuration first when the project starts. If you want to know what is automatically configured in your project, start the project with –debug and it will show up in the console log.

3.2 @ ComponentScan

In Sping you can use annotations to inject beans, such as @Controller@Service and so on, which are automatically injected. ComponentScan(“org.example.web”) is a ComponentScan(“org.example.web”). This annotation and the corresponding configuration in XML are

<context:component-scan base-package="org.example.web"/>
Copy the code

3.3 @ SpringBootConfiguration

This annotation indicates that a class provides a SpringBoot application. In the previous Spring Test module, if you wanted to do a modular Test, you might look for @ContextConfiguration to load some resource files. You might also need to write @Configuration for the Test module. This is not the case in SpringBoot because it has an algorithm that automatically looks for @SpringBootConfiguration annotations during modular testing. This does not require a separate configuration file for the test module, but if you want to customize the configuration file for the test module, you can just add @testConfiguration.

3.4 Other Notes

3.4.1 track @ ImportResource

SpringBoot is a Configuration file that is not recommended to use XML. What it wants is for all configurations to be Java classes with @Configuration on top. However, in cases where a project really needs an XML configuration file, you can use the @importResource annotation