【Study Notes】Hello SpringBoot

【Study Notes】Hello SpringBoot

insert image description here

Introduction to SpringBoot

What is SpringBoot?

Spring Boot is a new framework provided by the Pivotal team, which is designed to simplify the initial setup and development of Spring applications. The framework uses a specific way to configure, so that developers no longer need to define boilerplate configuration. Spring Boot is actually a framework that integrates many pluggable components (frameworks) and has embedded tools (such as Tomcat, Jetty, etc.), which is convenient for developers to quickly build and develop.

Advantages and disadvantages of SpringBoot

advantage:

  • Create stand-alone Spring applications

    • Create a standalone Spring application
  • Embed Tomcat, Jetty or Undertow directly (no need to deploy WAR files)

    • Embedded web server
  • Provide opinionated ‘starter’ dependencies to simplify your build configuration

    • Automatic starter dependencies to simplify build configuration
  • Automatically configure Spring and 3rd party libraries whenever possible

    • Automatic configuration of Spring and third-party functions
  • Provide production-ready features such as metrics, health checks, and externalized configuration

    • Provides production-level monitoring, health checks, and externalized configuration
  • Absolutely no code generation and no requirement for XML configuration

    • No code generation, no need to write XML

shortcoming:

  • Known as the version emperor, iterates fast, and needs to pay attention to changes at all times
  • The package is too deep, the internal principle is complex, and it is not easy to master

SpringBoot's environment configuration

1. System requirements

The environment required by SpringBoot is generally:

  • JDK version 1.8 and above.

  • Maven version 3.3 and above.

2. maven settings

You can set the Alibaba Cloud image in the maven configuration file to improve the download speed.

And configure JDK1.8.

<mirrors>
      <mirror>
        <id>nexus-aliyun</id>
        <mirrorOf>central</mirrorOf>
        <name>Nexus aliyun</name>
        <url>http://maven.aliyun.com/nexus/content/groups/public</url>
      </mirror>
  </mirrors>
 
  <profiles>
         <profile>
              <id>jdk-1.8</id>
              <activation>
                <activeByDefault>true</activeByDefault>
                <jdk>1.8</jdk>
              </activation>
              <properties>
                <maven.compiler.source>1.8</maven.compiler.source>
                <maven.compiler.target>1.8</maven.compiler.target>
                <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
              </properties>
         </profile>
  </profiles>

3. Introduce dependencies

When learning SpringMVC before , we needed to import a lot of dependency packages in pom.xml, and SpringBoot simplifies this step.

We only need to import one dependency in SpringBoot .

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

After importing, we can find that this dependency is to import all the previous WEB dependencies into it.

insert image description here


Hello SpringBoot

1. Write the main program

Under the Java package, we create com.xiaobao.boot.MainApplication.java and write the program:


/**
 * 主程序类
 * @SpringBootApplication:这是一个SpringBoot应用
 */
@SpringBootApplication
public class MainApplication {
    
    
    //固定写法,让SpringBoot跑起来
    public static void main(String[] args) {
    
    
        SpringApplication.run(MainApplication.class,args);
    }
}

2. Write business

Create a controller package in the bean directory and create a Java program.

@RestController
public class ControllerHello {
    
    
        @RequestMapping("/hello")
        public String handle01(){
    
    
            return "Hello, Spring Boot 2!";
        }
    }

@RestController介绍

Source code:

@Target({
    
    ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Controller
@ResponseBody
public @interface RestController {
    
    
    @AliasFor(
        annotation = Controller.class
    )
    String value() default "";
}

Through the source code, we can see @Controllerand @ResponseBody. I believe that the small partners who have learned SpringMVC already know the role of the two.

So introducing annotations ResControllercan simplify a lot of code for us.

3. Test

We use the test tool that comes with IDEA to test it.

Test Results:
insert image description here

It can be seen that our most basic Hello SpringBoot environment is configured!


Simplified configuration

In the process of learning SpringMVC , we need to write a lot of xmlfile configuration.

insert image description here

In SpringBoot , we just need application.propertiesto .

E.g:

server.port=8888

Modify the port number to 8888.

Test :

When the port number is 8888 :

insert image description here

insert image description here

Guess you like

Origin blog.csdn.net/m0_54355125/article/details/124359455