WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Spring Framework · Expert · question 62 of 100

How can you implement custom property sources and property source placeholders in Spring?

📕 Buy this interview preparation book: 100 Spring Framework questions & answers — PDF + EPUB for $5

Custom property sources and property source placeholders are powerful features in the Spring Framework that allow us to externalize configuration details of our application. They make the application more flexible and easier to maintain, as configuration values can be changed without making any changes in the underlying code.

Here’s how you can implement custom property sources and property source placeholders in Spring:

**Custom Property Sources**

Spring provides an interface called ‘PropertySource‘ that allows us to define custom property sources. Essentially, a ‘PropertySource‘ is a key/value pair that can be used to provide configuration properties to different parts of our application. We can create our custom ‘PropertySource‘ implementation by implementing this interface and overriding the ‘getProperty(String name)‘ method to return a property value for a given name.

public class MyPropertySource implements PropertySource<String> {

    private final Properties properties;

    public MyPropertySource() {
        properties = new Properties();
        // populate properties
        properties.put("my.property.key", "my.property.value");
        // add more properties
    }

    @Override
    public String getName() {
        return "myPropertySource";
    }

    @Override
    public Object getProperty(String name) {
        return properties.getProperty(name);
    }
}

In the above example, we have defined a custom ‘PropertySource‘ called ‘MyPropertySource‘ that has a predefined key/value pair. We can register this property source with the Spring ‘Environment‘ using the ‘StandardEnvironment‘ class.

@Configuration
public class AppConfig {

    @Bean
    public MyPropertySource myPropertySource() {
        return new MyPropertySource();
    }

    @Bean
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        final PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
        configurer.setPropertySources(new MutablePropertySources()
                .addFirst(new StandardEnvironment().getPropertySources())
                .addFirst(myPropertySource()));
        return configurer;
    }
}

In the above configuration, we have defined a ‘PropertySourcesPlaceholderConfigurer‘ bean that is responsible for replacing placeholders in property values with their actual values. We have also added our custom ‘PropertySource‘ to the ‘PropertySources‘ object. Note that we have added our custom ‘PropertySource‘ before ‘StandardEnvironment‘ to give it higher priority.

**Property Source Placeholders**

Spring’s ‘PropertySourcesPlaceholderConfigurer‘ enables property placeholders in bean definitions, which means that we can use properties in our beans by defining a property placeholder. This placeholder is replaced with the actual property value at runtime.

@Value("${property.key}")
private String propertyValue;

In the above example, ‘$property.key‘ is the property placeholder. The actual property value for this placeholder can be defined in different property sources, such as environment variables, system properties, or custom property sources.

We can also use expressions in a property value by enclosing them in ‘#‘.

@Value("#{myBean.someMethod()}")
private String propertyValue;

In the above example, ‘#myBean.someMethod()‘ is an expression that calls a method on the ‘myBean‘ bean and returns its result.

In conclusion, custom property sources and property source placeholders provide an efficient way to externalize configuration details in a Spring application. We can define our custom property sources by implementing the ‘PropertySources‘ interface and register them with the Spring ‘Environment‘. Additionally, Spring’s ‘PropertySourcesPlaceholderConfigurer‘ enables us to use property placeholders in bean definitions and replace them with their actual values at runtime.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Spring Framework interview — then scores it.
📞 Practice Spring Framework — free 15 min
📕 Buy this interview preparation book: 100 Spring Framework questions & answers — PDF + EPUB for $5

All 100 Spring Framework questions · All topics