给SpringBoot属性赋值的几种方式

一、使用@Value注解

一个@Value注解给一个属性赋值

package com.modi.hello.pojo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class Teacher {
    // 通过@Value直接给属性赋值
    @Value("3")
    private int cid;
    @Value("张老师")
    private String nameTeacher;

    public Teacher() {
    }

    public Teacher(int cid, String nameTeacher) {
        this.cid = cid;
        nameTeacher = nameTeacher;
    }

    public int getCid() {
        return cid;
    }

    public void setCid(int cid) {
        this.cid = cid;
    }

    public String getNameTeacher() {
        return nameTeacher;
    }

    public void setNameTeacher(String nameTeacher) {
        this.nameTeacher = nameTeacher;
    }

    @Override
    public String toString() {
        return "Teacher{" +
                "cid=" + cid +
                ", nameTeacher='" + nameTeacher + '\'' +
                '}';
    }
}

二、通过application.yaml配置文件

在application.yaml配置文件中为属性赋值
在这里插入图片描述
在需要给属性赋值的类中使用@ConfigurationProperties注解为属性赋值。具体写法:
@ConfigurationProperties(prefix = “student”)

package com.modi.hello.pojo;

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

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

@Component
@ConfigurationProperties(prefix = "student")
public class Student {
    private String name;
    private int age;
    private Teacher teacher;
    private Map<String,Object> map;
    private List<String> list;
    private Date birthday;

    public Student() {
    }

    public Student(String name, int age, com.modi.hello.pojo.Teacher teacher, Map<String, Object> map, List<String> list, Date birthday) {
        this.name = name;
        this.age = age;
        this.teacher = teacher;
        this.map = map;
        this.list = list;
        this.birthday = birthday;
    }

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

    public com.modi.hello.pojo.Teacher getTeacher() {
        return teacher;
    }

    public void setTeacher(Teacher teacher){
        this.teacher = teacher;
    }

    public Map<String, Object> getMap() {
        return map;
    }

    public void setMap(Map<String, Object> map) {
        this.map = map;
    }

    public List<String> getList() {
        return list;
    }

    public void setList(List<String> list) {
        this.list = list;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", teacher=" + teacher +
                ", map=" + map +
                ", list=" + list +
                ", birthday=" + birthday +
                '}';
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值