springboot中使用自定义的properties属性
在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;
public String getName() {return name;}public void setName(String name) {this.name = name;}public Long getAge() {return age;}public void setAge(Long age) {this.age = age;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}
}
若使用新的配置文件,需新建一个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;
public String getName() {return name;}public void setName(String name) {this.name = name;}public Long getAge() {return age;}public void setAge(Long age) {this.age = age;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}
}
在Controller中调用,
@Autowired
private PropertiesSetting propertiesSetting;
@Autowired
private Properties1Setting properties1Setting;
@RequestMapping("/properties")public @ResponseBody String properties(){System.out.println("姓名:"+propertiesSetting.getName()+",年龄:"+propertiesSetting.getAge()+",性别:"+propertiesSetting.getSex());System.out.println("姓名:"+properties1Setting.getName()+",年龄:"+properties1Setting.getAge()+",性别:"+properties1Setting.getSex());return "ok";}
在项目的启动类上面添加@EnableConfigurationProperties注解,如下。
@SpringBootApplication
@EnableConfigurationProperties({PropertiesSetting.class,Properties1Setting.class})
public class SpringbootNewApplication {
public static void main(String[] args) {SpringApplication.run(SpringbootNewApplication.class, args);}
运行项目,控制台输出结果,到此配置完成。
(idea为例)如果控制台输出的中文是乱码,需要在
File—>Settings—>File Encodings进行设置如下图。

重新运行,乱码问题解决。
springboot中使用自定义的properties属性的更多相关文章
- Springboot中读取自定义名称properties的
Springboot读取自定义的配置文件时候,使用@value,一定要指定配置文件的位置! 否则报错参数化异常!
- SpringBoot利用注解@Value获取properties属性为null
参考:https://www.cnblogs.com/zacky31/p/8609990.html 今天在项目中想使用@Value来获取Springboot中properties中属性值. 场景:定义 ...
- springboot中使用自定义两级缓存
工作中用到了springboot的缓存,使用起来挺方便的,直接引入redis或者ehcache这些缓存依赖包和相关缓存的starter依赖包,然后在启动类中加入@EnableCaching注解,然后在 ...
- ASP.NET Core中使用自定义MVC过滤器属性的依赖注入
除了将自己的中间件添加到ASP.NET MVC Core应用程序管道之外,您还可以使用自定义MVC过滤器属性来控制响应,并有选择地将它们应用于整个控制器或控制器操作. ASP.NET Core中常用的 ...
- Maven中的-D(Properties属性)和-P(Profiles配置文件)
-D代表(Properties属性) 使用命令行设置属性-D的正确方法是: mvn -DpropertyName=propertyValue clean package 如果propertyName不 ...
- Java:集合,对列表(List)中的自定义对象按属性(字段)排序(正序、倒序)的方法
1. 要求 对列表(List)中的自定义对象,要求能够按照对象的属性(字段)进行排序(正序.倒序). 如:用户对象(Member)有用户名(username).级别(level).出生日期(birth ...
- SpringBoot中设置自定义拦截器
SpringBoot中设置自动以拦截器需要写一个类继承HandlerInterceptorAdapter并重写preHandle方法 例子 public class AuthorityIntercep ...
- SpringBoot中 application.yml /application.properties常用配置介绍
# Tomcat server: tomcat: uri-encoding: UTF-8 max-threads: 1000 min-spare-threads: 30 port: 10444 ser ...
- springboot中使用自定义注解实现策略模式,去除工厂模式的switch或ifelse,实现新增策略代码零修改
前言 思路与模拟业务 源码地址 https://gitee.com/houzheng1216/springboot 整体思路就是通过注解在策略类上指定约定好的type,项目启动之后将所有有注解的typ ...
随机推荐
- linux对文件赋权限的命令chmod的详细说明
指令名称 : chmod使用权限 : 所有使用者 使用方式 : chmod [-cfvR] [--help] [--version] mode file... 说明 : Linux/Unix 的档案调 ...
- 解决axios请求本地的json文件在打包后路径出错问题
vue 项目中使用axios请求了本地项目的static文件夹下的json文件,使用npm run build 打包后,在Hbuilder编辑器打开,页面报错404: 在浏览器打开的路径 http:/ ...
- 本地搭建https服务
首先确保机器上安装了openssl和openssl-devel npm install openssl npm install openssl-devel (安装报错 导致我没安装成功,但是也还是配置 ...
- python3模块: sys
一.简介 sys模块用于提供对python解释器的相关操作. 二.常用函数 sys.argv 命令行参数List,第一个元素是程序本身路径 sys.modules 返回系统导入的模块字段,key是模块 ...
- 【NOIP2016提高组】 Day2 T3 愤怒的小鸟
题目传送门:https://www.luogu.org/problemnew/show/P2831 说个题外话:NOIP2014也有一道题叫做愤怒的小鸟. 这题自测时算错了eps,导致被卡了精度,从1 ...
- 程序模拟HTTP请求
1. 使用HttpClient 前面拼接StringContent string strContent = "client_id=client&client_secret=secre ...
- MYSQL 中的日期操作(不包含跨年问题)
先从一个简单的SQL说起 当前week的第一天:select date_sub(curdate(),INTERVAL WEEKDAY(curdate()) + 1 DAY): DATE_SUB() 函 ...
- Flow类注释解读
参考: (1)https://docs.oracle.com/javase/specs/jls/se7/html/jls-16.html (2)https://docs.oracle.com/java ...
- Phoenix 4.8
From v4.8.0 onwards, user can enable to map it’s schema to the namespace so that any table created w ...
- JAVA 图像操作辅助类
package util; import java.awt.Component; import java.awt.Image; import java.awt.MediaTracker; import ...