5 给属性赋值的几种方式

news2024/10/6 0:31:51

        首先创建两个类,Person和Dog。为了可以被扫描到,在前面加入@Component注解。

         Person类如下:

package jiang.com.helloworld.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
public class Person {
    private String name;
    private int age;
    private boolean happy;
    private Date birthday;
    private Map<String,Object> maps;
    private List<Object> lists;

    public Person(String name, int age, boolean happy, Date birthday, Map<String, Object> maps, List<Object> lists) {
        this.name = name;
        this.age = age;
        this.happy = happy;
        this.birthday = birthday;
        this.maps = maps;
        this.lists = lists;
    }

    public Person() {
    }

    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 boolean isHappy() {
        return happy;
    }

    public void setHappy(boolean happy) {
        this.happy = happy;
    }

    public Date getBirthday() {
        return birthday;
    }

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

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

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", happy=" + happy +
                ", birthday=" + birthday +
                ", maps=" + maps +
                ", lists=" + lists +
                '}';
    }
}

        Dog类如下:

package jiang.com.helloworld.pojo;

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

@Component
public class Dog {
    private String name;
    private Integer age;

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

    public Dog(String name, Integer age) {
        this.name = name;
        this.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;
    }

    public Dog() {
    }
}

        其次,在application.yaml配置文件中填入属性。

person:
  name: wangcai${random.uuid}   #wangcai与随机的字符串拼接
  age: ${random.int}
  happy: true
  birth: 1966/6/16
  maps: {k1: v1,k2: v2}

  lists:
    - cats
    - dogs
    - pigs
dogs:
  name: wbq${person.hello:hello}_wangcai    #如果person.hello存在则wbq与person.hello拼接,如果不存在则与hello拼接。
  age: 3

        然后,在实体类另外加上@ConfigurationProperties(prefix = "")。其中,prefix后面对应的是配置文件中的类名。

        Person类:

package jiang.com.helloworld.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 = "person")
public class Person {
    private String name;
    private int age;
    private boolean happy;
    private Date birthday;
    private Map<String,Object> maps;
    private List<Object> lists;

    public Person(String name, int age, boolean happy, Date birthday, Map<String, Object> maps, List<Object> lists) {
        this.name = name;
        this.age = age;
        this.happy = happy;
        this.birthday = birthday;
        this.maps = maps;
        this.lists = lists;
    }

    public Person() {
    }

    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 boolean isHappy() {
        return happy;
    }

    public void setHappy(boolean happy) {
        this.happy = happy;
    }

    public Date getBirthday() {
        return birthday;
    }

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

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

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", happy=" + happy +
                ", birthday=" + birthday +
                ", maps=" + maps +
                ", lists=" + lists +
                '}';
    }
}

        Dog类:

package jiang.com.helloworld.pojo;

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

@Component
@ConfigurationProperties(prefix = "dogs")
public class Dog {
    private String name;
    private Integer age;

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

    public Dog(String name, Integer age) {
        this.name = name;
        this.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;
    }

    public Dog() {
    }
}

        之后编写测试代码。记得在每个类上面加自动注入的注解@Autowired。

package jiang.com.helloworld;

import jiang.com.helloworld.pojo.Dog;
import jiang.com.helloworld.pojo.Person;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class HelloworldApplicationTests {
    @Autowired
    private Person person;
    @Autowired
    private Dog dog;
    @Test
    void contextLoads() {
        System.out.println(person);
        System.out.println(dog);
    }

}

        输出结果如下:

        Person{name='wangcaicc2668d4-0a56-4adb-9ef3-a82f81428430', age=-1447258931, happy=true, birthday=null, maps={k1=v1, k2=v2}, lists=[cats, dogs, pigs]}
Dog{name='wbqhello_wangcai', age=3}

        注意!如果熟悉名是驼峰的方式,如myDog,则可以在配置文件中用my-dog替换。

        如创建了一个Cat类:

package jiang.com.helloworld.pojo;

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

@Component
@ConfigurationProperties(prefix = "cat")
public class Cat {
    private String catName;
    private Integer catAge;

    @Override
    public String toString() {
        return "Cat{" +
                "catName='" + catName + '\'' +
                ", catAge=" + catAge +
                '}';
    }

    public String getCatName() {
        return catName;
    }

    public void setCatName(String catName) {
        this.catName = catName;
    }

    public Integer getCatAge() {
        return catAge;
    }

    public void setCatAge(Integer catAge) {
        this.catAge = catAge;
    }

