spring-boot重头再来 1 项目创建 新建访问接口 浅谈注解 配置文件 yaml 修改默认端口号 加载指定配置文件 配置文件中的占位符

spring-boot重头再来 1

由于之前有一定的基础,但由于是为了尽快上岗而赶工型学习,不够全面透彻,在工作过程中明显感觉到乏力,所以决定重新学习。

话不多说直接开始

项目创建

  1. 文件->新建->项目

  2. 填写项目信息

在这里插入图片描述

  1. 依赖项选择(后续可以在pom.xml中导入,这个不着急)
    在这里插入图片描述

新建接口

  1. 在src/main/java/包名下创建一个与Application.java同级且同文件夹下的包,名为controller,并在其下面创建一个java类:TestController
    在这里插入图片描述

  2. 输入以下代码

在这里插入图片描述

package com.example.springbootone.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author BIANG
 * @Date 2021/7/6 11:15
 */
@Controller
@ResponseBody
public class TestController {
    /**
     *  ”@RequestMapping“ 注解需要 spring-boot-starter-web 依赖
     */
    @RequestMapping("test")
    public String test(){
        return "test";
    }
}

补充:添加spring-boot-starter-web 依赖

需要进行下列操作

  1. 打开项目下的pom.xml

  2. 找到dependencies标签()

  3. 在其中添加以下代码

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

补充:@RestController注解

@RestController注解相当于@ResponseBody + @Controller合在一起的作用。

访问接口

  1. 运行Application.java

在这里插入图片描述

  1. 在浏览器中输入URL "localhost:8080"可以看到
    在这里插入图片描述

访问成功

浅谈注解

@SpringBootApplication

标注在某个类上说明这个类是Springboot的主配置类Springboot就应该运行这个类的main方法来启动Springboot应用

@ComponentScan

作用:自动扫描并加载符合条件的组件或者bean , 将这个bean定义加载到IOC容器中

IOC相关知识:浅谈IOC–说清楚IOC是什么_哲-CSDN博客_ioc

@SpringBootConfiguration

作用:SpringBoot的配置类 ,标注在某个类上 , 表示这是一个SpringBoot的配置类

@Configuration

说明这是一个配置类配置类就是对应Spring的xml 配置文件

@Component

说明这是一个组件

@EnableAutoConfiguration

开启自动配置功能

@AutoConfigurationPackage

自动配置包

@Import

给容器导入一个组件

配置文件

**配置文件的作用 :**修改SpringBoot自动配置的默认值

SpringBoot使用一个全局的配置文件 , 配置文件名称是固定的

  • application.properties

    • 语法结构 :key=value
  • application.yml

    • 语法结构 :key:空格 value

yaml

语法

  1. 空格不能省略
  2. 以缩进来控制层级关系,只要是左边对齐的一列数据都是同一个层级的
  3. 属性和值的大小写都是十分敏感的
补充:单引号与双引号在yaml中的不同表现

注意:

  • “ ” 双引号,不会转义字符串里面的特殊字符 , 特殊字符会作为本身想表示的意思;

    比如 :name: " \n " 输出 : 换行

  • ‘’ 单引号,会转义特殊字符 , 特殊字符最终会变成和普通字符一样输出

    比如 :name: ‘ \n’ 输出 : \n

对象与map

缩进写法

pokemon:
	id: 25
	name: Pikachu

行内写法

pokemon: {id: 25,name: Pikachu}

数组

缩进写法

pokemons: 
 - Pikachu
 - Zorua
 - Lugia

行内写法

pokemons: [Pikachu,Zorua,Lugia]

注入配置文件

