1. @Value 例如:
@Value("${spring.profiles.active}")
private String profileActive;------相当于把properties文件中的spring.profiles.active注入到变量profileActive中
2. @ConfigurationProperties 例如:
@Component
@ConfigurationProperties(locations = "classpath:application.properties",prefix="test")
public class TestProperties {
String url;
String key;
}
其他类中使用时,就可以直接注入该TestProperties 进行访问相关的值
3. 使用Enviroment 例如:
private Enviroment env;
env.getProperty("test.url");
而env方式效率较低
注:@ConfigurationProperties也可用于其他.properties文件,只要locations指定即可
本文介绍了Spring框架中三种常见的配置属性注入方法:@Value、@ConfigurationProperties和通过Environment接口。这些方法可以方便地将配置文件中的值注入到Java类中,便于在应用程序中使用。
3056

被折叠的 条评论
为什么被折叠?



