.yml文件写入如下配置信息
test: path1: xxxx path2: yyyy
方法一、通过@ConfigurationProperties注解
TestConfig类:
@Component
@ConfigurationProperties(prefix="test")
@Getter
@Setter
public class TestConfig {
private String path1;
private String path2;
}
测试:
@SpringBootTest
class DemoApplicationTests {
@Autowired
private TestConfig testConfig;
@Test
void contextLoads() {
System.out.println("path1: " + testConfig.getPath1() +" path2:" + testConfig.getPath2() + "");
}
}
运行结果
path1:xxxx path2:yyyy
方法二、通过@value注解
测试:
@SpringBootTest
class DemoApplicationTests {
@Value("${test.path1}")
private String path1;
@Value(