How do I read a value from properties file using Spring EL?

In the previous two examples you have seen how to access member of a collection and access a map element using the square-braces [] operator in Spring EL. In this example you will see how to use the [] operator to read a value from a properties file or java.util.Properties.

Let’s say we have a database properties file called with the following entries in it:


First, let’s create the spring configuration file. In this configuration we will use the <util:properties> to load the properties file into Spring. And then we will use Spring EL to access the value of these properties and assign it to some bean’s properties.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="" xmlns:xsi=""

    <util:properties id="database" location="" />

    <bean id="dataSource" class="org.kodejava.spring.core.el.MyDataSource">
        <property name="driverClassName" value="#{database['jdbc.driverClassName']}" />
        <property name="url" value="#{database['jdbc.url']}" />
        <property name="username" value="#{database['jdbc.username']}" />
        <property name="password" value="#{database['jdbc.password']}" />

To read a value from properties file what you do is the same as how we access an element of a map object. We pass the name of the properties as the key in the Spring EL.

<property name="driverClassName" value="#{database['jdbc.driverClassName']}"/>

The MyDataSource class is an imaginary data source object. It has some properties such as the driverClassName, url, username and password. It’s a common parameter you use to connect to a database using a JDBC driver. For simplicity the getters and setters we removed from the class.

package org.kodejava.spring.core.el;

public class MyDataSource {
    private String driverClassName;
    private String url;
    private String username;
    private String password;

    // Getters & Setters

As always, to run the Spring configuration above we will need to create a main class that load and execute the application context. This class will obtain the dataSource bean from the application context and print out its properties whose values are read from a properties file called

package org.kodejava.spring.core.el;


public class SpELPropertiesExample {
    public static void main(String[] args) {
        try (ClassPathXmlApplicationContext context =
                     new ClassPathXmlApplicationContext("spel-properties.xml")) {

            MyDataSource dataSource = (MyDataSource) context.getBean("dataSource");
            System.out.println("driverClassName = " + dataSource.getDriverClassName());
            System.out.println("url             = " + dataSource.getUrl());
            System.out.println("username        = " + dataSource.getUsername());
            System.out.println("password        = " + dataSource.getPassword());

Here are the result you get when running the code snippet:

driverClassName = com.mysql.cj.jdbc.Driver
url             = jdbc:mysql://localhost/kodejava
username        = root
password        = secret

Maven Dependencies


Maven Central Maven Central Maven Central



  1. Hi, i have a problem with util:properties, error (The prefix util for element util:properties is not bound). Thanks!


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.