    public Cat(String catName, Integer catAge) {
        this.catName = catName;
        this.catAge = catAge;
    }

    public Cat() {
    }
}

        配置文件可以写成:

cat:
  cat-name: wbq
  cat-age: 20

        测试代码:

package jiang.com.helloworld;

import jiang.com.helloworld.pojo.Cat;
import jiang.com.helloworld.pojo.Dog;
import jiang.com.helloworld.pojo.Person;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class HelloworldApplicationTests {
    @Autowired
    private Cat cat;
    @Test
    void contextLoads() {
        System.out.println(cat);
    }
}

        测试结果:

​​​​​​​ 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/727851.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

React 之 过渡动画

一、React的过渡动画 在开发中&#xff0c;我们想要给一个组件的显示和消失添加某种过渡动画&#xff0c;可以很好的增加用户体验 可以通过原生的CSS来实现这些过渡动画&#xff0c;但是React社区为我们提供了react-transition-group用来完成过渡动画 React曾为开发者提供过动画…

MySQL表/用户权限等基本操作

MySQL表操作练习题&#xff1a; 第一题&#xff1a; 具体要求如下所示&#xff1a; 1. 创建数据库Market&#xff1a; CREATE DATABASE Market;2. 创建customers表&#xff1a; 表结构如图所示&#xff1a; CREATE TABLE customers( c_num INT(11) PRIMARY KEY, c_name…

TiDB(8):技术内幕之计算

1 关系模型到 Key-Value 模型的映射 在这我们将关系模型简单理解为 Table 和 SQL 语句&#xff0c;那么问题变为如何在 KV 结构上保存 Table 以及如何在 KV 结构上运行 SQL 语句。 假设我们有这样一个表的定义&#xff1a; CREATE TABLE User {ID int,Name varchar(20),Role …

基于springboot+vue的文超市进销存管理系统(源代码+数据库+12000字论文)083

基于springbootvue的文超市进销存管理系统(源代码数据库12000字论文)083 一、系统介绍 (本项目有ssmvue版本) 本系统分为管理员、用户、员工三种角色 用户角色包含以下功能&#xff1a; 登录、注册、购物车、订单提交、商品评论、收藏、充值、收货地址管理、收藏管理、订单…

NXP-无感BLDC代码MCSPTE1AK116_BLDC_6Step代码详解

目录 开发平台 工程目录 Generated_Code Sources Config 电机的参数 BLDC参数 无感模式下的一些参数 Peripherals FTM/PDB/ADC配置参数 actuate_s32k meas_s32k motor_structure state_machine main main()主函数 PORT_IRQHandler() PDB0_IRQHandler() FTM1…

最大正方形 · Maximal Square

链接&#xff1a; 题解&#xff1a;九章算法 - 帮助更多程序员找到好工作&#xff0c;硅谷顶尖IT企业工程师实时在线授课为你传授面试技巧 1.暴力的方法&#xff1a;遍历每一个&#xff08;i&#xff0c;j&#xff09;位置&#xff0c;如果当前点为1&#xff0c;则以当前节点为…

如何编写PlantUml文本绘图时序图

效果如图 代码示例 startumlparticipant "上游" as BEGIN participant "SFTP" as SFTP control "文件系统" as FILE participant "业务系统" as BUSactivate BEGIN BEGIN ->SFTP: 上传文件 activate SFTP autonumber 1.0 FILE -&g…

常用的网址

画图网页&#xff1a; https://www.processon.com/diagrams 二进制转换网页&#xff1a; https://tool.oschina.net/hexconvert/ 在线网络计算器 https://www.sojson.com/convert/subnetmask.html 学习网站掘金&#xff1a; https://juejin.cn 注册外网账号网页&#xff1a…

使用Lambda表达式对List<Map<String,Object>>中key值相同的Map进行分组合并

现有两张表A表和B表&#xff0c;A表存放的是各省市的认证次数&#xff0c;B表存放的是各省市的申领次数&#xff0c;重点关注dq,cs这两个字段&#xff0c;其他的字段可忽略 A表&#xff08;省市认证次数表&#xff09; B表&#xff08;省市申领次数表&#xff09; 项目中有以下…

辅助性能优化——长安链性能分析工具原理及用法

如何提升区块链系统性能是很多开发者都会关注的事&#xff0c;但是有些对区块链并非十分熟悉的开发者可能会感到没有头绪。长安链提供了性能分析工具帮助开发者梳理系统耗时&#xff0c;优化系统性能。下面对长安链性能分析工具原理及使用进行介绍。 一、 概述 time_counter.s…

