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加载外部配置文件的更多相关文章

  1. springboot加载外部配置文件

    网上搜集和整理如下(自己已验证过) 1. war包在tomcat中加载外部配置文件 war包运行在独立tomcat下时,如何加载war包外部配置application.properties,以达到每次 ...

  2. 脚本命令加载外部配置文件夹conf

    加载log4j配置文件 Log4iConfigurer类 public class Log4iConfigurer { private static boolean INITIALIZED = fal ...

  3. Spring注解@PropertySource加载配置文件和SpringBoot注解@Value、@ConfigurationProperties进行属性映射

    SpringBoot的配置文件 位置:resources目录下 配置文件的作用: (1).SpringBoot是基于约定的,所以很多配置都有默认值,但如果想使用自己的配置替换默认配置的话,就可以使用a ...

  4. 【Spark】SparkStreaming-加载外部配置文件

    SparkStreaming-加载外部配置文件 spark加载配置文件_百度搜索 Spark加载外部配置文件 - CSDN博客 spark读取配置文件中的配置 - CSDN博客 spark加载prop ...

  5. SpringBoot系列——加载自定义配置文件

    前言 SpringBoot启动时默认加载bootstrap.properties或bootstrap.yml(这两个优先级最高).application.properties或application. ...

  6. 【Java Web开发学习】Spring加载外部properties配置文件

    [Java Web开发学习]Spring加载外部properties配置文件 转载:https://www.cnblogs.com/yangchongxing/p/9136505.html 1.声明属 ...

  7. springboot属性类自动加载配置文件中的值

    springboot属性类自动加载配置文件中的值,如Person类加载在yml中配置的name,age等属性值,可以通过如下步骤获取: 类上添加@ConfigurationProperties注解,p ...

  8. Springboot2.0加载指定配置文件@PropertySource的使用

    1. 在resouces下编写待加载的配置文件 这里使用person.properties # String person.last-name=john # int person.age=112 # ...

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

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

随机推荐

  1. 认识 Spring 框架(一)

    认识 Spring 框架 Spring 框架是 Java 应用最广的框架,它的成功来源于理念,而不是技术本身,它的理念包括 IoC (Inversion of Control,控制反转) 和 AOP( ...

  2. arcgis for android100.x 禁止地图旋转

    by 蔡建良2019-5-16 关键类: com.esri.arcgisruntime.mapping.view.DefaultMapViewOnTouchListener DefaultMapVie ...

  3. (十)Activitivi5之启动流程/完成任务的时候设置流程变量

    一.启动流程的时候设置流程变量 1.1 案例 /** * 启动流程实例 */ @Test public void start() { Student student=new Student(); st ...

  4. 谷歌(google)广告尺寸大小列表

    在平时做网页模板时,我们需要计算Google AdSense 的尺寸,以确保它能和页面完美的结合,提高AdSense的点击率,进台后看又很麻烦,下面整理了Google 广告的各种尺寸,跟大家分享一下. ...

  5. Senparc.Weixin+nginx配置之坑 ‘10003 redirect_uri域名与后台不一致’

    微信公众号扫一扫功能提示:10003 redirect_uri域名与后台不一致 Senparc.Weixin组件很好用,但一个坑,不知道这和个是否有关.. 先说明下环境,centos+.net cor ...

  6. 微软发布云端基因服务:推动AI驱动的精准医疗

    微软发布云端基因服务:推动AI驱动的精准医疗 2018年03月07日 00:00:00 微软研究院AI头条 阅读数:117    版权声明:本文为博主原创文章,未经博主允许不得转载. https:// ...

  7. 一、eureka服务端自动配置

    所有文章 https://www.cnblogs.com/lay2017/p/11908715.html 正文 @EnableEurekaServer开关 eureka是一个c/s架构的服务治理框架, ...

  8. python3爬虫之requests库基本使用

    官方文档链接(中文) https://2.python-requests.org/zh_CN/latest/ requests  基于  urllib3 ,python编写. 安装 pip insta ...

  9. 使用RevitNet操作多个版本的Revit

    在Revit二次开发中,如果只是简单的从模型中提取数据或不需要界面对Revit进行修改,我们一般使用RevitNet. 如果对RevitNet不熟悉的,请参考:RevitAPI进阶之独立进程内读取.写 ...

  10. rsync & inotify-tools 实时同步

    1.根据之前一篇关于rsync的随笔部署好rsync服务后,可以开始inotify的部署 2.inotify的部署使用 ①.检查系统是否支持inotify [root@iZ25w1kdi5zZ ~]# ...