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
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
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

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • How To Build Web Service Using Spring Boot 2.x
  • Visually Designing Views for Java Web Apps
  • Spring Boot – How to Change Default Context Path Using the application. properties?
  • How to Activate New User Accounts by Email

Trending

  • Modular Software Architecture: Advantages and Disadvantages of Using Monolith, Microservices and Modular Monolith
  • Bad Software Examples: How Much Can Poor Code Hurt You?
  • Performance Optimization Strategies in Highly Scalable Systems
  • Implementing Stronger RBAC and Multitenancy in Kubernetes Using Istio
  1. DZone
  2. Coding
  3. Frameworks
  4. A Custom Property in Spring

A Custom Property in Spring

Rob Gordon user avatar by
Rob Gordon
·
Apr. 22, 12 · Interview
Like (0)
Save
Tweet
Share
12.27K Views

Join the DZone community and get the full member experience.

Join For Free

<context:property-placeholder> is a really easy way to provide property replacements in Spring configurations with values from a standard Java Properties file. But what if you don’t want a property hard coded into a file – a clear text password for instance? Spring provides all the bits and pieces to write your own property replacement. Let me introduce my CustomPropertyConfigurer.

I’ll demonstrate using a variation on the theme of the Spring JDBC Template. MyQuery is a simple extension of org.springframework.jdbc.core.JDBCTemplate that gets the current timestamp from a MySQL database. Here’s the, hopefully familiar, configuration:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
 
  <context:property-placeholder location="classpath:jdbc.properties" />
 
  <bean id="myQuery" class="rob.MyQuery">
    <property name="dataSource" ref="dataSource" />
  </bean>
 
  <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">
    <property name="driverClassName" value="${jdbc.driverClassName}" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
  </bean>
 
</beans>

Except the jdbc.properties file does not contain the password:

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://rob-7
jdbc.username=rob

I will set the jdbc.password property myself from what is entered on the command line.

public static void main(String... args) {
 
    char[] password = System.console().readPassword("Password: ");
 
    Properties properties = new Properties();
    properties.setProperty("jdbc.password", new String(password));
 
    ConfigurableApplicationContext context =
        new ClassPathXmlApplicationContext(
            new String[] {
                "rob/MyQuery.xml"},
            false);
 
    context.addBeanFactoryPostProcessor(
            new CustomPropertyConfigurer(properties));
    context.refresh();
 
    MyQuery myQuery = context.getBean(MyQuery.class);
 
    myQuery.run();
 
    context.close();
}

Where the CustomPropertyConfigurer is:

import java.util.Properties;
 
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanDefinitionStoreException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanDefinitionVisitor;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.util.PropertyPlaceholderHelper;
import org.springframework.util.StringValueResolver;
 
public class CustomPropertyConfigurer implements BeanFactoryPostProcessor {
 
    private final Properties properties;
 
    public CustomPropertyConfigurer(Properties properties) {
        this.properties = properties;
    }
 
    public void postProcessBeanFactory(
            ConfigurableListableBeanFactory beanFactoryToProcess)
    throws BeansException {
 
        BeanDefinitionVisitor visitor = new BeanDefinitionVisitor(
                new BeanDirectoryResolver());
 
        String[] beanNames =
                beanFactoryToProcess.getBeanDefinitionNames();
        for (int i = 0; i < beanNames.length; i++) {
 
            BeanDefinition bd = beanFactoryToProcess.getBeanDefinition(
                    beanNames[i]);
            try {
                visitor.visitBeanDefinition(bd);
            }
            catch (BeanDefinitionStoreException ex) {
                throw new BeanDefinitionStoreException(
                        bd.getResourceDescription(), beanNames[i],
                        ex.getMessage());
            }
        }
    }
 
    class BeanDirectoryResolver implements StringValueResolver {
 
        private final PropertyPlaceholderHelper helper;
 
        public BeanDirectoryResolver() {
            helper = new PropertyPlaceholderHelper("${", "}");
        }
 
        public String resolveStringValue(String strVal) {
            return helper.replacePlaceholders(strVal, properties);
        }
    }
}

The CustomPropertyConfigurer gets applied first. It leaves any properties it can’t resolve (all but the password) for the standard <context:property-configurer> to resolve. Unit tests running against a different jdbc.propeties file can continue to provide the password as before.

Here it is running:

Cusom Propery Configurer Running

There are many other examples of configuration values that might only be discovered at runtime – file names, schedule dates, form values etc. So long as the value can be a String, a CustomPropertyConfigurer provides a simple way of passing these values to Spring.

 

Property (programming) Spring Framework

Published at DZone with permission of Rob Gordon, 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
  • Spring Boot – How to Change Default Context Path Using the application. properties?
  • How to Activate New User Accounts by Email

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: