springBoot03

Spring Boot基础学习笔记03:Spring Boot两种全局配置和两种注解

创建一个springboot项目

image-20210618201434477

在application.yaml里添加相关配置

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

#修改tomcat默认端口号
server:
  port: 8888
  #修改web虚拟路径
  servlet:
  context-path: /lzy

image-20210618201831676

  • 启动应用,查看控制台

image-20210618201931722

2、对象类型的配置与使用

(1)创建Pet类

image-20210618202035309

package com.wh.demo.enity;

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

@Component
@ConfigurationProperties(prefix = "person.pet")
public class Pet {
//    @Value("${pet.type}")
    private String type; // 类型
//    @Value("${pet.name}")
    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 + '\'' +
                '}';
    }
}

(2)创建Person类
package com.wh.demo.enity;

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

import java.util.List;
import java.util.Map;
@Component
@ConfigurationProperties(prefix = "person")
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 +
                '}';
    }
}
(3)在application.yaml里配置对象
#配置对象
person:
  id: 1
  name: 张三丰
  hobby: 旅游,美食,音乐
  family:
    father: 张云光
    mother: 吴文燕
    grandpa: 张宏宇
    grandma: 唐雨欣
    son: 张君宝
    daughter: 张晓敏
  pet:
    type: 泰迪犬
    name: 瑞瑞

image-20210618202541565

(4)给Person类添加注解
  • 添加注解@Component,交给Spring去管理

  • 添加注解@ConfigurationProperties(prefix = “person”)

image-20210618202904707

  • 注意:采用@ConfigurationProperties注解方式,必须要有set方法,才会自动为Person类所有属性注入相应的值,包括简单类型和复杂类型
(5)给Pet类添加注解
  • 添加注解@Component,交给Spring去管理
  • 添加注解@ConfigurationProperties(prefix = “person.pet”) - 可以不用添加

image-20210618203012439

(6)从Spring容器里获取Person类的实例并输出
  • 实现接口ApplicationContextAware,实现其抽象方法setApplicationContext

image-20210618203351911

  • 声明ApplicationContext对象,并在setApplicationContext里初始化

image-20210618203541718

  • 创建测试方法testPerson(),从Spring容器中获取Person类的实例并输出

image-20210618203616267

  • 运行测试方法testPerson(),查看结果

image-20210618204235226

(8)从Spring容器里获取Pet类的实例并输出
  • 在测试类里添加测试方法testPet()

image-20210618204307940

  • 运行测试方法testPet(),查看结果

image-20210618204340642

  • 给Pet类的属性添加值注解@Value

image-20210618204535157

  • 再次运行测试方法testPet(),查看结果

image-20210618204557202

3、两种属性注解方式的对比

  • 采用@ConfigurationProperties注解方式,必须要有set方法,才会自动为所注解的类的全部属性注入相应的值,包括简单类型和复杂类型(List、Map、Pet……)。
  • 采用@Value注解方式,优点在于可以不要set方法,但是有两点不足:其一、需要一个一个地注入,显得麻烦;其二、对于复杂类型不能注入,比如Map、List、Pet等。

课后作业

任务:修改StudentInfo项目输出学生信息

  • 创建学生实体类Student

image-20210618204833563

编写student类

package com.wh.demo.enity;

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

@Component
@ConfigurationProperties(prefix = "student")
public class student {
    private String id;
    private String name;
    private String gender;
    private int age;
    private String major;
    private String telephone;
    private String email;
    private String hobby;

    public String getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getMajor() {
        return major;
    }

    public void setMajor(String major) {
        this.major = major;
    }

    public String getTelephone() {
        return telephone;
    }

    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getHobby() {
        return hobby;
    }

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

    @Override
    public String toString() {
        return "student{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", gender='" + gender + '\'' +
                ", age=" + age +
                ", major='" + major + '\'' +
                ", telephone='" + telephone + '\'' +
                ", email='" + email + '\'' +
                ", hobby='" + hobby + '\'' +
                '}';
    }
}

配置student对象属性

student:
  id: 19204030
  name: 李晓好昂
  gender:age: 19
  major: 大数据2班
  telephone: 15825702580
  email: maset@qq.com
  hobby:

修改控制器,student()方法里返回student的值

image-20210618205500470

运行启动类

image-20210618205549293

配置student对象属性

student:
  id: 19204030
  name: 李晓好昂
  gender:age: 19
  major: 大数据2班
  telephone: 15825702580
  email: maset@qq.com
  hobby:

在浏览器里访问http://localhost:8080/info

image-20210618210047668

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值