• springboot web 05 springboot通过@ConfigurationProperties注解springboot获取自定义配置



    在这里插入图片描述

    1、application.properties

    server.port=8081
    school.name=bjpowernode
    school.website=www.bjpowernode.com
    city.name=beijing
    city.website=www.beijing.com
    
    • 1
    • 2
    • 3
    • 4
    • 5

    2、School.class

    package com.bjpowernode.springboot.model;
    
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.stereotype.Component;
    
    @Component  //将此类加载到spring容器中
    @ConfigurationProperties(prefix = "school")  //此school为配置文件中key值的前缀
    public class School {
        private String name;
        private String website;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getWebsite() {
            return website;
        }
    
        public void setWebsite(String website) {
            this.website = website;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    3、City.class

    package com.bjpowernode.springboot.model;
    
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.stereotype.Component;
    
    @Component  //将此类加载到spring容器中
    @ConfigurationProperties(prefix = "city")
    public class City {
        private String name;
        private String website;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getWebsite() {
            return website;
        }
    
        public void setWebsite(String website) {
            this.website = website;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    4、IndexController.class

    package com.bjpowernode.springboot.web;
    
    import com.bjpowernode.springboot.model.City;
    import com.bjpowernode.springboot.model.School;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    @Controller
    public class IndexController {
        @Autowired
        private School school;
        @Autowired
        private City city;
    
        @RequestMapping(value = "/school")
        public @ResponseBody Object myschool(){
            return "school.name="+school.getName()+", school.website="+school.getWebsite();
        }
    
        @RequestMapping(value = "/city")
        public @ResponseBody Object mycity(){
            return "city.name="+city.getName()+", city.website="+city.getWebsite();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    5、运行结果

    在这里插入图片描述
    在这里插入图片描述

  • 相关阅读:
    新疆维吾尔自治区工程系列建筑专业职称评审条件
    STM32 如何定位导致发生 hard fault 的代码段
    java时间日期类
    Git相关配置及问题解决
    学习OpenCV——cv::inpaint函数(三)
    Java定时任务实现
    Python练习题:实现三数之和
    阿里低代码引擎怎么样,好不好用?
    54_Pandas将DataFrame、Series转换为字典 (to_dict)
    LLama的激活函数SwiGLU 解释
  • 原文地址:https://blog.csdn.net/weixuan_/article/details/127732902