Java Spring Boot读取属性(不带映射前缀)
我需要读取地图中application.properties文件中的所有属性 在下面的代码中,属性测试具有相应的值,但映射为空。如何使用application.properties文件中的值填充“map”,而不向属性添加前缀 这是我的application.properties文件Java Spring Boot读取属性(不带映射前缀),java,spring,spring-boot,configurationproperties,Java,Spring,Spring Boot,Configurationproperties,我需要读取地图中application.properties文件中的所有属性 在下面的代码中,属性测试具有相应的值,但映射为空。如何使用application.properties文件中的值填充“map”,而不向属性添加前缀 这是我的application.properties文件 AAPL=25 GDDY=65 test=22 我正在使用@ConfigurationProperties,如下所示 @Configuration @ConfigurationProperties("") @Pro
AAPL=25
GDDY=65
test=22
我正在使用@ConfigurationProperties,如下所示
@Configuration
@ConfigurationProperties("")
@PropertySource("classpath:application.properties")
public class InitialConfiguration {
private HashMap<String, BigInteger> map = new HashMap<>();
private String test;
public HashMap<String, BigInteger> getMap() {
return map;
}
public void setMap(HashMap<String, BigInteger> map) {
this.map = map;
}
public String getTest() {
return test;
}
public void setTest(String test) {
this.test = test;
}
}
@Value("${AAPL}")
private String aapl;
@配置
@配置属性(“”)
@PropertySource(“类路径:application.properties”)
公共类初始配置{
private HashMap map=new HashMap();
私有字符串测试;
公共HashMap getMap(){
返回图;
}
公共void setMap(HashMap映射){
this.map=map;
}
公共字符串getTest(){
回归试验;
}
公共无效设置测试(字符串测试){
这个。测试=测试;
}
}
在spring boot中,如果需要从application.properties获取单个值,只需使用带有给定名称的@value注释即可
因此,要获得AAPL值,只需添加一个类级属性,如下所示
@Configuration
@ConfigurationProperties("")
@PropertySource("classpath:application.properties")
public class InitialConfiguration {
private HashMap<String, BigInteger> map = new HashMap<>();
private String test;
public HashMap<String, BigInteger> getMap() {
return map;
}
public void setMap(HashMap<String, BigInteger> map) {
this.map = map;
}
public String getTest() {
return test;
}
public void setTest(String test) {
this.test = test;
}
}
@Value("${AAPL}")
private String aapl;
如果需要将完整属性文件作为映射加载,我将使用ResourceLoader将完整文件作为流加载,然后按如下方式解析它
@Autowired
public loadResources(ResourceLoader resourceLoader) throws Exception {
Resource resource = resourceLoader.getResource("classpath:myProperties.properties"));
BufferedReader br = new BufferedReader(new InputStreamReader(resource.getInputStream()));
String line;
int pos = 0;
Map<String, String> map = new HashMap<>();
while ((line = br.readLine()) != null) {
pos = line.indexOf("=");
map.put(line.substring(0, pos), line.substring( pos + 1));
}
}
@Autowired
公共loadResources(ResourceLoader ResourceLoader)引发异常{
Resource=resourceLoader.getResource(“classpath:myProperties.properties”);
BufferedReader br=新的BufferedReader(新的InputStreamReader(resource.getInputStream());
弦线;
int pos=0;
Map Map=newhashmap();
而((line=br.readLine())!=null){
pos=行索引(“=”);
映射放置(行子字符串(0,位置),行子字符串(位置+1));
}
}
您必须使用@Value从application.properties文件中获取值据我所知,您不能使用
@ConfigurationProperties
来实现这一点,它们需要前缀才能在bean中加载这些属性
AAPL=25
GDDY=65
test=22
但是,如果您的目标是以编程方式获取“属性X”的“值Y”,则始终可以插入并使用getProperty()
方法查找某些属性,例如:
@配置
公共类初始配置{
@自动连线
私人环境;
@施工后
公开无效测试(){
整数aapl=environment.getProperty(“aapl”,Integer.class);//25
整数gddy=environment.getProperty(“gddy”,Integer.class);//65
Integer test=environment.getProperty(“test”,Integer.class);//22
}
}
这可以使用属性LoaderUtils和@PostConstruct
请检查以下样本:
@Configuration
public class HelloConfiguration {
private Map<String, String> valueMap = new HashMap<>();
@PostConstruct
public void doInit() throws IOException {
Properties properties = PropertiesLoaderUtils.loadAllProperties("application.properties");
properties.keySet().forEach(key -> {
valueMap.put((String) key, properties.getProperty((String) key));
});
System.err.println("valueMap -> "+valueMap);
}
public Map<String, String> getValueMap() {
return valueMap;
}
public void setValueMap(Map<String, String> valueMap) {
this.valueMap = valueMap;
}
}
@配置
公共类Hello配置{
私有映射valueMap=newhashmap();
@施工后
public void doInit()引发IOException{
Properties Properties=PropertiesLoaderUtils.loadAllProperties(“application.Properties”);
properties.keySet().forEach(键->{
valueMap.put((字符串)键,properties.getProperty((字符串)键));
});
System.err.println(“valueMap->”+valueMap);
}
公共地图getValueMap(){
返回值映射;
}
public void setValueMap(Map valueMap){
this.valueMap=valueMap;
}
}
实际上,您可以使用@ConfigurationProperties
而不加前缀,以获得Spring应用程序已知的全部属性,即应用程序、系统和环境属性等
下面的示例创建一个完全填充的映射作为Springbean。然后将这个bean连接/注入到您需要的任何地方
@Configuration
class YetAnotherConfiguration {
@ConfigurationProperties /* or @ConfigurationProperties("") */
@Bean
Map<String, String> allProperties() {
return new LinkedHashMap<>();
}
}
@Autowire
void test(Map<String, String> allProperties) {
System.out.println(allProperties.get("AAPL")); // 25
...
}
@配置
类YetAnotherConfiguration{
@ConfigurationProperties/*或@ConfigurationProperties(“”)*/
@豆子
映射所有属性(){
返回新的LinkedHashMap();
}
}
@自动连线
无效测试(映射所有属性){
System.out.println(allProperties.get(“AAPL”);//25
...
}
谢谢,我试过了,但我不想直接使用环境,但这是最简单的解决方案。这应该是选定的答案。通过环境加载是最健壮的方法。