Compound Property Names in Spring

With this example I will show you how compound or nested property names work in Spring application. Compound or nested property names are perfectly legal when setting bean properties, as long as all components of the path except the final property name are non-null.

You can use compound or nested property names when you set bean properties, as long as all components of the path except the final property name are not null.

For example, in this bean definition:

<bean id="foo" class="foo.Bar">
  <property name="fred.bob.sammy" value="123"/>

the foo bean has a fred property which has a bob property, which has a sammy property, and that final sammy property is being set to a scalar value of 123. In order for this to work, the fred property of foo, and the bob property of fred must both be non-null after the bean is constructed, or a null-pointer exception will be thrown.


Java at least 8, Spring Boot 2.4.1, Maven 3.6.3, Gradle 6.7.1

Project Setup

You can create a gradle or maven based project in your favorite IDE or tool.

For gradle based project you can use the following build.gradle script:

buildscript {
	ext {
		springBootVersion = '2.4.1'
    repositories {
    	maven {
    		url ''
    dependencies {

plugins {
    id 'java-library'
    id 'org.springframework.boot' version "${springBootVersion}"

sourceCompatibility = 12
targetCompatibility = 12

repositories {

dependencies {

For maven based project you can use the following pom.xml file:

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns=""






Compound Property Config

Create a source folder src/main/resources if it does not exist already. Create an XML file called compound-property-config.xml with the following content.

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

	<bean id="compoundPropertyName"
		<property name="foo.fred.bob.jhon" value="Jhon Rambo" />


Required Classes

Now you need to create the required classes which has been configured in the above XML config file for compound property names.

Class – Bob


public class Bob {

	private String jhon;

	public String getJhon() {
		return jhon;

	public void setJhon(String jhon) {
		this.jhon = jhon;


Class – Fred


public class Fred {

	private Bob bob;

	public Fred() {
		bob = new Bob();

	public Bob getBob() {
		return bob;

	public void setBob(Bob bob) {
		this.bob = bob;


Class – Foo


public class Foo {

	private Fred fred;

	public Foo() {
		fred = new Fred();

	public Fred getFred() {
		return fred;

	public void setFred(Fred fred) {
		this.fred = fred;


Class – Bar


public class Bar {

	private Foo foo;

	public Bar() {
		foo = new Foo();

	public Foo getFoo() {
		return foo;

	public void setFoo(Foo foo) { = foo;


Load XML Config File

You need to load the XML configuration file which was put under the class path folder src/main/resources. So create the below Config Java class to load the XML file using annotation @ImportResource.


import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;

public class Config {


Main Class

A class with main method and @SpringBootApplication annotation is used to start the Spring Boot application. I am using CLI version for starting the app.

I am injecting the ApplicationContext which is easily available in the Spring Boot based application.


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

public class SpringCompoundPropertyNameApp implements CommandLineRunner {

	private ApplicationContext applicationContext;

	public static void main(String[] args) {, args);

	public void run(String... args) throws Exception {
		Bar bar = applicationContext.getBean(Bar.class);



Testing the Application

Running the above main class you will see the following output:

Jhon Rambo

That’s all about compound property names in Spring application.

Source Code


Leave a Reply

Your email address will not be published. Required fields are marked *