Windows兼容性设置图文教程,Windows兼容模式怎么设置?服务器兼容是什么意思?服务器兼容性怎么改?

兼容性&#xff08;compatibility&#xff09;是指硬件之间、软件之间或是软硬件组合系统之间的相互协调工作的程度。兼容的概念比较广&#xff0c;相对于硬件来说&#xff0c;几种不同的电脑部件&#xff0c;如CPU、主板、显示卡等&#xff0c;如果在工作时能够相互配合、稳定…

备战秋招004(20230706)

文章目录 前言一、今天学习了什么&#xff1f;二、关于问题的答案1.SE 总结 前言 提示&#xff1a;这里为每天自己的学习内容心情总结&#xff1b; Learn By Doing&#xff0c;Now or Never&#xff0c;Writing is organized thinking. 目前的想法是&#xff0c;根据 Java G…

三种方法将视频转换为AVI格式,与大家分享!

将视频转换为AVI格式是常见的需求&#xff0c;因为AVI格式具有广泛的兼容性和可编辑性。本文将介绍三种常用的方法&#xff0c;包括记灵在线工具、剪映和格式工厂。这些方法简单易行&#xff0c;帮助您将视频文件快速转换为AVI格式&#xff0c;满足不同的需求。 方法一&#x…

EasyCVR接入大量设备级联后出现分组加载异常是什么原因?

EasyCVR可拓展性强、视频能力灵活、部署轻快&#xff0c;可支持的主流标准协议有GB28181、RTSP/Onvif、RTMP等&#xff0c;以及厂家私有协议与SDK接入&#xff0c;包括海康Ehome、海大宇等设备的SDK等&#xff0c;能对外分发RTSP、RTMP、FLV、HLS、WebRTC等格式的视频流。 有用…

接口自动化测试实战之pytest框架+allure讲解

一、前言 本文章主要会讲解Python中pytest框架的讲解&#xff0c;介绍什么是pytest、为何要测试、为何使用以及参考和扩展等等&#xff0c;话不多说&#xff0c;咱们直接进入主题哟。 二、pytest讲解 2.1 什么是pytest&#xff1f; pytest是一款单元测试框架&#xff0c;在…

尚硅谷Docker实战教程-笔记10【高级篇,Docker微服务实战】

尚硅谷大数据技术-教程-学习路线-笔记汇总表【课程资料下载】视频地址&#xff1a;尚硅谷Docker实战教程&#xff08;docker教程天花板&#xff09;_哔哩哔哩_bilibili 尚硅谷Docker实战教程-笔记01【基础篇&#xff0c;Docker理念简介、官网介绍、平台入门图解、平台架构图解】…

浅析住宅小区电动车充电桩的电气设计与平台管理系统

安科瑞电气股份有限公司 上海嘉定201801 摘要&#xff1a;根据目前对于新能源汽车发展规划及政策&#xff0c;以及国内外充电设施的主要类型和技术参数。论述地下车库电动汽车充电桩的供配电系统的设计及设计过程中需要注意的一些问题。 关键词&#xff1a;充电桩&#xff1b…

力扣题库刷题笔记36--有效的数独

1、题目如下&#xff1a; 2、个人Python代码实现如下&#xff1a; 3、个人Python代码思路&#xff1a; 先放一个AI解释的思路&#xff1a; 个人理解&#xff0c;本题思路其实很简单&#xff0c;判断每一行、每一列、每一个3*3的子数独是否存在重复数字&#xff0c;如果存在则返…

不用转化器把pdf转化成Excel,分享两个实用方法!

将PDF文件转换为Excel格式通常是进行数据提取和分析的重要步骤。尽管市面上有许多PDF转Excel的工具&#xff0c;但本文将介绍两种无需使用转换器的实用方法&#xff0c;分别是复制粘贴法和使用记灵在线工具。这些方法简单易行&#xff0c;帮助您快速将PDF中的数据提取到Excel表…

第21章:索引优化与查询优化

一、索引优化与查询优化 1.什么情况下要进行数据库调优 ①索引失效&#xff0c;没有充分利用到索引---索引建立 ②关联查询太多join---SQL优化 ③服务器调优和各个参数的设置---调整my.cnf ④数据过多---分库分表 2.SQL优化的技术 ①物理查询优化&#xff1a;通过索引和…