Fix: NoSuchBeanDefinitionException: No bean named x available (application-config.xml)

If you are working with Spring Boot with a Spring Config file (application-config.xml) you may come across NoSuchBeanDefinitionException when you create an object of a bean in your code using the ApplicationContext object getBean() method but the bean is not defined in the Spring config file.

package org.code2care.demo;

public class Employee {

    private int empId;
    private String empName;
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""

    <bean name="department" class="org.code2care.demo.Department"></bean>

package org.code2care.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

public class DemoApplication {

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

        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("application-config.xml");
        Employee employee = applicationContext.getBean("employee", Employee.class);


Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: 

No bean named 'employee' available

	at org.code2care.demo.DemoApplication.main(

As you can see I have not defined the bean tag under the application-config.xml file for the class employee, to fix this issue, I will need to create it as well.

<bean name="employee" class="org.code2care.demo.Employee"></bean>
Fix NoSuchBeanDefinitionException

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:

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