[Microservices] Basic development of SpringBoot

Getting to know SpringBoot for the first time

1. The origin of SpringBoot
SpringBoot is a framework provided by the Spring team to simplify the initial construction and development process of Spring applications.
2. SpringBoot Features :
Dependency Management, Automatic Configuration
3. SpringBoot Advantages
(1) Can quickly build an independent Spring application
(2) Embedded Servlet container, no need to install a separate container to run the project independently
(3) Compatibility with mainstream frameworks Configuration-free integration
(4) provides out-of-the-box Spring plug-ins, which simplifies the configuration of Maven and Gradle
(5) does not require any XML configuration
4. The use of SpringBoot2 requires
Java8 and above, and Maven3.5 or above
to view the Java version in cmd: java -version
Check the Maven version under cmd: mvn -v
5.
The name of the configuration file yaml or yml SpringBoot configuration file is fixed, the default is application.properties, the custom is application.ymlor application.yaml, the following are application.yamlthe syntax rules.
insert image description here
6. Quickly develop a SpringBoot application
(1) import the parent project and related dependencies

<!--使用的父项目-->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.6.4</version>
</parent>
<!--需要的依赖-->
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

(2) Write code

/**
 * @SpringBootApplication:复合注解,标识这是一个SpringBoot应用的主程序类
 */
@SpringBootApplication
public class HelloApplication {
    
    
    public static void main(String[] args) {
    
    
        SpringApplication.run(HelloApplication.class,args);
    }
}
/**
 * @RestController:@ResponseBody和@Controller的复合注解
 * 此注解标识的方法返回值不再是页面,而是return与剧中的内容
 */
@RestController
public class HelloController {
    
    
    //@RequestMapping用于标注方法的请求路径
    @RequestMapping("/hello")
    public String hello(){
    
    
        return "Hello SpringBoot 2!";
    }
}

Note: The location of the Controller class is only allowed in the main program class Application and the directory under it, otherwise it will not be recognized and needs to be manually configured.
Start the project, visit: in the browser http://localhost:8080/hello, you can see the return statement of the method in the Controller class.
Extension: If you need to package the project into a jar package to run
① you need to configure the following plug-ins in pom.xml, and configure the packaging method as jar

<!--配置项目的打包方式(默认是jar包)-->
<packaging>jar</packaging>
<!--用于将SpringBoot项目打包成jar包-->
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

② Find the corresponding project in Maven, select the package, and click Run to continue. insert image description here
③ Find and open the directory in the project directory target, and you can see the successfully packaged jar package. Then select the jar package and open in --> Explorer. insert image description here
④ Open cmd in the current folder, run: java -jar springboot-demo01-1.0-SNAPSHOT.jar, you can see that the project runs successfully in the form of a jar package.
insert image description here
⑤ Access browser: http://localhost:8080/hello.
insert image description here

Guess you like

Origin blog.csdn.net/weixin_46081857/article/details/123416913