Over a million developers have joined DZone.

Spring Core: Reading properties with PropertyPlaceHolderConfigurer

DZone's Guide to

Spring Core: Reading properties with PropertyPlaceHolderConfigurer

PropertyPlaceHolderConfigurer is a handy way to externalize the properties you want to use in a property file so that they're still resolved when starting the app.

· Java Zone ·
Free Resource

Build vs Buy a Data Quality Solution: Which is Best for You? Gain insights on a hybrid approach. Download white paper now!

In this post, we will externalize the properties used in an application in a property file and will use PropertyPlaceHolderConfigurer to resolve the placeholder at startup time.

Java Configuration for PropertyPlaceHolderConfigurer

public class AppConfig {

    public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
        propertySourcesPlaceholderConfigurer.setLocations(new ClassPathResource("application-db.properties"));
        return propertySourcesPlaceholderConfigurer;

We created an object of PropertySourcesPlaceholderConfigurer and set the locations to search. In this example, we used ClassPathResource to resolve the properties file from the classpath. You can use a file-based resource, which needs the absolute path of the file.

DBProperties File

public class DBProperties {

  private String userName;

  private String password;

  private String url;

  //getters for instance fields

We used the @Value annotation to resolve the placeholders.

Testing the Configuration

public class Main {
    private static final Logger logger = Logger.getLogger(Main.class.getName());

    public static void main(String[] args) {
        try (ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class, DBProperties.class);) {
          DBProperties dbProperties = context.getBean(DBProperties.class);
          logger.info("This is dbProperties: " + dbProperties.toString());

For testing, we created an object of AnnotationConfigApplicationContext and got theDBProperties bean from it and logged it using Logger. This is a simple way to externalize the configuration properties from framework configuration. You can also get the full example code from GitHub.

Build vs Buy a Data Quality Solution: Which is Best for You? Maintaining high quality data is essential for operational efficiency, meaningful analytics and good long-term customer relationships. But, when dealing with multiple sources of data, data quality becomes complex, so you need to know when you should build a custom data quality tools effort over canned solutions. Download our whitepaper for more insights into a hybrid approach.

java ,spring core ,properties ,configuration ,tutorial

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}