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
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

How does AI transform chaos engineering from an experiment into a critical capability? Learn how to effectively operationalize the chaos.

Data quality isn't just a technical issue: It impacts an organization's compliance, operational efficiency, and customer satisfaction.

Are you a front-end or full-stack developer frustrated by front-end distractions? Learn to move forward with tooling and clear boundaries.

Developer Experience: Demand to support engineering teams has risen, and there is a shift from traditional DevOps to workflow improvements.

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 Install and Set Up Jenkins With Docker Compose
  • Code of Shadows: Master Shifu and Po Use Functional Java to Solve the Decorator Pattern Mystery
  • Mastering Kubernetes Observability: Boost Performance, Security, and Stability With Tracestore, OPA, Flagger, and Custom Metrics
  • Create POM With LLM (GitHub Copilot) and Playwright MCP
  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 user avatar
Gaurav Rai Mazra
·
Updated May. 16, 17 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
68.5K Views

Join the DZone community and get the full member experience.

Join For Free

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, DZone MVB. 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

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • [email protected]

Let's be friends: