Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.dav.spring.domain.HelloWorldImpl] is defined at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:319) at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:985) at Test.main(Test.java:24)
interface :
public interface HelloWorld {
void sayHello(String helloStr);}
impl class :
package com.dav.spring.domain;
public class HelloWorldImpl implements HelloWorld {
@Override
public void sayHello(String helloStr) {
System.out.println("Welcome to spring 4" + helloStr);
}
}
Configuraion Class:
@Configuration
public class MyConfiguration {
@Bean(name = "helloworld")
@Description("This is newly added in spring 4")
@Scope("prototype") // If removed this, it's fine.
HelloWorld getHelloWorldImpl() {
return new HelloWorldImpl();
}
}
Main class:
public class Test {
public static void main(String[] args) {
// We did configuration by using JavaConfiguration so
// we should use like this
AnnotationConfigApplicationContext antConf = new AnnotationConfigApplicationContext(
MyConfiguration.class);
HelloWorld byParentClass = antConf.getBean(HelloWorld.class);
byParentClass.sayHello(" Raju");
System.out.println(byParentClass.hashCode());
HelloWorld byBeanName = (HelloWorld) antConf.getBean("helloworld");
byBeanName.sayHello(" Chinna");
System.out.println(byBeanName.hashCode());
HelloWorldImpl byImplClass = antConf.getBean(HelloWorldImpl.class);
byImplClass.sayHello(" Daveedu Raju");
}
}