SpringBoot是Spring项目中的一个子工程,与我们所熟知的Spring-framework 同属于spring的产品:

Java学习之SpringBoot

 

 

把Spring Boot称为搭建程序的脚手架。其最主要作用就是帮我们快速的构建庞大的spring项目,并且尽可能的减少一切xml配置,做到开箱即用,迅速上手,让我们关注于业务而非配置。

我们可以使用SpringBoot创建java应用,并使用"java –jar"命令启动,就能得到一个生产级别的web工程。

特点:

  • 创建独立的spring应用程序

  • 直接内嵌tomcat、jetty和undertow(不需要打包成war包部署)

  • 提供了固定化的“starter”配置,以简化构建配置

  • 尽可能的自动配置spring和第三方库

  • 提供产品级的功能,如:安全指标、运行状况监测和外部化配置等

  • 绝对不会生成代码,并且不需要XML配置

项目Demo

Java学习之SpringBoot

 Java学习之SpringBoot

 Java学习之SpringBoot

 Java学习之SpringBoot

 Java学习之SpringBoot

 Java学习之SpringBoot

 Java学习之SpringBoot

 Java学习之SpringBoot

添加启动器:

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

添加测试:

Java学习之SpringBoot

@RestController
@EnableAutoConfiguration
public class MyController {

    @GetMapping("hello")
    public String test(){
        return "HelloWorld!";
    }

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

运行后,在浏览器输入地址:http://localhost:8080/hello

结果:

Java学习之SpringBoot

注意:SpringBoot启动需要先关闭上一个再启动!

上述demo只有一个controller,可以进行优化:

启动类:

Java学习之SpringBoot

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

测试类:

Java学习之SpringBoot

@RestController
public class MyController {

    @GetMapping("hello")
    public String test(){
        return "First SpringBoot";
    }
}
@RestController
public class MyController2 {

    @GetMapping("hello2")
    public String test(){
        return "Second SpringBoot";
    }
}

结果:

Java学习之SpringBootJava学习之SpringBoot

 补充:

@EnableAutoConfiguration

开启spring应用程序的自动配置,SpringBoot基于你所添加的依赖和你自己定义的bean,试图去猜测并配置你想要的配置。比如我们引入了`spring-boot-starter-web`,而这个启动器中帮我们添加了`tomcat`、`SpringMVC`的依赖。此时自动配置就知道你是要开发一个web应用,所以就帮你完成了web及SpringMVC的默认配置了!

总结,SpringBoot内部对大量的第三方库或Spring内部库进行了默认配置,这些配置是否生效,取决于我们是否引入了对应库所需的依赖,如果有那么默认配置就会生效。所以,我们使用SpringBoot构建一个项目,只需要引入所需依赖,配置就可以交给SpringBoot处理了。

 

 @SpringBootApplication

@SpringBootApplication其实是一个组合注解,这里重点的注解有3个:

  • @SpringBootConfiguration(来声明当前类是SpringBoot应用的配置类,项目中只能有一个,所以一般我们无需自己添加。)

  • @EnableAutoConfiguration:开启自动配置

  • @ComponentScan:开启注解扫描

配置文件

SpringBoot使用一个全局的配置文件,配置文件名是固定的,文件命名主要分为以下两种(任选其一):

•application.properties

•application.yml

完整项目

(Springboot+HikariCP连接池+MyBatis+Thymeleaf,实现数据查询)

 项目结构:

Java学习之SpringBoot

 

 pom.xml:

<?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>com.demo.myspringboot</groupId>
    <artifactId>myspringboot</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.1.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.28</version>
        </dependency>
        <!--默认使用HikariCP连接池-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
            <version>2.1.5.RELEASE</version>
        </dependency>
        <!--不要忘记数据库驱动,因为springboot不知道我们使用的什么数据库,这里选择mysql-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.46</version>
        </dependency>
        <!--mybatis -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>
        <!-- thymeleaf -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
            <version>2.1.4.RELEASE</version>
        </dependency>
    </dependencies>
</project>
pom.xml

相关文章:

  • 2021-08-07
  • 2022-12-23
  • 2021-07-30
  • 2021-09-20
  • 2021-09-07
  • 2021-08-14
猜你喜欢
  • 2022-01-15
  • 2022-12-23
  • 2021-12-10
  • 2021-08-05
  • 2021-11-05
  • 2021-08-23
相关资源
相似解决方案