1

I am testing out simple AOP use case in Spring but am getting the below error,

Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'bean1' is defined

Below are my source files,

  1. DemoConfig.java

    package com.luv2code.aopdemo;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.EnableAspectJAutoProxy;
    
    import com.luv2code.aopdemo.aspect.MyDemoLoggingAspect;
    import com.luv2code.aopdemo.dao.AccountDAO;
    
    @Configuration
    @EnableAspectJAutoProxy
    @ComponentScan("com.luv2code.aopdemo")
    public class DemoConfig {
    
        @Bean
        @Qualifier("bean1")
        public AccountDAO accDao() {
            return new AccountDAO();
        }
    
        @Bean
        @Qualifier("bean2")
        public MyDemoLoggingAspect myAscpect() {
            return new MyDemoLoggingAspect();
        }
    }
    
  2. MyDemoLoggingAspect.java

     package com.luv2code.aopdemo.aspect;
    
     import org.aspectj.lang.annotation.Aspect;
     import org.aspectj.lang.annotation.Before;
    
     @Aspect
     public class MyDemoLoggingAspect {
    
        // this is where we add all of our related advices for logging  
        // let's start with an @Before advice
        @Before("execution(** com.luv2code.aopdemo.dao.AccountDAO.addAccount(..))")
        public void beforeAddAccountAdvice() {  
            System.out.println("\n=====>>> Executing @Before advice on addAccount()");
    
        }
     }
    
  3. MainDemoApp.java

    package com.luv2code.aopdemo;
    
    import com.luv2code.aopdemo.dao.AccountDAO;
    
    public class MainDemoApp {
    
        public static void main(String[] args) {
            // read spring config java class
            AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(DemoConfig.class);          
            // get the bean from spring container
            AccountDAO theAccountDAO = context.getBean("bean1", AccountDAO.class);
            // call the business method
            theAccountDAO.addAccount();
            // do it again!
            System.out.println("\nlet's call it again!\n");
            // call the business method again
            theAccountDAO.addAccount();     
            // close the context
            context.close();
        }
    }
    

I have given my bean ID "bean1", even after that Spring is not able to find my bean in the context. Why am I getting this error and how to resolve this?

Elarbi Mohamed Aymen
  • 1,617
  • 2
  • 14
  • 26
Marco Roberts
  • 205
  • 2
  • 9

1 Answers1

5

The @Qualifier tag is used with the @Autowired annotation.

What you need is

@Bean(name="bean1")
public AccountDAO accDao() {
    return new AccountDAO();
}
Evris Tzam
  • 178
  • 9