preface

I believe that those of you who are engaged in Java development have heard of the SSM framework, which is still young, and those of you who are older have even experienced SSH, which is a bit scary to say, haha. For example, I have experienced the end of SSH era, there is no way, very helpless.

Of course, neither SSM nor SSH is the focus of today’s talk. Today’s talk is about Spring Boot, an impressive framework. In a large sense, Spring Boot replaces SS in SSM.

Today’s article will talk about Spring Boot, the first time I’ve used a straightforward framework.

What is Spring Boot?

Spring Boot is a new framework provided by the Pivotal team. Spring Boot is the starting point for all projects based on Spring Framework 5.0. Spring Boot is designed to get your Spring applications running as fast as possible with as few configuration files as possible.

Spring Boot is designed to simplify the initial setup and development of Spring applications.

At its most basic, Spring Boot is a collection of libraries that can be used by any project’s build system. It uses the idea of “convention over configuration” (there is a lot of configuration in the project, plus a habitual configuration built in) to get your project running quickly.

Convention is greater than configuration. In fact, simply speaking, Spring Boot has built many common configurations in actual development at the beginning of construction, and only a few configurations need to be configured by developers themselves.

How to set up a Spring Boot project?

There are many ways to build a SpringBoot project. The two most common ways are as follows:

  1. Create a Maven project, import your own dependencies, and create startup classes and configuration files.
  2. In direct IDEA Spring InitializrCreate the project.

The first way is not suitable for beginners to play, today shows the second way to build a Spring Boot project.

The first step is to select ideasFile-->NEW-->Project, the choice ofSpring Initializr, specifyJDKversion1.8And thenNext. The diagram below:

Step 2 Specify Maven coordinates, package name, JDK version, and so onNext, as shown below:

The third step is to choose the dependency you need and the version of Spring Boot. Spring Boot is compatible with each frameworkstarterMode, here we select the required for WEB developmentstarterOk, as shown below:

The fourth step is to specify the name of the project and complete the path. ClickFinishWait for the successful creation, as shown below:

The successful project is shown below:

DemoApplication is the project’s startup class, which has a main() method to start Spring Boot. Application. Properties is the Spring Boot configuration file.

You can start the project at this pointDemoApplicationrunmainThe successful startup is shown in the following figure:

Since SpringBoot has Tomcat built-in by default, the default port to boot is 8080.

The first program is Hello World

Learning any kind of technology always want to greet the world, ha ha……….

Since it is WEB development, we can write an interface that already references the WEB starter. If there is no reference, we can introduce the following dependencies in PM.xml:

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

Copy the code
  • Let me write oneHelloWorldControllerAs follows:
package com.example.demo.controller;
@RestController
public class HelloWorldController {
    @RequestMapping("/hello")
    public String helloWorld(a){
        return "Hello World"; }}Copy the code

@restController: Mark this is a Controller, which is the set of @Controller and @responseBody annotations.

@requestMapping: Specifies a mapping

The above two annotations are in Spring, so I won’t go into detail here.

Because of the built-in Tomcat default port is 8080, so start the project, visit http://127.0.0.1:8080/hello.

Depend on reading

There is a dependency in pom.xml in the Spring Boot project that looks like this:

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.4. RELEASE</version>
        <relativePath/>
    </parent>
Copy the code

The tag knows what it means, and its main purpose is for version control. This is why the introduction of the WEB module starter does not specify the version

tag, because it is already specified in the spring-boot-starter-parent. It is a kind of inheritance relationship that the parent already provides for you, and you only need to choose whether to use it or not.

Why introducespring-boot-starter-webYou can useSpring mvcThe function of?

This is a difficult question to understand. To understand this question, let’s take a look at what the spring-boot-starter-web launcher depends on. As follows:

<dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
      <version>2.3.4. RELEASE</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-json</artifactId>
      <version>2.3.4. RELEASE</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-tomcat</artifactId>
      <version>2.3.4. RELEASE</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>5.2.9. RELEASE</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.2.9. RELEASE</version>
      <scope>compile</scope>
    </dependency>
  </dependencies>
Copy the code

Spring-boot-starter-web is an internal starter that uses spring, SpringMVC, and Tomcat dependencies.

What is a profile?

Application. Properties is a configuration file for Spring Boot.

In order to adapt to each component, Spring Boot provides a starter, but some information about these initiators cannot be written internally. For example, the user name and password of the database must be specified by the developer. Therefore, it is written in a Properties class. Read from a configuration file at Spring Boot startup based on the prefix name + property name, such as WebMvcProperties, which defines some Spring Mvc related configurations with the prefix spring.mvc. As follows:

@ConfigurationProperties(prefix = "spring.mvc")
public class WebMvcProperties {
Copy the code

We need to modify the Spring Mvc configuration by specifying spring.mvC.xxxx = XXXX in the application.properties file.

In fact, there are many ways to configure files, which will be covered in a later article.

What is a startup class?

The DemoApplication class is DemoApplication.

@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); }}Copy the code

What is @SpringBootApplication? In fact, at first glance, this class looks pretty ordinary. The only thing that stands out is the @SpringBootApplication annotation, which is of course the main function. The source code for this annotation is as follows:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class), @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {}
Copy the code

@springBootConfiguration, @EnableAutoConfiguration, @ComponentScan.

ComponentScan: This is not a new annotation, a Spring annotation, a package-scanning annotation, which scans for beans in the class and child packages of the startup class at project startup.

@springBootconfiguration: This annotation makes SpringBoot annotation, source code is as follows:

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
public @interface SpringBootConfiguration {
    @AliasFor( annotation = Configuration.class )
    boolean proxyBeanMethods(a) default true;
}
Copy the code

@Configuration is a Spring annotation that indicates that this class is a Configuration class, so we can do something in the boot class that the Configuration class can do. For example, inject a Bean.

EnableAutoConfiguration @enableAutoConfiguration: This annotation will translate the code directly and enable automatic configuration, as the name indicates.

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {
Copy the code

Another familiar annotation @import, what does it do? There are three ways to quickly import beans into an IOC container, one of which is the ImportSelector method used here. Not the point of this article, no more details.

The @enableAutoConfiguration annotation is no more than a form of @import to quickly inject beans into the IOC container at project startup time.

Well, that’s the start class for now, and the power of this class will be more clearly understood later in the source code article.

How do unit tests work?

The Spring Boot project was created with a unit test class that looks like this:

@SpringBootTest
class DemoApplicationTests {

    @Test
    void contextLoads(a) {}}Copy the code

SpringBootTest: This annotation specifies that this class is a unit test class.

This class automatically retrieves beans from the IOC container, for example:

@SpringBootTest
class DemoApplicationTests {

    @Autowired
    private HelloWorldController helloWorldController;
Copy the code

As the project gets bigger and takes longer to start, who would be stupid enough to start a test method to verify the code? It’s a waste of time.

conclusion

As the first shot of Spring Boot, write here is over, nothing in-depth content, just a simple preliminary understanding of Spring Boot.

The development tool used in this article is IDEA, there is a need for the 2020 version of the public number [code ape technology column] reply key words IDEA2020, there is a need for THE reply key words IDEA crack package