spring的属性绑定

前言

我准备对我所学的spring属性绑定进行总结:

1.spring属性绑定需要一个实体类 如  StudentProperties.Java

2.需要在application.properties文件里面配置

3.需要一个ApplicationTest类来测试

一.创建实体类
StudentProperties.Java
package com.sunshine.app.entity;

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

import java.time.Period;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Component
@ConfigurationProperties(prefix = "student")
public class StudentProperties {
    private int age;
    private String name;

    private Clazz clazz;

    private String []course;

    private List <Person> Schoolmate;

    private Map<String, Object> result;

    public int getAge() {
        return age;
    }

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

    public String getName() {
        return name;
    }

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

    public String[] getCourse() {
        return course;
    }

    public void setCourse(String[] course) {
        this.course = course;
    }

    public List<Person> getSchoolmate() {
        return Schoolmate;
    }

    public void setSchoolmate(List<Person> schoolmate) {
        Schoolmate = schoolmate;
    }

    public Map<String, Object> getResult() {
        return result;
    }

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

    public Clazz getClazz() {
        return clazz;
    }

    public void setClazz(Clazz clazz) {
        this.clazz = clazz;
    }

    @Override
    public String toString() {
        return "StudentProperties{" +
                "age=" + age +
                ", name='" + name + '\'' +
                ", clazz=" + clazz +
                ", course=" + Arrays.toString(course) +
                ", Schoolmate=" + Schoolmate +
                ", result=" + result +
                '}';
    }
}
Clazz.java
package com.sunshine.app.entity;

public class Clazz {
    private String name;
    private  String id;

    public String getName() {
        return name;
    }

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

    public String getId() {
        return id;
    }

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

    @Override
    public String toString() {
        return "Clazz{" +
                "name='" + name + '\'' +
                ", id='" + id + '\'' +
                '}';
    }
}
Person.java 
package com.sunshine.app.entity;

public class Person {
    private String name;
    private int sex;

    public String getName() {
        return name;
    }

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

    public int getSex() {
        return sex;
    }

    public void setSex(int sex) {
        this.sex = sex;
    }

    @Override
    public String  toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", sex=" + sex +
                '}';
    }
}
二.创建一个application.properties文件,并绑定实体类的属性
server.port= 8080
spring.mvc.view.prefix=/
spring.mvc.view.suffix=.html
student.age=1
student.name= 叶飞

student.clazz.name = clazz1

//prefix.属性名[索引] = 值

student.course[0] =100
student.course[1] =200

//prefix.属性名[索引].对象的属性  = 值

student.schoolmate[0].name= 断德
student.schoolmate[0].sex=1

student.schoolmate[1].name= 应用
student.schoolmate[1].sex=1

//map
//prefix.属性名.key具体值  = 复杂对象.属性

student.result.s1.score = score1
student.result.s1.id = 1

student.result.c1.score = score2
student.result.c1.id 
三.用测试类测试
ApplicationTest.java
package com.sunshine.app;

import com.sunshine.app.entity.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.util.Arrays;

@Slf4j(topic = "e")
@SpringBootApplication
public class ApplicationTest {
    public static void main(String[] args) {

        var context =  SpringApplication.run(ApplicationTest.class,args);
        log.debug( String.valueOf(context.getBean(StudentProperties.class).getAge()));
        log.debug( String.valueOf(context.getBean(StudentProperties.class).getName()));
        log.debug( String.valueOf(context.getBean(StudentProperties.class).getSchoolmate()));
        log.debug(Arrays.toString(context.getBean(StudentProperties.class).getCourse())  );
        log.debug( String.valueOf(context.getBean(StudentProperties.class).getResult() ));
        log.debug( String.valueOf(context.getBean(StudentProperties.class).getClazz()));
 }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值