Spring学习(十三)——SpringBoot入门

前面我们学习了Spring的使用和Spring MVC的使用,今天我们来研究一下SpringBoot,SpringBoot融入了Spring和Spring MVC,并且简化了Spring的配置,且嵌入了Tomcat,那么我们学习SpringBoot的过程和之前学习Spring以及Spring MVC的过程类似,来一步一步探讨SpringBoot的奥秘。

一、Spring和Spring MVC学习清单

Spring学习(一)——Spring模块概述
Spring学习(二)——依赖注入
Spring学习(三)——面向切面编程
Spring学习(四)——Spring整合Mybatis
Spring学习(五)——配置工厂类Bean
Spring学习(六)——基于xml配置文件进行AOP编程
Spring学习(七)——Spring MVC
Spring学习(八)——Spring MVC之Restful API返回json数据
Spring学习(九)——Spring MVC之全局异常处理
Spring学习(十)——Spring MVC之拦截器
Spring学习(十一)——Spring MVC之文件上传
Spring学习(十二)——Spring MVC之文件下载

二、Maven引入Spring Boot

pom.xml

引入父项目:

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.1.RELEASE</version>
  </parent>

测试依赖:

    <!-- SpringBoot自动配置 -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-autoconfigure</artifactId>
    </dependency>

    <!-- Junit -->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>

    <!-- SpringBoot test -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>

三、入门案例——依赖注入

入口类

package cool.gjh;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * SpringBoot入口类
 *
 * @author ACE_GJH
 */
@SpringBootApplication
public class App 
{
    public static void main( String[] args )
    {
        SpringApplication.run(App.class, args);
    }
}

测试组件

Car接口

package cool.gjh.beans;

/**
 * 车
 *
 * @author ACE_GJH
 */
public interface Car {

    /**
     * 驾驶
     */
    void drive();

}

Bicycle实现类

package cool.gjh.beans;

import org.springframework.stereotype.Component;

/**
 * 自行车
 *
 * @author ACE_GJH
 */
@Component
public class Bicycle implements Car{
    /**
     * 驾驶
     */
    @Override
    public void drive() {
        System.out.println("骑自行车开始兜风......");
    }
}

测试类

package cool.gjh;

import static org.junit.Assert.assertTrue;

import cool.gjh.beans.Car;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

/**
 * SpringBoot测试类
 *
 * @author ACE_GJH
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = App.class)
public class AppTest 
{

    @Autowired
    private Car car;

    /**
     * SpringBoot依赖注入测试
     */
    @Test
    public void SpringBootDITest()
    {
        car.drive();
    }
}

测试结果

测试结果

小贴士

注意,SpringBoot本质上是Spring和Spring MVC的结合,从该案例中也可以看出来,SpringBoot的入口类扫描的组件默认是入口类所在的包及其子包,和Spring的配置类一样,如果想指定组件扫描的范围,可以在入口类上面@ComponentScan注解,当然,这和Spring一模一样,如下所示:

package cool.gjh.application;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

/**
 * SpringBoot入口类
 *
 * @author ACE_GJH
 */
@SpringBootApplication
@ComponentScan("cool.gjh")
public class App 
{
    public static void main( String[] args )
    {
        SpringApplication.run(App.class, args);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

郭建華

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值