• 8.springboot的自定义配置(@value注解和@ConfigurationProperties)


    一、@value注解
    @Value("${key}") , key 来自 application.properties(yml)
    项目结构

    1.首先在核心配置文件application.properties中编写key=value格式的数据

    1. #配置端口号
    2. server.port=8082
    3. #context-path
    4. server.servlet.context-path=/myboot
    5. #自定义key=value
    6. school.name=清华大学
    7. school.website=www.tsinghua.edu.cn
    8. school.address=北京海淀区
    9. site=www.baidu.com

    2.编写controller包下的HelloContoller类

    1. package com.it.controller;
    2. import org.springframework.beans.factory.annotation.Value;
    3. import org.springframework.stereotype.Controller;
    4. import org.springframework.web.bind.annotation.RequestMapping;
    5. import org.springframework.web.bind.annotation.ResponseBody;
    6. @Controller
    7. public class HelloController {
    8. @Value("${server.port}")
    9. private Integer port;
    10. @Value("${server.servlet.context-path}")
    11. private String contextPath;
    12. @Value("${school.name}")
    13. private String name;
    14. @Value("${site}")
    15. private String site;
    16. @RequestMapping(value = "/data")
    17. @ResponseBody
    18. public String queryData(){
    19. return name+",site="+site+",项目的访问地址="+contextPath+",使用的端口="+port;
    20. }
    21. }

    3.运行Application类的项目主函数,自动运行项目

     项目结果

    二、@ConfigurationProperties 注解

    将整个文件映射成一个对象,用于自定义配置项比较多的情况

    在 com.bjpowernode.springboot.config 包下创建 SchoolInfo 类,并为该 类加上Component 和 ConfigurationProperties 注解,prefix 可以不指定,如果不指定,那么 会去配置文件中寻找与该类的属性名一致的配置,prefix 的作用可以区分同名配置
    1.SchoolInfo类
    1. package com.it.entity;
    2. import org.springframework.boot.context.properties.ConfigurationProperties;
    3. import org.springframework.stereotype.Component;
    4. @Component
    5. @ConfigurationProperties(prefix = "school")
    6. public class SchoolInfo {
    7. private String name;
    8. private String website;
    9. private String address;
    10. @Override
    11. public String toString() {
    12. return "SchoolInfo{" +
    13. "name='" + name + '\'' +
    14. ", website='" + website + '\'' +
    15. ", address='" + address + '\'' +
    16. '}';
    17. }
    18. public String getName() {
    19. return name;
    20. }
    21. public void setName(String name) {
    22. this.name = name;
    23. }
    24. public String getWebsite() {
    25. return website;
    26. }
    27. public void setWebsite(String website) {
    28. this.website = website;
    29. }
    30. public String getAddress() {
    31. return address;
    32. }
    33. public void setAddress(String address) {
    34. this.address = address;
    35. }
    36. }

    2.controller包下的SchoolController类

    1. package com.it.controller;
    2. import com.it.entity.SchoolInfo;
    3. import org.springframework.beans.factory.annotation.Value;
    4. import org.springframework.stereotype.Controller;
    5. import org.springframework.web.bind.annotation.RequestMapping;
    6. import org.springframework.web.bind.annotation.ResponseBody;
    7. import javax.annotation.Resource;
    8. @Controller
    9. public class HelloController {
    10. @Resource
    11. private SchoolInfo info;
    12. @RequestMapping(value = "/info")
    13. @ResponseBody
    14. public String queryInfo(){
    15. return "SchoolInfo对象=="+info.toString();
    16. }
    17. }

    运行程序

     三、警告解决

    在 SchoolInfo 类中使用了 ConfigurationProperties 注解,IDEA 会出现一个警告,不影响程序的执行 。
    点击 open documentnation 跳转到网页,在网页中提示需要加一个依赖,我们将这个依赖拷贝,粘贴到 pom.xml 文件中 。
  • 相关阅读:
    数据库问题汇总
    Pandas数据处理分析系列6-数据特征分析
    26、Qt调用.py文件中的函数
    重组蛋白/细胞因子的实验操作
    aspose-slides-22.5-jdk16
    2022一带一路暨金砖国家技能发展与技术创新大赛小程序应用开发国内赛-二等奖经验分享
    使用IDEA创建SpringCloud项目
    【强化学习论文合集】ICLR-2021 强化学习论文
    uni-app + mui-player & vue + mui-player 播放flv文件
    libopus 实现pcm 编码到opus
  • 原文地址:https://blog.csdn.net/weixin_59334478/article/details/126596102