0

I have to read a few string array variables from properties file of my spring-boot project.I have already created a class with getter and setter methods for the arrays.I don't know how to get the values of those string array variables from the property.yml file using java 1.8

1 Answers1

1

Using @Value

In your .yaml:

myPropertiesList: item1, item2

or

myPropertiesList: >
  item1, 
  item2

In your Java class:

@Value("${myPropertiesList}")    
String[] myPropertiesArray;

or in SpringBoot2:

@Value("${myPropertiesList}")
List<String> myPropertiesList;

Using @ConfigurationProperties

In your .yaml:

myPrefix.myPropertiesList: item1, item2

Configuration class:

@Configuration
@ConfigurationProperties(prefix = "myPrefix")
public class ConfigProperties {
    private List<String> myPropertiesList;
}

and add the following to your SpringBoot configuration:

@EnableConfigurationProperties(ConfigProperties.class)
Babyburger
  • 1,730
  • 3
  • 19
  • 32