DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • How To Build Web Service Using Spring Boot 2.x
  • Visually Designing Views for Java Web Apps
  • How to Activate New User Accounts by Email
  • How To Read the Properties File Outside the Jar in Spring Boot

Trending

  • How to Migrate From C/C++ to Rust: A Practical Guide for Embedded IoT Systems
  • Columnar Formats for AI Telemetry: Parquet vs Vortex With DuckDB
  • When Production Stops Moving: Running Claude Code Across a Distributed Enterprise Integration Team
  • Treating Web Scrapers Like Production Services

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Frameworks
  4. Spring Core: Reading properties with PropertyPlaceHolderConfigurer

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.

By 
Gaurav Rai Mazra
·
Updated May. 16, 17 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
69.8K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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

@Configuration
public class AppConfig {

    @Bean
    public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
        propertySourcesPlaceholderConfigurer.setLocations(new ClassPathResource("application-db.properties"));
        //propertySourcesPlaceholderConfigurer.setIgnoreUnresolvablePlaceholders(true);
        //propertySourcesPlaceholderConfigurer.setIgnoreResourceNotFound(true);
        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

@Configuration
public class DBProperties {

  @Value("${db.username}")
  private String userName;

  @Value("${db.password}")
  private String password;

  @Value("${db.url}")
  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.

Property (programming) Spring Framework

Published at DZone with permission of Gaurav Rai Mazra. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • How To Build Web Service Using Spring Boot 2.x
  • Visually Designing Views for Java Web Apps
  • How to Activate New User Accounts by Email
  • How To Read the Properties File Outside the Jar in Spring Boot

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook