19、属性赋值-@PropertySource加载外部配置文件
19、属性赋值-@PropertySource加载外部配置文件
- 加载外部配置文件的注解
19.1 【xml】
- 在原先的xml 中需要 导入context:property-placeholder 声明,然后使用${nickName}取值
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--包扫描 , 只要标注了 @Controller 、@Service、@Repository、@Component的类 都会被扫描-->
<context:component-scan base-package="com.hw.springannotation"></context:component-scan>
<!--导入配置文件中的属性值-->
<context:property-placeholder location="classpath:pension.properties"></context:property-placeholder>
<bean id="pension" class="com.hw.springannotation.beans.Pension" scope="prototype" init-method="" destroy-method="">
<property name="name" value="hw"></property>
<property name="age" value="18"></property>
<property name="nickName" value="${nickName}"></property>
</bean>
</beans>
19.2 【注解】@PropertySource
- 使用@PropertySource来读取外部配置文件中的k/v值 保存到运行的环境变量中,加载完就可以使用${变量名}取出
// 使用@PropertySource来读取外部配置文件中的k/v值 保存到运行的环境变量中,加载完就可以使用${}取出
@PropertySource(value = {"classpath:/pension.properties"})
@Configuration
public class MainConfigOfPropertyValues {
@Bean
public Pension pension() {
return new Pension();
}
}
19.3 pension类新添加nickName属性
@Value("张三")
private String name;
@Value("#{22-1}")
private Integer age;
@Value("${nickName}")
private String nickName;
19.4 新建pension.properties配置文件
- 注意,在这里一定要注意项目的编码,本项目都是采用UTF-8编码,配置文件也是(idea默认是GBK,会导致读取的配置文件乱码)
- 可以在idea设置中搜索File-Encodeings,更改下面的配置文件默认编码格式,如下图

- 创建完配置文件,添加nickName值:
nickName=小张三
19.5 还可以通过Environment获取
// 通过 applicationContext 获取配置文件值
String nickName = applicationContext.getEnvironment().getProperty("nickName");
System.out.println(nickName);
19.6 测试用例

19、属性赋值-@PropertySource加载外部配置文件的更多相关文章
- springboot加载外部配置文件
网上搜集和整理如下(自己已验证过) 1. war包在tomcat中加载外部配置文件 war包运行在独立tomcat下时,如何加载war包外部配置application.properties,以达到每次 ...
- 脚本命令加载外部配置文件夹conf
加载log4j配置文件 Log4iConfigurer类 public class Log4iConfigurer { private static boolean INITIALIZED = fal ...
- Spring注解@PropertySource加载配置文件和SpringBoot注解@Value、@ConfigurationProperties进行属性映射
SpringBoot的配置文件 位置:resources目录下 配置文件的作用: (1).SpringBoot是基于约定的,所以很多配置都有默认值,但如果想使用自己的配置替换默认配置的话,就可以使用a ...
- 【Spark】SparkStreaming-加载外部配置文件
SparkStreaming-加载外部配置文件 spark加载配置文件_百度搜索 Spark加载外部配置文件 - CSDN博客 spark读取配置文件中的配置 - CSDN博客 spark加载prop ...
- SpringBoot系列——加载自定义配置文件
前言 SpringBoot启动时默认加载bootstrap.properties或bootstrap.yml(这两个优先级最高).application.properties或application. ...
- 【Java Web开发学习】Spring加载外部properties配置文件
[Java Web开发学习]Spring加载外部properties配置文件 转载:https://www.cnblogs.com/yangchongxing/p/9136505.html 1.声明属 ...
- springboot属性类自动加载配置文件中的值
springboot属性类自动加载配置文件中的值,如Person类加载在yml中配置的name,age等属性值,可以通过如下步骤获取: 类上添加@ConfigurationProperties注解,p ...
- Springboot2.0加载指定配置文件@PropertySource的使用
1. 在resouces下编写待加载的配置文件 这里使用person.properties # String person.last-name=john # int person.age=112 # ...
- 【Spring注解驱动开发】使用@PropertySource加载配置文件,我只看这一篇!!
写在前面 很多小伙伴都在问:冰河,你的Spring专题更新完了吗?怎么感觉像是写了一半啊?我:没有更新完呀,整个专题预计会有70多篇.那怎么更新了一半就去写别的了呢?那是因为有很多其他的小伙伴在后台留 ...
随机推荐
- [转帖]持久化journalctl日志清空命令查看配置参数详解
持久化journalctl日志清空命令查看配置参数详解 最近 linux上面部署服务 习惯使用systemd 进行处理 这样最大的好处能够 使用journalctl 进行查看日志信息. 今天清理了下 ...
- ARTS
ARTS的初衷 Algorithm.主要是为了编程训练和学习.每周至少做一个 leetcode 的算法题(先从Easy开始,然后再Medium,最后才Hard).进行编程训练,如果不训练你看再多的算法 ...
- MySQL使用中遇到的error
eclipse连接不上数据库 //加载驱动 //oracal.jdbc.drive.Oracle.Driver //com.mysql.jdbc.Driver try { Class.forName( ...
- Kafka集群安装及prometheus监控
前提 zookeeper安装参考:https://www.cnblogs.com/JustinLau/p/11372782.html 其他安装参考:https://www.cnblogs.com/lu ...
- MyBatis 源码篇-MyBatis-Spring 剖析
本章通过分析 mybatis-spring-x.x.x.jar Jar 包中的源码,了解 MyBatis 是如何与 Spring 进行集成的. Spring 配置文件 MyBatis 与 Spring ...
- VUE.js devtool 安装简易教程(转)
最简便的方法是用FQ来通过google应用商店进行安装,但是大多数人还是处在非FQ的状态 尝试过安装Node的方法来安装,但是极其不建议用这个方法安装,非常麻烦,而且必须要有node的基础,非常不接地 ...
- Mysql连接数设置
在使用MySQL数据库的时候,经常会遇到这么一个问题,就是“Can not connect to MySQL server. Too many connections”-mysql 1040错误,这是 ...
- default(Nullable(type)) vs default(type)
default(Nullable<long>) == null default(long?) == null default(long) == 0L
- 空a标签在IE下无效之解决方法
过程就不分析了,只说解决方法: 1.给a标签添加样式:background:url(about:blank); 2.给a标签随便添加背景色或者背景图片,然后将a标签的透明度设置为0,不过在IE中需要使 ...
- python 操作Hbase 详解
博文参考:https://www.cnblogs.com/tashanzhishi/p/10917956.html 如果你们学习过Python,可以用Python来对Hbase进行操作. happyb ...