Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I tell Spring to use a Java map to resolve property placeholder?

Tags:

java

spring

I have a Map<String, String> and I would like to tell Spring to use this when creating beans and resolving property placeholders. What is the easiest way to do this? Here is an example:

@Component
public class MyClass {
   private String myValue;

    @Autowired
    public MyClass(@Value("${key.in.map}") String myValue) {
        this.myValue = myValue;
    }

    public String getMyValue() {
        return myValue;
    }
}

public static void main(String[] args) {
    Map<String, String> propertyMap = new HashMap<>();
    propertyMap.put("key.in.map", "value.in.map");
    ApplicationContext ctx = ...;
    // Do something???
    ctx.getBean(MyClass.class).getMyValue(); // Should return "value.in.map"
}
like image 988
Max Avatar asked Jun 07 '15 14:06

Max


People also ask

What is Property placeholder in spring?

The context:property-placeholder tag is used to externalize properties in a separate file. It automatically configures PropertyPlaceholderConfigurer , which replaces the ${} placeholders, which are resolved against a specified properties file (as a Spring resource location).

What is a Java placeholder?

A Placeholder is a predefined location in a JSP that displays a single piece of web content at a time that is dynamically retrieved from the BEA Virtual Content Repository.

What is placeholder spring boot?

properties in spring boot. Using placeholders, Spring Boot allows you to reuse values in the application properties file. Spring boot placeholder can be used to configure a key value that is the same as another key value or to create a value using another key value.


1 Answers

Spring provides a MapPropertySource which you can register with your ApplicationContext's Environment (you'll need a ConfigurableEnvironment which most ApplicationContext implementations provide).

These registered PropertySource values are used by the resolvers (in order) to find a value for your placeholder names.

Here's a complete example:

@Configuration
@ComponentScan
public class Example {

    @Bean
    public static PropertySourcesPlaceholderConfigurer configurer() {
        PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
        // can also add it here
        //configurer.setPropertySources(propertySources);
        return configurer;
    }

    public static void main(String[] args) {
        Map<String, Object> propertyMap = new HashMap<>();
        propertyMap.put("key.in.map", "value.in.map");
        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
        MapPropertySource propertySource = new MapPropertySource("map-source", propertyMap);
        ctx.getEnvironment().getPropertySources().addLast(propertySource);
        ctx.register(Example.class);
        ctx.refresh();

        MyClass instance = ctx.getBean(MyClass.class);
        System.out.println(instance.getMyValue());
    }
}

@Component
class MyClass {
    private String myValue;
    @Autowired
    public MyClass(@Value("${key.in.map}") String myValue) {
        this.myValue = myValue;
    }
    public String getMyValue() {
        return myValue;
    }
}
like image 76
Sotirios Delimanolis Avatar answered Oct 02 '22 03:10

Sotirios Delimanolis