Tuesday, 21 August 2018

Causes and Solutions for NoSuchBeanDefinitionException in Spring

         In the previous post, we discussed the causes and solutions for LazyInitializationException in Hibernate. In this post, we will discuss the causes and solutions for NoSuchBeanDefinitionException in Spring.

Spring provides several exceptions related to bean creation and dependency injection, such as NoSuchBeanDefinitionException, NoUniqueBeanDefinitionException, BeanInstantiationException, and CannotLoadBeanClassException. Among these, NoSuchBeanDefinitionException is one of the most common exceptions encountered by Spring developers.

The NoSuchBeanDefinitionException is thrown when the BeanFactory is asked to provide a bean instance, but it cannot find a corresponding bean definition. This may occur because the requested bean does not exist, multiple beans match the requested type, or a singleton instance has been registered manually without an associated bean definition.

Below are some common reasons why the BeanFactory cannot find a bean definition.

1) Bean doesn't exist or it was not registered

         In the below example, the Student class is not annotated with @Bean or @Component, so it can not find the bean of Student.

@Configuration
public class Example {
    public static void main(String[] args) throws Exception {
        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(App.class);
        ctx.getBean(Student.class);
    }
}

class Student {
  //
}

The Student bean is not registered with annotation or XML configuration, so can not find the Student bean, it will through the error,

Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException:
    No qualifying bean of type [com.adnblog.Student] is defined

There are several ways to register bean definitions in Spring. Some of the most common approaches are:

  1. Using the @Bean annotation in a @Configuration class or the <bean> element in an XML configuration file.

  2. Using the @Component annotation and its specialized stereotype annotations (@Service, @Repository, and @Controller) along with @ComponentScan, or the <context:component-scan> element in an XML configuration file.



2) Multiple matching beans


     In some cases we need multiple beans of same type. In XML configuration we are defining two beans of same type e.g Student in the below configuration.

<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
class ="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
        
  <bean id="school" class="com.test.School" >

  <bean id="student1" class="com.test.Student" >
       <property name="name" value="Mahesh" />
  </bean>

  <bean id="student2" class="com.test.Student" >
       <property name="name" value="Rajesh" />
  </bean>

</beans>

The below code to call the Student bean in the School class as follow,

package com.test;

import org.springframework.beans.factory.annotation.Autowired;

public class School{

     @Autowired
     private Student student;
 
     // other property and setter and getters
}

If you run the above code, will get the exception as,

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException:
 No unique bean of type [com.test.Student] is defined:
  expected single matching bean but found 2: [student1, student2]

Because Spring doesn't know which bean should autowire.

To avoid this confusion or exception, should use @Qualifier annotation.

package com.test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

public class School{

        @Autowired
        @Qualifier("student1")
        private Student student;
 
        // other property and setter and getters
}

Reference : Spring @Qualifier Annotation with example


3) Using wrong bean name


  Just as there are multiple ways to register beans in Spring, there are also multiple ways to assign names to them.

When using the @Bean annotation, you can specify the bean name using its name or value attribute. If no name is specified, the name of the annotated method is used as the bean name by default. If a name is explicitly provided, the method name is ignored.

When using XML configuration, the <bean> element provides the id and name attributes for naming a bean. The id attribute specifies the unique identifier for the bean, while the name attribute can be used to define one or more aliases for the same bean.


Thank you for visiting blog.


Related Posts:--

No comments:

Post a Comment