user2112430
user2112430

Reputation: 123

Spring error - org.springframework.beans.factory.NoSuchBeanDefinitionException

I was starting with Spring tutorial and was trying this simple code,

AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
    ctx.register(HelloWorldConfig.class);
    ctx.register(HelloWorldConfig1.class);
    ctx.refresh();

    HelloWorld helloWorld = ctx.getBean(HelloWorld.class);

    helloWorld.setMessage("Hello World!");
    helloWorld.getMessage();

     HelloWorld1 helloWorld1 = ctx.getBean(HelloWorld1.class);

     helloWorld1.setMessage("Hello World!");
     helloWorld1.getMessage();

this is throwing below exception,

Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.tutorialspoint.HelloWorld] is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:295)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1125)
at com.tutorialspoint.MainApp.main(MainApp.java:13)

Although if I try registering only one config and use bean from there, it just works fine. so HelloWorld/HelloWorldConfig or HelloWorld/HelloWorldConfig1 works fine if used individually, only when I register both together then I get this error.

Other classes below,

            package com.tutorialspoint;
            import org.springframework.context.annotation.*;

            @Configuration
            public class HelloWorldConfig {

               @Bean 
               public HelloWorld helloWorld(){
                  return new HelloWorld(); 
               }
            }

and

                            package com.tutorialspoint;
            import org.springframework.context.annotation.*;

            @Configuration
            public class HelloWorldConfig1 {

               @Bean 
               public HelloWorld1 helloWorld(){
                  return new HelloWorld1(); 
               }
            }

and

package com.tutorialspoint;

            public class HelloWorld {
               private String message;

               public void setMessage(String message){
                  this.message  = message;
               }

               public void getMessage(){
                  System.out.println("Your Message : " + message); 
               }
            }

and

package com.tutorialspoint;

            public class HelloWorld1 {
               private String message;

               public void setMessage(String message){
                  this.message  = message;
               }

               public void getMessage(){
                  System.out.println("Your Message 1: " + message); 
               }
            }

Upvotes: 0

Views: 2147

Answers (1)

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 279970

The @Bean method in

@Configuration
public class HelloWorldConfig1 {
    @Bean 
    public HelloWorld1 helloWorld(){
        return new HelloWorld1(); 
    }
 }

is overwriting the @Bean method from the other @Configuration class. If there is no name attribute in the @Bean declaration, Spring uses the method name as the bean definition name. Since both their names are the same, Spring will only keep the last one it registers as a bean definition.

Rename the method to something else or add a name attribute to the @Bean declaration

@Bean("helloWorld1")

Upvotes: 2

Related Questions