SpringBoot基础应用

SpringBoot

SpringBoot概念

SpringBoot是基于Spring开发的,Spring Boot的设计是为了让你尽可能快的跑起来Spring应用程序并且尽可能减少你的配置。

它的设计思想是:约定大于配置

SpringBoot为了解决Spring项目依赖多,配置繁琐的问题,对Spring进行了改善和优化

  • 起步依赖

起步依赖本质上是一个Maven项目对象模型,简单的说就是把某些功能的依赖包都打包到一起,你引入一个就行了。

  • 自动配置

springboot的自动配置指的是springboot会自动的将一些配置类的bean注册进ioc容器。

“自动”的表现形式就是我们只需要引入我们想用功能的包,相关配置不用管,springboot就会自动注入这些配置bean。

SpringBoot项目构建

  1. 使用Spring Initializr方式构建Spring Boot项目

  2. 填写包名

  1. 选择对应的版本,和根据场景选择依赖

  1. 点击下一步,完成就可以了。
    项目结构介绍:

附:解决中文乱码

#设置响应为utf-8
spring.http.encoding.force-response=true

配置详解

1. application.properties配置文件

我们可以在application.properties文件中定义Spring Boot项目的相关属性,当然,这些相关属性可以
是系统属性、环境变量、命令参数等信息,也可以是自定义配置文件名称和位置

server.port=8081
spring.config.location=
spring.config.name=application

2. application.yaml配置文件

YAML文件格式是Spring Boot支持的一种JSON超集文件格式,相较于传统的Properties配置文件,
YAML文件以数据为核心,是一种更为直观且容易被电脑识别的数据序列化格式

  • YAML文件的扩展名可以使用.yml或者.yaml
  • application.yml文件使用 “key:(空格)value”格式配置属性,使用缩进控制层级关系。

对于不同类型的属性值,yml的配置格式不同

  1. value值为普通数据类型
server:
  port: 8081
  1. value值为数组和单列集合
person:
  hobby:
    - aa
    - bb
    - cc

person:
  hobby:
    aa,
    bb,
    cc

person:
  hobby: [aa,bb,cc]
  1. value值为Map集合和对象
person:
  map:
    k1: v1
    k2: v2

person:
  map: {k1: v1,k2: v2}

3. 配置文件属性值注入

  1. 使用@ConfigurationProperties注入属性
@Component
@ConfigurationProperties(prefix = "person")
public class Person {

    private Integer id;

    private String name;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Person{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}

配置文件:

person:
  id: 2
  name: 李明
  1. 使用@Value
package com.mmc.springbootdemo.bean;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
public class Person {

    @Value("${person.id}")
    private Integer id;

    @Value("${person.name}")
    private String name;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Person{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}

4. 自定义配置文件

之前我们的配置文件都是写在application.properties或application.yml里的,如果我们自己新建的配置文件怎么读取呢?

使用@PropertySource

@Component
@ConfigurationProperties(prefix = "test")
@PropertySource("classpath:test.properties")
public class MyProperties {

    private Integer id;
    private String name;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

5. 编写配置类

在Spring Boot框架中,推荐使用配置类的方式向容器中添加和配置组件

在Spring Boot框架中,通常使用@Configuration注解定义一个配置类,Spring Boot会自动扫描和识别
配置类,从而替换传统Spring框架中的XML配置文件。

@Configuration  // 定义该类是一个配置类
public class MyConfig {
  @Bean     // 将返回值对象作为组件添加到Spring容器中,该组件id默认为方法名
  public MyService myService(){
    return new MyService();
 }
}
书山有路勤为径,学海无涯苦作舟
原文地址:https://www.cnblogs.com/javammc/p/15619069.html