spring-boot 如何加载rsources下面的自定义配置文件
来源:互联网 发布:mysql设置主键 guid 编辑:程序博客网 时间:2024/06/05 14:41
1.创建一个自定义properties文件,如下:
config.msg = hello world!
config.name = "潘浩"
2.写一个读取配置文件的类
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component
@PropertySource("classpath:config.properties")
/**
* 在@ConfigurationProperties注释中有两个属性:
prefix:指定配置文件中键名称的前缀(我这里配置文件中所有键名都是以web.开头)
* @author 潘浩
*/
public class MyConfig {
@Value("${config.msg}")
private String msg;
@Value("${config.name}")
private String name;
public String getMsg() {
return msg;
}
public String getName() {
return name;
}
}
3.写一个测试类可以测试是否成功读取配置文件中的属性.
/**
* 测试类
*
* @author 潘浩
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes=GlsserviceProviderProjectMgrApplication.class) // 指定spring-boot的启动类
public class ControllerTest {
@Autowired
private MyConfig myConfig;
@Test
public void test_如何获取配置文件中的属性值()
{
System.out.println("test_如何获取配置文件中的属性值: "+myConfig.getMsg());
}
}
- spring-boot 如何加载rsources下面的自定义配置文件
- Spring Boot加载配置文件
- spring自定义配置文件的加载
- 如何在Spring容器中加载自定义的配置文件
- 如何在Spring容器中加载自定义的配置文件
- spring boot 自定义配置文件的类型转换
- 自定义加载Spring配置文件
- [Spring boot]配置文件properties加载
- Spring Boot 配置文件加载顺序
- spring boot加载第三方jar包的配置文件
- Spring Boot 系列(2) 配置文件的加载
- spring boot 自定义加载tomcat
- spring boot:使用@ConfigurationProperties注解加载配置文件
- spring boot 中手动加载配置文件
- (二)Spring-boot 配置文件加载顺序
- spring boot 自定义logback日志配置文件
- Spring boot之读取自定义配置文件
- spring配置文件的加载
- javaSSM框架详解
- Timer 控件中的Elapsed事件与tick事件的区别
- Cocos2d-x里面如何实现MVC(一)
- A*寻路算法
- Codeforces Round #430
- spring-boot 如何加载rsources下面的自定义配置文件
- Linux下的Spark安装及使用
- 【学习摘记】马士兵JDBC技术_课时11_JDBC处理存储过程
- elasticsearch配置
- 英语角
- python里使用Future对象进行回调别的函数
- 动态生成ul li
- 【TL8266】向蓝牙模块发送AT指令的APP
- LeetCode 383. Ransom Note 自己的解法