Spring Boot 入门

Spring Boot 入门

1.导入依赖

SpringBoot提供了一个名为spring-boot-starter-parent的工程,里面已经对各种常用依赖(并非全部)的版本进行了管理,我们的项目需要以这个项目为父工程,这样我们就不用操心依赖的版本问题了,需要什么依赖,直接引入坐标即可!

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>cn.lantian.springboot</groupId>
    <artifactId>lantian-springboot</artifactId>
    <version>1.0-SNAPSHOT</version>

    <!-- 所有的springboot的工程都以spring父工程为父工程 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.6.RELEASE</version>
    </parent>
    <!-- web的启动器,每一个启动器背后都是一堆的依赖 -->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
</project>

2.编写HelloController


@RestController
@RequestMapping("hello")
public class HelloController {

    @RequestMapping("show")
    public String test() {
        return "test1";
    }
}

添加引导类,springboot应用的入口

@SpringBootApplication
@MapperScan(value = "com.lantian.course.mapper")
public class TestApplication {

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

@SpringBootApplication

我们现在的引导类中使用了@EnableAutoConfiguration和@ComponentScan注解,有点麻烦。springboot提供了一种简便的玩法:@SpringBootApplication注解

启动器

全局配置

另外,SpringBoot的默认配置,都会读取默认属性,而这些属性可以通过自定义application.properties文件来进行覆盖。这样虽然使用的还是默认配置,但是配置中的值改成了我们自定义的。

因此,玩SpringBoot的第二件事情,就是通过application.properties来覆盖默认属性值,形成自定义配置。我们需要知道SpringBoot的默认属性key

posted @ 2020-08-17 02:37  silence022  阅读(7)  评论(0)    收藏  举报