SpringBoot学习4之配置map list

第一步:在com.pp.bean下创建一个Car类

package com.pp.springboothelloworld.bean;

public class Car {
    private String name;
    private Integer age;

    @Override
    public String toString() {
        return "Car{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public String getName() {
        return name;
    }

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

    public Integer getAge() {
        return age;
    }

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

第二步:在Persion中添加一句private Car car,并且将@ConfigurationProperties(prefix = “persion”)注释取消

package com.pp.springboothelloworld.bean;

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.List;
import java.util.Map;

@Component  //@Components说明把当前的内容放在容器中去
//@PropertySource(value = "persion.properties")
@ConfigurationProperties(prefix = "persion")
public class Persion {
    // @Value("${persion.name}")的使用用,获取到配置文件的persion.name然后赋值给这里的name
    @Value("${persion.name}")
    private String name;
    private Integer age;
    private Boolean sex;

    private Map<String,Object> maps;
    private List<Object> lists;

    private Car car;
    @Override
    public String toString() {
        return "Persion{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", sex=" + sex +
                ", maps=" + maps +
                ", lists=" + lists +
                ",car="+car+
                '}';
    }

    public Car getCar() {
        return car;
    }

    public void setCar(Car car) {
        this.car = car;
    }

    public String getName() {
        return name;
    }

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

    public Integer getAge() {
        return age;
    }

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

    public Boolean getSex() {
        return sex;
    }

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

    public Map<String, Object> getMaps() {
        return maps;
    }

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

    public List<Object> getLists() {
        return lists;
    }

    public void setLists(List<Object> lists) {
        this.lists = lists;
    }
}


第三步:在application.perproties

#浏览器端口号
server.port=8080   

persion.name=张三
persion.age=${random.int}

persion.maps.k1=a1
persion.maps.k2=a2

persion.lists=a,b,c

persion.car.name=宝马

第四步:在com.pp.controller中的HelloController类中定义一个Persion并且进行注解

package com.pp.springboothelloworld.controller;

import com.pp.springboothelloworld.bean.Persion;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HelloController {
    @Value("${persion.name}")
    private String name;
    @Autowired
    private Persion persion;
    @ResponseBody
    @RequestMapping("hello")
    public String hello(){
        return "hello word"+persion.getMaps().get("k1");
    }
    
}

注解里面将Persion的persion.getMaps().get(“k1”)获取注解文件的为k1的值

第五步:上面步骤运行结果

在这里插入图片描述

第六步:如果我们在HelloController中hello中修改下面内容

package com.pp.springboothelloworld.controller;

import com.pp.springboothelloworld.bean.Persion;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HelloController {
    @Value("${persion.name}")
    private String name;
    @Autowired
    private Persion persion;
    @ResponseBody
    @RequestMapping("hello")
    public String hello(){
        return "hello word"+persion.getLists();
    }
}

结果显示
在这里插入图片描述
上面就是对bean包下面的类进行测试,其中通过在application.perperties中定义好数据在去控制层进行编码,最后在浏览器总运行查看页面。

扩展:

1.除了上面的方法,我们也可以在 est/java/com/pp/springhelloworld类中定义的又测试方法

package com.pp.springboothelloworld;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class SpringBootHelloworldApplicationTests {

    @Test
    void contextLoads() {
    }

}

2.然后我们在里面进行写方法

package com.pp.springboothelloworld;

import com.pp.springboothelloworld.bean.Persion;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class SpringBootHelloworldApplicationTests {
    @Autowired
    private Persion persion;
    @Test
    void contextLoads() {
        System.out.println(persion.getLists());
    }
}

上面的内容中,我们先使用注解对private Persion persion进行注解,然后再测试方法里面对persion.getLists()进行打印到控制台。

3.运行结果

在这里插入图片描述
源码地址:https://gitee.com/yangforever/project-learning/tree/master/demo/SpringBoot/spring-boot-helloworld2

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值