注解@Value和@ConfigurationProperties可以用于获取配置文件的属性值,不过有个细节容易被忽略,那就是二者在Spring Boot项目中都是获取默认配置文件的属性值,也就是application.yml或者application.properties的属性值,如果想引用其它配置文件的属性值,就需要使用本篇博文的主角@PropertySource注解。

  使用@PropertySource获取对应的properties文件,再用@ConfigurationProperties或者@Value进行属性映射。在application.properties中配置user.properties文件的路径:

config.path= classpath:self/user.properties

user.properties的配置信息如下:

user.id= 1000
user.userName= root
user.msg= true or not
user.age= 1
user.mobilePhone=15190977799
user.address.tel= 0370-97708099
user.address.name=商丘

  创建一个Bean,用于展示配置的信息,

import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component; import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.io.Serializable; @Getter
@Setter
@Component
//方式①
//@PropertySource("classpath:self/user.properties")
//方式②
@PropertySource(value = {"${config.path}"}, encoding= "UTF-8")
@ConfigurationProperties(prefix = "user")
public class User implements Serializable {
//实现serializable接口
private static final long serialVersionUID = -5259637730100548689L; private Long id;
private String userName;
private String msg;
private Integer age;
private Address address;
private String mobilePhone;
private String remark; /**
* 无参构造器,服务启动过程中就加载
*/
public User() {
System.out.println("正在初始化 user"); } public User(Long id, String userName, Integer age) {
this.id = id;
this.userName = userName;
this.age = age;
} @Override
public String toString() {
return "User{" +
"id=" + id +
", userName='" + userName + '\'' +
", msg='" + msg + '\'' +
", age=" + age +
", address=" + address +
", mobilePhone='" + mobilePhone + '\'' +
", remark='" + remark + '\'' +
'}';
}
}

  如上两种方式都可以配置指定的文件并读取其中的属性。测试用例如下:

    @Autowired
private User user;// 不要用 new User()
@PostMapping("/getUser")
public User getUser() {
logger.info("执行结果user = {}", user);
return user;
}

  工欲善其事,必先利其器。性能与效率是程序猿永恒的追求,对代码如此,对日常搬砖亦如此。Wiener祝愿各位同仁Work Life Balance,效率高,错误少,回家早...

