在application.properties中添加属性
ai.name=明
ai.age=22
ai.sex=男
定义配置类如下,前缀(prefix)可自定义修改,本文为 ai。
@ConfigurationProperties(prefix = “ai”)
public class PropertiesSetting1 {
private String name;
private Long age;
private String sex;

  1. public String getName() {
  2. return name;
  3. }
  4. public void setName(String name) {
  5. this.name = name;
  6. }
  7. public Long getAge() {
  8. return age;
  9. }
  10. public void setAge(Long age) {
  11. this.age = age;
  12. }
  13. public String getSex() {
  14. return sex;
  15. }
  16. public void setSex(String sex) {
  17. this.sex = sex;
  18. }

}
若使用新的配置文件,需新建一个ai.properties。
ai.name=婷婷
ai.age=22
ai.sex=女
配置如下配置类,需要指定ai.properties的位置。
@ConfigurationProperties(prefix = “ai”,locations=”classpath:config/ai.properties”)
public class Properties1Setting {
private String name;
private Long age;
private String sex;

  1. public String getName() {
  2. return name;
  3. }
  4. public void setName(String name) {
  5. this.name = name;
  6. }
  7. public Long getAge() {
  8. return age;
  9. }
  10. public void setAge(Long age) {
  11. this.age = age;
  12. }
  13. public String getSex() {
  14. return sex;
  15. }
  16. public void setSex(String sex) {
  17. this.sex = sex;
  18. }

}

在Controller中调用,
@Autowired
private PropertiesSetting propertiesSetting;
@Autowired
private Properties1Setting properties1Setting;

  1. @RequestMapping("/properties")
  2. public @ResponseBody String properties(){
  3. System.out.println("姓名:"+propertiesSetting.getName()+",年龄:"+propertiesSetting.getAge()
  4. +",性别:"+propertiesSetting.getSex());
  5. System.out.println("姓名:"+properties1Setting.getName()+",年龄:"+properties1Setting.getAge()
  6. +",性别:"+properties1Setting.getSex());
  7. return "ok";
  8. }

在项目的启动类上面添加@EnableConfigurationProperties注解,如下。

@SpringBootApplication
@EnableConfigurationProperties({PropertiesSetting.class,Properties1Setting.class})
public class SpringbootNewApplication {

  1. public static void main(String[] args) {
  2. SpringApplication.run(SpringbootNewApplication.class, args);
  3. }

运行项目,控制台输出结果,到此配置完成。
(idea为例)如果控制台输出的中文是乱码,需要在
File—>Settings—>File Encodings进行设置如下图。

重新运行,乱码问题解决。

springboot中使用自定义的properties属性的更多相关文章

  1. Springboot中读取自定义名称properties的

    Springboot读取自定义的配置文件时候,使用@value,一定要指定配置文件的位置!  否则报错参数化异常!

  2. SpringBoot利用注解@Value获取properties属性为null

    参考:https://www.cnblogs.com/zacky31/p/8609990.html 今天在项目中想使用@Value来获取Springboot中properties中属性值. 场景:定义 ...

  3. springboot中使用自定义两级缓存

    工作中用到了springboot的缓存,使用起来挺方便的,直接引入redis或者ehcache这些缓存依赖包和相关缓存的starter依赖包,然后在启动类中加入@EnableCaching注解,然后在 ...

  4. ASP.NET Core中使用自定义MVC过滤器属性的依赖注入

    除了将自己的中间件添加到ASP.NET MVC Core应用程序管道之外,您还可以使用自定义MVC过滤器属性来控制响应,并有选择地将它们应用于整个控制器或控制器操作. ASP.NET Core中常用的 ...

  5. Maven中的-D(Properties属性)和-P(Profiles配置文件)

    -D代表(Properties属性) 使用命令行设置属性-D的正确方法是: mvn -DpropertyName=propertyValue clean package 如果propertyName不 ...

  6. Java:集合,对列表(List)中的自定义对象按属性(字段)排序(正序、倒序)的方法

    1. 要求 对列表(List)中的自定义对象,要求能够按照对象的属性(字段)进行排序(正序.倒序). 如:用户对象(Member)有用户名(username).级别(level).出生日期(birth ...

  7. SpringBoot中设置自定义拦截器

    SpringBoot中设置自动以拦截器需要写一个类继承HandlerInterceptorAdapter并重写preHandle方法 例子 public class AuthorityIntercep ...

  8. SpringBoot中 application.yml /application.properties常用配置介绍

    # Tomcat server: tomcat: uri-encoding: UTF-8 max-threads: 1000 min-spare-threads: 30 port: 10444 ser ...

  9. springboot中使用自定义注解实现策略模式,去除工厂模式的switch或ifelse,实现新增策略代码零修改

    前言 思路与模拟业务 源码地址 https://gitee.com/houzheng1216/springboot 整体思路就是通过注解在策略类上指定约定好的type,项目启动之后将所有有注解的typ ...

随机推荐

  1. [bug] VUE 的 template 中使用 ES6 语法导致页面空白

    如果你在 template 中,使用了 es6 及以上的语法,那么,在部分ios.安卓.微信浏览器中,打开页面后显示一片空白内容.如下: <ul id="example-1" ...

  2. Android之AppWidget

    1.Widget设计步骤 需要修改三个XML,一个class: 1)第一个xml是布局XML文件(如:main.xml),是这个widget的.一般来说如果用这个部件显示时间,那就只在这个布局XML中 ...

  3. day 68crm(5) 分页器的进一步优化,以及在stark上使用分页器,,以及,整理代码,以及stark组件search查询

    前情提要: 本节内容 自定制分页器 保存及查询记录 代码整理, stark组件search 查询    一:自定制分页器 page 1:创建类 Pagination  # 自定制分页器 _ _init ...

  4. docker微服务部署之:七、Rancher进行微服务扩容和缩容

    docker微服务部署之:六.Rancher管理部署微服务 Rancher有两个特色用起来很方便,那就是扩容和缩容. 一.扩容前的准备工作 为了能直观的查看效果,需要修改下demo_article项目 ...

  5. Java中equals,hashcode

         在Java语言中,Object对象中包含一个equals和hashCode方法,其中hashCode方法是由JVM本地代码(native code)实现的,返回值是一个有符号的32位整数,对 ...

  6. 配置文件备份方案(expect+shell)

    需求描述:备份所有线上服务器squid.httpd.mysql.nginx的配置文件 环境:在公司内网采用expect+shell脚本模式,进行批量备份.expect脚本通过ssh登录服务器,从本地c ...

  7. openssl生成RSA格式的公私钥,并转为pkcs8格式

    第一步:生成私钥,这里我们指定私钥的长度为2048 openssl genrsa -out rsa_private_key.pem 2048 第二步:根据私钥生成对应的公钥: openssl rsa ...

  8. HTML BODY 背景图片

    内嵌: background:url(背景图片路径)  no-repeat;/*不重复默认在左上方*/ background:url(背景图片路径)  no-repeat center;/*不重复背景 ...

  9. Check类中的incl、union,excl,diff,intersect

    定义一些类,这些类之间有父子关系,如下: class Father{} class Son1 extends Father{} class Son2 extends Father{} class To ...

  10. Linux内存信息查看——free命令

    free 命令可以显示系统已用和空闲的内存情况.包括物理内存.交互区内存(swap)和内核缓冲区内存(buffer).共享内存将被忽略.在Linux系统监控的工具中,free命令是最经常使用的命令之一 ...