How to Solve Error Message Configuration problem: element for property must specify a ref or value in Java Spring Application

Posted on

Introduction

Actually, this article still have a connection with the previous article focusing on how to create a Java Spring Application. That article is an article where the title is ‘How to Create a Simple Java Spring Application using NetBeans IDE’ and it exist in this link. In that article, there is a Java Spring application where it become the main basis for showing the error message appear as in the title of this article. The following is the full and complete error display :

Caused by: org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: <property> element for property 'id' must specify a ref or value
Offending resource: class path resource [spring-config.xml]
Property 'id'
-> Bean 'employeeBean'

The following is the condition and the situation which is triggering the error above concerning several files. The first one is the Spring Configuration file where it exist in ‘src/java/resources’ with the name of ‘spring-config.xml’ as an example. The content is available as follows :

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
       <bean id="employeeBean" class="com.test.spring.Employee">
             <property name="id" />
       </bean>
</beans>

Furthermore, there is a Java Spring Bean exist in the form of a Java class with the name of ‘Employee’. The following is the content of the file :

package com.test.spring;

import java.util.List;

public class Employee {
    public String id;

    public void setId(String id) {
         this.id = id;
    }

    public void identifyID(){
         System.out.println("I am an "+this.id+" !");
    }
}

Last but not least, the following is the class with the name of ‘SpringMain.java”. It is the main program for executing the main Java Spring application. Below is the content of the file :

package com.test.spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringMain {
     public static void main(String[] args){
            ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
            Employee employee1 = (Employee) context.getBean("employeeBean");
            employee1.identifyID();
     }
}

Solution

Actually, the solution for the above error message is very simple. Analyzing the error message, the one triggering the error message is the property with the name of ‘id’. That part of the error message exist in the following line :

Configuration problem: <property> element for property 'id' must specify a ref or value

It other words, there is a need to specify a ref or value for the property with the element of ‘id’. It is considered as a ‘Configuration problem. So, where is the configuration actually exist ?. The information available in the next part of the error message as follows :

Offending resource: class path resource [spring-config.xml]

Actually, according to the above line, there is one reason which is being offended triggering the error message. It is a file with the name of ‘spring-config.xml’. The explanation further exist in the next line informing the property as followss :

Property 'id'

Last but not least, it is informing the Java Spring Bean name where the property exist. It is in the following line :

-> Bean 'employeeBean'

In the end, the solution is possible to implement by editing the spring-config.xml file by adding the following attribute for the property ‘id’ :

<property name="id" value="Employee of the Company" />

Last but not least, after changing it by adding a ‘value’ attribute with a certain string of value, the execution will be exist as an output in the following image  :

Leave a Reply