步骤
  1. 在springboot项目中的resources目录下新建一个文件 application.yml

  2. 编写一个实体类Pokemon

    package com.example.springbootone.po;
    
    import org.springframework.stereotype.Component;
    
    /**
     * @author BIANG
     * @Date 2021/7/6 12:46
     */
    @Component
    public class Pokemon {
        private int id;
        private String name;
    
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        @Override
        public String toString() {
            return "Pokemon{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    '}';
        }
    }
    
  • 原有的方法

    • 通过@Value给bean注入属性值在这里插入图片描述

    • 修改springboot测试类

      package com.example.springbootone;
      
      import com.example.springbootone.po.Pokemon;
      import org.junit.jupiter.api.Test;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.boot.test.context.SpringBootTest;
      
      @SpringBootTest
      class SpringbootOneApplicationTests {
      
          @Autowired
          Pokemon pokemon;
      
          @Test
          void contextLoads() {
              System.out.println(pokemon);
          }
      
      }
      
    • 运行测试类得到在这里插入图片描述
      注入成功

  • yaml方法

    • 编写一个实体类PokemonTrainer

      /**
       * @author BIANG
       * @Date 2021/7/6 19:07
       */
      @Component
      public class PokemonTrainer {
          private String name;
          private Pokemon[] pokemons;
      
          public PokemonTrainer() {
          }
      
          public String getName() {
              return name;
          }
      
          public void setName(String name) {
              this.name = name;
          }
      
          public Pokemon[] getPokemons() {
              return pokemons;
          }
      
          public void setPokemons(Pokemon[] pokemons) {
              this.pokemons = pokemons;
          }
      
          @Override
          public String toString() {
              return "PokemonTrainer{" +
                      "name='" + name + '\'' +
                      ", pokemons=" + Arrays.toString(pokemons) +
                      '}';
          }
      }
      
    • 新建一个application.yml在resource文件夹下

      pokemon-trainer:
        name: Ash
        pokemons:
         - {id: 25,name: Pikachu}
         - {id: 570,name: Zorua}
      
    • IDEA提示springboot配置注解处理器没有找到,在pom中添加一个依赖

      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <optional>true</optional>
      </dependency>
      
    • 添加@ConfigurationProperties注解

    在这里插入图片描述

    • 修改测试类

      package com.example.springbootone;
      
      import com.example.springbootone.po.PokemonTrainer;
      import org.junit.jupiter.api.Test;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.boot.test.context.SpringBootTest;
      
      @SpringBootTest
      class SpringbootOneApplicationTests {
          @Autowired
          PokemonTrainer pokemonTrainer;
      
          @Test
          void contextLoads() {
              System.out.println(pokemonTrainer);
          }
      
      }
      
    • 运行测试类

    在这里插入图片描述
    注入成功

修改默认端口号

  • application.properties

    server.port=8081
    
  • application.yml

    server:
      port: 8081
    

加载指定的配置文件

@ConfigurationProperties

默认从全局配置文件中获取值,比如刚刚默认加载了Application.yml

比方说我们想加载pokemon.properties中的内容,那应该怎么办呢

@PropertySource

加载指定的配置文件

  1. 在resource文件夹下新建一个pokemon.properties

在这里插入图片描述

name=Ash
  1. package com.example.springbootone.po;
    
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.annotation.PropertySource;
    import org.springframework.stereotype.Component;
    import java.util.Arrays;
    
    
    /**
     * @author BIANG
     * @Date 2021/7/6 19:07
     */
    @Component
    //通过改变prefix的值可以改变绑定的值
    //@ConfigurationProperties(prefix = "pokemon-trainer2")
    @PropertySource(value = "classpath:pokemon.properties")
    public class PokemonTrainer {
        @Value("${name}")
        private String name;
        private Pokemon[] pokemons;
    
        public PokemonTrainer() {
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public Pokemon[] getPokemons() {
            return pokemons;
        }
    
        public void setPokemons(Pokemon[] pokemons) {
            this.pokemons = pokemons;
        }
    
        @Override
        public String toString() {
            return "PokemonTrainer{" +
                    "name='" + name + '\'' +
                    ", pokemons=" + Arrays.toString(pokemons) +
                    '}';
        }
    }
    
  2. 运行测试类

在这里插入图片描述

注入成功

配置文件中的占位符

随机数

${random.uuid}生成随机uuid

${random.int}生成随机整数

例子

生成随机数作为id

  1. 修改pokemon.properties
    在这里插入图片描述

  2. 修改PokemonTrainer.java

    package com.example.springbootone.po;
    
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.context.annotation.PropertySource;
    import org.springframework.stereotype.Component;
    import java.util.Arrays;
    
    
    /**
     * @author BIANG
     * @Date 2021/7/6 19:07
     */
    @Component
    //通过改变prefix的值可以改变绑定的值
    //@ConfigurationProperties(prefix = "pokemon-trainer")
    @PropertySource(value = "classpath:pokemon.properties")
    public class PokemonTrainer {
        @Value("${name}")
        private String name;
        @Value("${id}")
        private int id;
        private Pokemon[] pokemons;
    
        public PokemonTrainer() {
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public Pokemon[] getPokemons() {
            return pokemons;
        }
    
        public void setPokemons(Pokemon[] pokemons) {
            this.pokemons = pokemons;
        }
    
        @Override
        public String toString() {
            return "PokemonTrainer{" +
                    "name='" + name + '\'' +
                    ", id=" + id +
                    ", pokemons=" + Arrays.toString(pokemons) +
                    '}';
        }
    }
    
  3. 执行测试类在这里插入图片描述

其他数据

当我希望用到其他的数据中的内容时,其他数据的占位符就尤为重要

例子
  1. 修改pokemon.properties

    name=${train-name}
    id=1
    
    
    train-name=Ash
    
  2. 运行测试类

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值