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多篇.那怎么更新了一半就去写别的了呢?那是因为有很多其他的小伙伴在后台留 ...
随机推荐
- SQLServer 查询view中是否包含某个关键字
在数据库view的创建中,会遇到一些跨数据库的view脚本,但是在将view更新到production的时候可能忘记更改database name,导致出现一些问题. 以下脚本可以检查出包含某个关键字 ...
- double check
http://www.cnblogs.com/limingluzhu/p/5156659.html http://blog.csdn.net/chenchaofuck1/article/details ...
- 剑指Offer(4)——替换空格
题目: 请实现一个函数,把字符串中的每个空格替换成"%20".例如输入“We are happy.”,则输出“We%20are%20happy.”. 思路: 如果按照顺序从前往后依 ...
- sit、qas、dev、pet
SIT: System Integrate Test 系统整合测试 QAS: Quality Assurance system 质量保证 DEV: Development 开发 PET: Perfor ...
- (十四)Activitivi5之个人任务分配
一.个人任务分配 1.1 方式一:直接流程图配置中写死: 1.2 方式二:使用流程变量 我们在启动流程的时候设置流程变量即可 /** * 启动流程实例 */ @Test public void sta ...
- asp.net类似于js中的setTimeOut()的函数作用?
asp.net类似于js中的setTimeOut()的函数作用? 插入这行即可,定时2秒,再运行下一步: System.Threading.Thread.Sleep(); 加个随机数 Random r ...
- Windows 下 mysql 5.7 设置 区分大小写(敏感),设置默认编码 utf8mb4
修改编码 c盘下搜索 C:\ProgramData\MySQL\MySQL Server 5.7 在该my.ini文件下进行配置修改 [client] default-character-set = ...
- XCode下在不同位置声明变量的用法(转)
XCode下在不同位置声明变量的用法 方式一:直接在.h文件@interface中的大括号中声明. @interface Test : NSObject { NSString *str; // 私有变 ...
- iOS 9.0中UIAlertController的用法。
1.我为什么要写这篇博客记录它? 答:因为 UIAlertView和UIActionSheet 被划线了 苹果不推荐我们使用这两个类了,也不再进行维护和更新,为了以后方便使用我来记录一下.如图所示 正 ...
- 开发六年mybatisplus使用小结
最近在项目里用到了一个第三方库,叫mybatisplus,是一个mybatis的增强库,简单来说就是增强了mybatis的功能,让mybatis更好用,mybatisplus给的官方定义是Mybati ...