Is it mandatory to have the “id” property in Bean/ApplicationContext Xml

Typical Bean Configuration XML file is given below.

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

<bean class="com.malliktalksjava.service.EmployeeService">
    <property name="empRepository" ref="employeeDAO"/>

<bean id="employeeDAO" class="com.malliktalksjava.dao.EmployeeDao"/>


From the above config, EmployeeService Bean is Anonymous because no id is supplied explicitly. Hence, Spring Container generates a unique id for that bean. It uses the fully qualified class name and appends a number to it.

However, if you want to refer that bean by name, through the use of the ref element you must provide a name.

Right way of doing is shown using second Bean, its been declared with employeeDAO id attribute in order to be referenced by the empRepository property of the first Bean.