您的位置:首页 > 编程语言 > Java开发

SpringBoot 快速简单读取properties文件

2017-10-09 11:03 316 查看
1、添加maven依赖注解

<!-- 做动态properties的getter/setter赋值方法: -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>


2、添加读取properties文件PropertiesConfig类

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.io.ClassPathResource;

/**
* 读取properties文件
* Created by CatalpaFlat on 2017/8/11.
*/
@Configuration
public class PropertiesConfig {

@Bean
public PropertySourcesPlaceholderConfigurer createPropertySourcesPlaceholderConfigurer() {
ClassPathResource resource = new ClassPathResource("properties/base.properties");
PropertySourcesPlaceholderConfigurer propertyPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
propertyPlaceholderConfigurer.setLocation(resource);
return propertyPlaceholderConfigurer;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: