SpringBoot中自定义properties文件配置参数并带有输入提示

1. 创建配置类

在项目中创建一个参数映射类如下

@ConfigurationProperties(prefix = "user.info")
public class MyProperties {
    private String name;
    private Integer age;

    public String getName() {
        return name;
    }

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

    public String getAge() {
        return age;
    }

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

2. 编写提示信息

在项目中按此路径创建一个json文件 
resources/META-INF/spring-configuration-metadata.json

{
  "hints": [
    {
      "name": "user.info.age",
      "values": [
        {
          "value": 18
        },
        {
          "value": 20
        }
      ]
    }
  ],
  "groups": [
    {
      "sourceType": "org.xavier.config.properties.MyProperties",
      "name": "随便乱取",
      "type": "org.xavier.config.properties.MyProperties"
    }
  ],
  "properties": [
    {
      "sourceType": "org.xavier.config.properties.MyProperties",
      "name": "user.info.Name",
      "type": "java.lang.String"
    },
    {
      "sourceType": "org.xavier.config.properties.MyProperties",
      "name": "user.info.age",
      "type": "java.lang.Integer"
    }
  ]
}

记得重新编译项目,编译后才生效

3. 编写配置信息

在application.properties 里就可以给配置项赋值了,例如:

user.info.name=张三

user.info.age=18



原文地址:https://www.cnblogs.com/netcorner/p/10564196.html

时间: 2024-08-29 05:06:02

SpringBoot中自定义properties文件配置参数并带有输入提示的相关文章

通过.properties文件配置参数

通过.properties文件配置参数的好处最重要的就是,如果在维护时这个参数被频繁的修改,只需要在.properties文件里面修改一次即可. 在src文件夹下新建.properties文件,在里面添加变量和值. 那么如何在代码中使用.properties文件的变量呢? private static String wsUrl; static{ InputStream inputStream = SpaceInfoImpl.class.getResourceAsStream("/Constant

详解Springboot中自定义SpringMVC配置

详解Springboot中自定义SpringMVC配置 WebMvcConfigurer接口 ? 这个接口可以自定义拦截器,例如跨域设置.类型转化器等等.可以说此接口为开发者提前想到了很多拦截层面的需求,方便开发者自由选择使用.由于Spring5.0废弃了WebMvcConfigurerAdapter,所以WebMvcConfigurer继承了WebMvcConfigurerAdapter大部分内容. WebMvcConfigurer接口中的方法 举例1:configurePathMatch配置

五种方式让你在java中读取properties文件内容不再是难题

一.背景 最近,在项目开发的过程中,遇到需要在properties文件中定义一些自定义的变量,以供java程序动态的读取,修改变量,不再需要修改代码的问题.就借此机会把Spring+SpringMVC+Mybatis整合开发的项目中通过java程序读取properties文件内容的方式进行了梳理和分析,先和大家共享. 二.项目环境介绍 Spring 4.2.6.RELEASE SpringMvc 4.2.6.RELEASE Mybatis 3.2.8 Maven 3.3.9 Jdk 1.7 Id

在JSP页面中读取properties文件

在做web开发时,经常遇到要修改一下配置信息.如果把这些配置信息写在代码中,后期的维护便会比较麻烦.所以,一般都是把配置信息写在配置文件里面. 在JSP文件中,如果想要调用properties文件中的变量,则要在有文件中引入 java.util.ResourceBundle 类: <%@ page contentType="text/html; charset=UTF-8" import="java.util.ResourceBundle" %> 已知配

SpringBoot读取application.properties文件

SpringBoot读取application.properties文件,通常有3种方式 1. @Value  例如: @Value("${spring.profiles.active}") private String profileActive;------相当于把properties文件中的spring.profiles.active注入到变量profileActive中 2. @ConfigurationProperties  例如: @Component@Configurat

java读取package中的properties文件java.util.MissingResourceException

文件结构: /build/classes/d914/Hello.class /build/classes/d914/mess.properties /build/classes/d914/mess_zh_CN.properties /build/classes/d914/mess_en_US.properties 在eclipse中运行如下代码: package d914; import java.util.ResourceBundle; import java.util.Locale; pub

springboot中读取自定义properties文件

一.在高版本的springboot中,@ConfigurationProperties(prefix = "wisely2",locations = "classpath:wisely.properties")这个注解不支持了,所以我们要另辟蹊径 二.使用组合式注解: 1.自定义config.properties文件: 1 config.fileServer=/root/jzyp/staticserver/webapps/ROOT/server 2 config.s

spring配置中,properties文件以及xml文件配置问题

spring方便我们的项目快速搭建,功能强大,自然也会是体系复杂! 这里说下配置文件properties管理的问题. 一些不涉及到代码逻辑,仅仅只是配置数据,可以放在xxxx.properties文件里面,项目功能复杂的时候,往往properties文件很多,这时,就比较容易让人困惑,有些properties的文件内容总是加载不起来,应用启动时,就不断爆出错误,说某某参数加载失败,这个是什么原因呢? 其实,这个是spring配置的时候,org.springframework.beans.fact

springboot中自定义根路径的配置

Spring boot默认是/ ,这样直接通过http://ip:port/就可以访问到index页面,如果要修改为http://ip:port/path/ 访问的话,那么需要在Application.properties文件中加入server.context-path = /你的path,比如:spring-boot,那么访问地址就是http://ip:port/spring-boot 路径. server.context-path=/spring-boot