当前位置:   article > 正文

Spring Boot学习3——Spring Boot全局配置和注解_spring boot 全局对象

spring boot 全局对象

全局配置

Spring Boot使用一个application.properties或application.yml/application.yaml的文件作为全局配置文件,该文件放在 【src/main/resources】目录或者类路径的 【/config】,一般为放在resources目录。我们可以在 application.properties / application.yml文件中定义Spring Boot定义项目的相关属性,、这些属性可以是系统属性、环境变量、命令参数等信息,也可以是自定义配置文件名称和位置。

掌握两个注解

  • @ConfigurationProperties注入属性
  • @Value注入属性

一、创建项目PropertiesDemo

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

二、配置tomcat端口号和web虚拟路径

  • 打开application.properties文件,此文件是系统自动生成的,初始状态是空文件
    在这里插入图片描述

三、创建Person类和Pet类

  • 创建bean子包
    在这里插入图片描述
    Person.java
package net.lj.lesson03.bean;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;


public class Person {
    private int id; //编号
    private String name; //姓名
    private List<String> hobby; //爱好
    private Map<String,String> family; //家庭成员
    private Pet pet; //宠物

    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;
    }

    public List<String> getHobby() {
        return hobby;
    }

    public void setHobby(List<String> hobby) {
        this.hobby = hobby;
    }

    public Map<String, String> getFamily() {
        return family;
    }

    public void setFamily(Map<String, String> family) {
        this.family = family;
    }

    public Pet getPet() {
        return pet;
    }

    public void setPet(Pet pet) {
        this.pet = pet;
    }

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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68

Pet.java

package net.lj.lesson03.bean;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * 宠物类
 */
public class Pet {
    private String type; //类型
    private String name; //名字

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getName() {
        return name;
    }

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

    @Override
    public String toString() {
        return "Pet{" +
                "type='" + type + '\'' +
                ", name='" + name + '\'' +
                '}';
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 检查下项目编码是否为UFT-8,否则可能出现乱码
    在这里插入图片描述

三、在application.properties里配置对象

配置对象
person.id=1
person.name=二哈
person.hobby=拆家,拆楼
person.family.father=哈狗
person.family.mother=傻狗
person.family.grandpa=二狗
person.famliy.grandma=土狗
person.family.son=玛卡巴卡
person.family.daughter=汤不理吧
person.pet.type=哈士奇
person.pet.name=黑子
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

四、给Person类和Pet类添加注解

在这里插入图片描述
在这里插入图片描述

五、编写测试类

package net.lj.lesson03;

import net.lj.lesson03.bean.Person;
import net.lj.lesson03.bean.Pet;
import org.junit.jupiter.api.Test;
import org.springframework.beans.BeansException;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

@SpringBootTest
class PropertiesDemoApplicationTests implements ApplicationContextAware {//应用容器感知接口
    private ApplicationContext context;//声明应用容器

    @Test
    void contextLoads() {
}

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        context = applicationContext;
    }

    @Test
    public void testPerson() {
        //从应用容器里按名称获取person实例
        Person person = (Person) context.getBean("person");
        //输出Person对象
        System.out.println(person);
    }

    @Test
    public void testPet(){
        //从应用容器里按名称获取Pet实例
        Pet pet = (Pet) context.getBean("pet");
        //输出Pet对象
        System.out.println(pet);

    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 现在还不能运行项目,因为会乱码,需要使用JDK工具native2ascii.exe将汉字处理成uncode编码

1.打开Terminal面板,输入以下命令

cd src/main/resources

native2ascii -encoding utf8 application.properties
  • 1
  • 2
  • 3

2.将转码的内容复制到application.properties文件
在这里插入图片描述
在这里插入图片描述

六、运行测试类

在这里插入图片描述
在这里插入图片描述

使用value注解

1.修改application.properties文件

在这里插入图片描述

2.修改Pet类注解方式

在这里插入图片描述

3.运行Pet测试类

在这里插入图片描述

  • @ConfigurationProperties注解方式,必须要有set方法才会自动为所注解的类的全部属性注入相应的值

  • @Value注解方式,可以不要set方法,但需要一个一个地注入,比较麻烦,而且对于复杂类型不能注入

使用yaml方式

  • yaml具有层次结构,并且支持中文

一、备份application.properties文件

  • 更改application.properties后缀名,使其失效
  • 创建application.yaml文件
    在这里插入图片描述

二、配置yaml对象属性

  • 注意层次,跟Python一样要求缩进
#配置服务器
server:
  port: 8888
  servlet:
    context-path: /lzy

#配置person对象
person:
  id: 1
  name: 张三
  hobby:
    吃东西
    跳舞
    唱歌
  family: {
    father: 李四,
    mother: 王五,
    grandpa: 赵六,
    grandma: 孙七,
    son: 王八,
    daughter: 李二
  }
  pet:
    type: 斗牛犬
    name: 拉拉

  #配置pet对象
pet:
  type: 牧羊犬
  name: 大黑
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

三、运行测试

在这里插入图片描述
在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/262897
推荐阅读
相关标签
  

闽ICP备14008679号