Spring注解之@PropertySource注解加载配置文件的属性的更多相关文章

  1. spring-自动加载配置文件\使用属性文件注入

    在上一篇jsf环境搭建的基础上 , 加入spring框架 , 先看下目录结构 src/main/resources 这个source folder 放置web项目所需的主要配置,打包时,会自动打包到W ...

  2. Spring 创建 IOC 容器时加载配置文件的几种方式

    一.ClassPathXmlApplicationContext 类路径加载 1. 使用 classpath 路径,classpath 前缀加不加都可以. ApplicationContext act ...

  3. 【spring bean】spring中bean的懒加载和depends-on属性设置

    项目结构如下: ResourceBean.java代码: package com.it.res; import java.io.File; import java.io.FileNotFoundExc ...

  4. 【Spring注解驱动开发】使用@PropertySource加载配置文件,我只看这一篇!!

    写在前面 很多小伙伴都在问:冰河,你的Spring专题更新完了吗?怎么感觉像是写了一半啊?我:没有更新完呀,整个专题预计会有70多篇.那怎么更新了一半就去写别的了呢?那是因为有很多其他的小伙伴在后台留 ...

  5. SpringBoot加载配置文件(@PropertySource@importSource@Value)

    情景描述 最近新搭建了一个项目,从Spring迁到了Springboot,为了兼容Spring加载配置文件的风格,所以还想把PropertyPlaceholderConfigurer放在.xml文件里 ...

  6. Spring详解(十)加载配置文件

    在项目中有些参数经常需要修改,或者后期可能会有改动时,那我们最好把这些参数放到properties文件中,在源代码中读取properties里面的配置,这样后期只需要改动properties文件即可, ...

  7. DB数据源之SpringBoot+MyBatis踏坑过程(三)手工+半自动注解配置数据源与加载Mapper.xml扫描

    DB数据源之SpringBoot+MyBatis踏坑过程(三)手工+半自动注解配置数据源与加载Mapper.xml扫描 liuyuhang原创,未经允许禁止转载    系列目录连接 DB数据源之Spr ...

  8. 微服务架构 | *2.3 Spring Cloud 启动及加载配置文件源码分析(以 Nacos 为例)

    目录 前言 1. Spring Cloud 什么时候加载配置文件 2. 准备 Environment 配置环境 2.1 配置 Environment 环境 SpringApplication.prep ...

  9. spring加载配置文件

    spring加载配置文件 1.把applicationContext.xml直接放在WEB-INF/classes下,spring会采用默认的加载方式2.采用在web.xml中配置ContextLoa ...

  10. 【Spring学习笔记-2】Myeclipse下第一个Spring程序-通过ClassPathXmlApplicationContext加载配置文件

    *.hl_mark_KMSmartTagPinkImg{background-color:#ffaaff;}*.hl_mark_KMSmartTagBlueImg{background-color:# ...

随机推荐

  1. 在 Aspire 项目下使用 AgileConfig

    什么是 Aspire .NET Aspire 是一组工具.模板和包,用于构建易于监控的.可投入生产的应用程序..NET Aspire 通过一系列 NuGet 包交付,这些包通过启动或解决现代应用开发中 ...

  2. # 如何引进高级的 IT 自动化项目:一个 3 步走计划

    如果您的团队与大多数 IT 组织一样,您的团队正在执行某种形式的自动化(包括开发和运营),即使只是运行简单的脚本来完成基本任务. 事实上,开始自动化之旅的最佳地点是执行普通的.低技能的任务,例如密码重 ...

  3. 0003 Failed to build the application: build go_beego/src/hello: cannot load

    我使用beego框架快速建立了一个应用,可当我运行 bee run的时候,出现了如下错误 D:\go_beego\src\product>bee run ______ | ___ \ | |_/ ...

  4. oracle的IP访问列表

    Windows版本Oracle 19c. 在sqlnet.ora中添加下面语句 tcp.validnode_checking=yes tcp.invited_nodes=(127.0.0.1,132. ...

  5. Redis 通用命令

    KEYS 语法: KEYS pattern 功能: 返回所有匹配 pattern 的键 可以使用该命令的Redis版本: 1.0.0 时间复杂度: O(N) N指的是在数据库中的键的数量 不建议在生成 ...

  6. 【Linux】5.8 Shell流程控制

    Shell 流程控制 1. 判断语句 1.1 if判断 if else-if else 语法格式: if condition1 then command1 elif condition2 then c ...

  7. 【Spring】JdbcTemplate的使用方法

    概念和准备 什么是 JdbcTemplate Spring 框架对 JDBC 进行封装,使用 JdbcTemplate 方便实现对数据库操作 准备工作 引入相关 jar 包 在 spring 配置文件 ...

  8. leetcode每日一题:使所有字符相等的最小成本

    题目 2712. 使所有字符相等的最小成本 给你一个下标从 0 开始.长度为 n 的二进制字符串 s ,你可以对其执行两种操作: 选中一个下标 i 并且反转从下标 0 到下标 i(包括下标 0 和下标 ...

  9. DelayQueue 底层原理

    一.DelayQueue 底层原理 DelayQueue是一种本地延迟队列,比如希望我们的任务在5秒后执行,就可以使用DelayQueue实现.常见的使用场景有: 订单10分钟内未支付,就取消. 缓存 ...

  10. 【自用】restful api 常用状态码

    GET(SELECT):从服务器取出资源(一项或多项). POST(CREATE):在服务器新建一个资源. PUT(UPDATE):在服务器更新资源(客户端提供改变后的完整资源). PATCH(UPD ...