Home > OS >  Why do I get the error named "NoSuchBeanDefinitionException: No bean named 'database'
Why do I get the error named "NoSuchBeanDefinitionException: No bean named 'database'

Time:08-24

After initializing context like this in my spring project :

AnnotationConfigApplicationContext context = 
            new AnnotationConfigApplicationContext();

after that I've tried to refresh the config file before calling "getBean()" :

context.refresh(); // explicit refresh of the config file.

and then :

ICustomerDal customerDal
        = context.getBean("database", ICustomerDal.class);
    customerDal.add();

But in the end I've got the "NoSuchBeanDefinitionException: No bean named 'database' available" error.

ICustomerDal.class File :

package com.springdemo;

public interface ICustomerDal {
    void add();
}

MySqlCustomerDal.class File :

package com.springdemo;

import org.springframework.stereotype.Component;

@Component("database")
public class MySqlCustomerDal implements ICustomerDal {
    String connectionString;
    // Getter
    public String getConnectionString() {
        return connectionString;
    }
    // Setter
    public void setConnectionString(String connectionString) {
        this.connectionString = connectionString;
    }
    @Override
    public void add() {
        System.out.println("Connection String : "   this.connectionString);
        System.out.println("Added to MySQL Database!"); // MySQL Codes.
        
    }
}

IocConfig.class File:

package com.springdemo;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan("com.springdemo")
public class IocConfig {

}

But if I do it like the code block below, there is no error :

AnnotationConfigApplicationContext context = 
            new AnnotationConfigApplicationContext("com.springdemo"); // implicitly registering and refreshing config file.

    ICustomerDal customerDal = context.getBean("database", ICustomerDal.class);
    customerDal.add();

I just couldn't understand that if I get "NoSuchBeanDefinitionException" in the first scenario, how come I get no error just after passing the package name in ApplicationContext parameter?

Thank you in advance for your precious thoughts.

CodePudding user response:

For anyone who could encounter the same issue here :

Actually I have managed to comprehend and solve the situation here.

I realized that I need to specify the config file either in ApplicationContext or manually by registering.

Also we can specify the config file like this for another bonus method :

new AnnotationConfigApplicationContext(IocConfig.class); // Specifying the exact class name rather than the package name itself. 

And using the code below helped me for another method instead of passing a parameter into "AnnotationConfigApplicationContext" :

   context.register(IocConfig.class); // --> To specify the config file. 
   context.refresh();                 // --> Need to explicitly refresh the config file after registering.
  • Related