Spring. Load complete properties file in class field as map

spring property-placeholder
spring annotation value from properties file
propertysourcesplaceholderconfigurer
spring boot dynamic properties
spring properties file
spring properties map
spring configurationproperties
load properties file in spring application context

it should be easy. But I have not some idea. A file 'props.properties' on disk. Where are any properties in the file. I have a class of configuration:

@Configuration
@PropertySource(value = "file:props.properties", encoding = "utf8")
public class AppConfig {
...
  @Some_spring_annotation  <-- that is qestion
  private Map map;
...
}

How to load all properties from 'props.properties' to 'map' by Spring?

You can load whole porperties file as Map by defining it as PropertiesFactoryBean and then using it with @Resource annotation.

@Configuration
@PropertySource(value = "file:src/main/resources/test.properties", encoding = "utf8")
public class AppConfig {

@Value("${propertyname}")
String prop;

@Resource(name = "propertyBean")
private Map<String, String> propMap;

@Bean(name = "propertyBean")
public static PropertiesFactoryBean mapper() {
        PropertiesFactoryBean bean = new PropertiesFactoryBean();
        bean.setLocation(new FileSystemResource("src/main/resources/test.properties"));
        return bean;
}

public Map<String, String> getPropMap() {
    return propMap;
}
}

And access any key present in the properties file using key like below:-

@RestController
public class Test {

@Autowired
private AppConfig appConfig;

@RequestMapping(value = "/test", method = RequestMethod.GET)
public String  login(HttpServletRequest request){
    return appConfig.getPropMap().get("application.name");
}

}



test.propeties:-
server.port1=8099
application.name=edge-service
propertyname=dddd

In this you don't need to write @Value annotation everytime, you can access values using propMap. If you want to read single key use

@Value("${propertyname}")
String prop;

Even you can define map data into property file

propertyname={key1:'value1',key2:'value2'}

There can be many implementation to achieve it based on requirement.

The @Value Annotation in Spring, Value is a Java annotation used at the field or method/constructor level. Injecting values from properties files with the help of @Value annotation is Because @Value is processed by the BeanPostProcessor class, it will be Now, to inject this, we'll need a Map<String, List<String>> : We were unable to load Disqus. By default, Spring Boot reads configuration properties from application.properties or application.yml at application startup. However, we can use @PropertySource to load a custom YAML file. Now that we are familiar with what a YAML file is, let's see how to inject YAML properties as a Map in Spring Boot. 3.

Properties file entry

valMap ={key1: '1', key2: '2', key3: '3'}

Spring annotation

@Value("#{${valMap}}")
private Map<String, Integer> valMap;

More on @Value annotation here : https://www.baeldung.com/spring-value-annotation

24. Externalized Configuration - Project Metadata API Guide, @PropertySource annotations on your @Configuration classes. Please SpringApplication loads properties from application.properties files in the following loads YAML as Properties and the YamlMapFactoryBean loads YAML as a Map . If nested POJO properties are initialized (like the Security field in the preceding  I want to populate a HashMap using the Properties class. I want to load the entries in the .propeties file and then copy it into the HashMap. Earlier, I used to just initialize the HashMap with the

You can have values from properties file read in a single map

Your application.properties file should look like

app.properties.map.key1 = value1
app.properties.map.key2 = value2
app.properties.map.key3 = value3
app.properties.map.key4 = value4
app.properties.map.key5 = value5

And, your @ConfigurationProperties annotated class should look like

@ConfigurationProperties(prefix = "app.properties")
public class AppProperties {

    private Map<String, String> map;

    // No Arguments Constructor

    // Getters & Setters
}

Also, starting from Spring Boot 2.2.0, your @ConfigurationProperties classes can be immutable

@ConfigurationProperties(prefix = "app.properties")
public class AppProperties {

    private final Map<String, String> map;

    @ConstructorBinding
    public AppProperties(Map<String, String> map) {

      this.map = map;
    }
}

Properties with Spring and Spring Boot, Tutorial for how to work with properties files and property values in Spring. public class PropertiesWithJavaConfig {. // } This env file will be loaded and will take precedence over the default property file. approach again, automatically mapping between property names and their corresponding fields. CREDIT: Programmatic access to properties in Spring without re-reading the properties file. I've found a nice implementation of accessing the properties programmatically in spring without reloading the same properties that spring has already loaded. [Also, It is not required to hardcode the property file location in the source]

Load Spring Boot Properties From a JSON File, Learn how to load configuration properties for Spring Boot from a JSON file. Now we have to connect our JSON file to the configuration class: Because the fields should be populated by the framework, we need to use So we can add a Map member to our JsonProperties class with getters and setters:. Spring and Spring Boot have strong support for loading external configurations – you can find a great overview of the basics in this article.. Since this support mainly focuses on .properties and .yml files – working with JSON typically needs extra configuration.

How to map Spring Boot properties to a POJO using , Properties files are useful to externalize Spring Boot configuration. In this post, we will see how to map all properties defined in the application.properties file to a POJO. Below configuration class uses @ConfigurationProperties annotation to bind Now that all properties is loaded from a application.properties file, we can   Spring Boot @ConfigurationProperties is letting developer maps the entire .properties and yml file into an object easily.. P.S Tested with Spring Boot 2.1.2.RELEASE. 1. @Value. 1.1 Normally, we use the @Value to inject the .properties value one by one, this is good for small and simple structure .properties files.

Spring Boot @ConfigurationProperties: Binding external, Following is the directory structure of the complete application for your Spring Boot application loads configuration properties from Let's now see how we can bind the properties defined in the application.properties file to a POJO class using Map; @ConfigurationProperties(prefix = "app") public class  Spring will automatically bind any property defined in our property file that has the prefix mail and the same name as one of the fields in the ConfigProperties class. Spring uses some relaxed rules for binding properties. So the following variations are all bound to the property hostName:

Comments
  • stackoverflow.com/questions/30691949/… Use annotation @Value
  • Possible duplicate of Spring Boot - inject map from properties file