Fix: Cannot resolve reference to bean while setting bean property Spring applicationConfig.xml


Exception:
o.s.c.s.ClassPathXmlApplicationContext   : Exception encountered during context initialization - 

cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: 

Error creating bean with name 'employee' defined in class path resource [applicationConfig.xml]: 
Cannot resolve reference to bean 'dept' while setting bean property 'department'
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'employee' defined in class path resource [applicationConfig.xml]: 

Cannot resolve reference to bean 'dept' while setting bean property 'department'
	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:377)

at org.code2care.demo.DemoApplication.main(DemoApplication.java:14)

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'dept' available

We may get the above exception when working with Spring Boot with Config file (applicationConfig.xml) when you have injected a bean using property tag but the reference name is incorrect or the referenced bean does not exists.

Example:
<?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.xsd">

    <bean name="employee" class="org.code2care.demo.Employee">
        <property name="department" ref="dept"/>
    </bean>
    <bean name="department" class="org.code2care.demo.Department"/>
</beans>

As you can see in my case, the issue is that I have used the reference name as dept instead of department.

Fix - Cannot resolve reference to bean x while setting bean property

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap