当前位置: 首页 > news >正文

使用@PropertySource加载配置文件

1.@PropertySource和@PropertySources注解

1.1.@PropertySource注解概述

@PropertySource注解是Spring 3.1开始引入的配置类注解。通过**@PropertySource注解可以将properties配置文件中的key/value存储到SpringEnvironment中,Environment接口提供了方法去读取配置文件中的值,参数是properties配置文件中定义的key值。当然了,也可以使用@Value注解用${}占位符**为bean的属性注入值。

@PropertySource注解的源代码,如下所示

package org.springframework.context.annotation;import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;import org.springframework.core.io.support.PropertySourceFactory;@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Repeatable(PropertySources.class)
public @interface PropertySource {String name() default "";String[] value();boolean ignoreResourceNotFound() default false;String encoding() default "";Class<? extends PropertySourceFactory> factory() default PropertySourceFactory.class;
}

从@PropertySource的源码中可以看出,可以通过@PropertySource注解指定多个properties文件,使用的形式如下所示:

@PropertySource(value={"classpath:/person.properties", "classpath:/car.properties"})

1.2.@PropertySources注解概述

@PropertySources注解的源码,如下所示:

package org.springframework.context.annotation;import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface PropertySources {PropertySource[] value();
}

@PropertySources注解的源码比较简单,只有一个PropertySource[]数组类型的value属性,那我们如何使用@PropertySources注解指定配置文件呢?其实也很简单,使用如下所示的方式就可以了。

@PropertySources(value={@PropertySource(value={"classpath:/person.properties"}),@PropertySource(value={"classpath:/car.properties"}),
})

2.@PropertySource注解的用法

2.1.使用注解方式获取值

在工程的src/main/resources目录下创建一个配置文件person.properties,内容如下所示:

person.nickName=liqb

在Person类中新增一个nickName字段,如下所示:

package com.tianxia.springannotation.entity;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Value;import java.io.Serializable;/*** Person类* @author liqb* @date 2023-04-21 16:00**/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Person implements Serializable {/*** 姓名*/private String name;/*** 年龄*/private Integer age;/*** 昵称*/@Value("${person.nickName}")private String nickName;public Person(String name, Integer age) {this.name = name;this.age = age;}
}

在MainConfigOfPropertyValues配置类上添加一个@PropertySource注解,如下所示:

package com.tianxia.springannotation.config;import com.tianxia.springannotation.entity.Person;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;/*** 配置类* @author liqb* @date 2023-05-04 11:28**/
@Configuration
// 使用@PropertySource读取外部配置文件中的key/value保存到运行的环境变量中,加载完外部的配置文件以后,使用${}取出配置文件中的值
@PropertySource(value={"classpath:/person.properties"})
public class MainConfigOfPropertyValues {@Bean("propertySource_person")public Person person() {return new Person();}
}

此时Person类的nickName字段的值为空。运行测试方法,输出结果如下所示:

/*** 测试类* @author liqb* @date**/
@Test
public void test() {ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfPropertyValues.class);Person person = applicationContext.getBean(Person.class);System.out.println(person);
}

在这里插入图片描述

此时Person类的nickName字段已经注入liqb这个值了

2.2.使用Environment获取值

使用@PropertySource注解读取外部配置文件中的key/value之后,是将其保存到运行的环境变量中了,所以我们也可以通过运行环境来获取外部配置文件中的值。

运行测试方法,输出结果如下所示:

/*** 使用Environment获取值* @author liqb* @date 2023-05-06 12:44*/
@Test
public void test01() {ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfPropertyValues.class);ConfigurableEnvironment environment = (ConfigurableEnvironment) applicationContext.getEnvironment();String property = environment.getProperty("person.nickName");System.out.println(property);
}

在这里插入图片描述

http://www.lryc.cn/news/66009.html

相关文章:

  • 事务及分布式事务解决方案
  • 【思科、华为、华三、锐捷网络设备巡检命令】
  • 代码随想录算法训练营第五十二天
  • 【Linux网络】传输层中UDP和TCP协议
  • 工具︱ Web3加密浏览器Brave有什么特别之处?
  • 绝对不能错过这份沃尔玛实用插件工具大全
  • 【Java】字符串模板拼接的方法
  • Vue3项目中使用ECharts图表并实现自适应效果
  • 快速跑通环信IM Android Demo
  • leetcode解题思路分析(一百三十九)1190 - 1196 题
  • PHP+vue基于web的小区物业管理管理系统1995a
  • 区间预测 | MATLAB实现QRCNN卷积神经网络分位数回归时间序列区间预测
  • 【AI 导航网站】为了更好的收集 AI 资源,我开发了一个 AI 导航网站
  • 谈谈HMI 的自动化生成技术
  • docker安装elasticsearch
  • Docker:使用dockerFile创建镜像(war包和jar包)
  • 2.基础篇
  • 取代你的可能不是AI,而是比你更会使用AI的人
  • NECCS|全国大学生英语竞赛C类|词汇和语法|语法题|时态 非谓语动词 |19:00~20:15|完形填空·词性转化
  • 【高等数学笔记】Stolz定理
  • 【24】核心易中期刊推荐——图像处理研究大数据及智能处理研究
  • Codeforces Round 870 (Div. 2)【A、B、C、D】
  • BetaFlight统一硬件AOCODARC H7DUAL配置文件讨论
  • 力扣题库刷题笔记682-棒球比赛
  • SpringCloud------Eureka修改实例显示信息、服务发现Discovery、自我保护(六)
  • Java 远程debug,IDEA 远程 Debug 调试
  • 将webrtc的音频模式改为共享模式
  • 电脑cpu占用率高?怎么办?1分钟快速解决!
  • 使用JPA自动生成代码(轻松上手看了就会版)
  • jdk动态代理