import org.springframework.stereotype.Component;
@Component
public class Action implements Category {
@Override
public String getCategory() {
return "Action movies are breathtaking";
}
}
now we have two Comedy and Action. If you execute your program now then
------------------------------------------------------------------------------------------------------------
EXCEPTION
------------------------------------------------------------------------------------------------------------
Feb 26, 2018 5:58:31 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@41975e01: startup date [Mon Feb 26 17:58:31 IST 2018]; root of context hierarchy
Feb 26, 2018 5:58:31 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
Feb 26, 2018 5:58:32 PM org.springframework.context.support.AbstractApplicationContext refresh
WARNING: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'bollywood': Unsatisfied dependency expressed through method 'setCat' parameter 0; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.demo.spring.Category' available: expected single matching bean but found 2: action,comedy
Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'bollywood': Unsatisfied dependency expressed through method 'setCat' parameter 0; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.demo.spring.Category' available: expected single matching bean but found 2: action,comedy
to resolve this we will use @Qualifier(bean id)
note : bean id is the classname in smallletters
@Qualifier annotation
------------------------------------------------------------------------------------------------------------
Bollywood.java
------------------------------------------------------------------------------------------------------------
package com.demo.spring;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
@Component("bollywood")
public class Bollywood implements Movie {
private Category cat;
@Override
public String getMovie() {
return "Bollywood id famous for acting and songs";
}
//Setter injection
@Autowired
@Qualifier("comedy")
public void setCat(Category cat) {
this.cat = cat;
}
@Override
public String getCategory() {
return cat.getCategory();
}
}
@Scope annotation(singleton/prototype)
------------------------------------------------------------------------------------------------------------
ScopeEntertainmentTesting.java
------------------------------------------------------------------------------------------------------------
package com.demo.spring;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ScopeEntertainmentTesting {
public static void main(String[] args) {
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
Movie obj=context.getBean("bollywood",Movie.class);
Movie obj1=context.getBean("bollywood",Movie.class);
boolean result=(obj1==obj);
System.out.println(result);
System.out.println(obj);
System.out.println(obj1);
context.close();
}
}
------------------------------------------------------------------------------------------------------------
Bollywood.java
------------------------------------------------------------------------------------------------------------
package com.demo.spring;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Component("bollywood")
@Scope("singleton")
public class Bollywood implements Movie {
private Category cat;
@Override
public String getMovie() {
return "Bollywood id famous for acting and songs";
}
//Setter injection
@Autowired
@Qualifier("comedy")
public void setCat(Category cat) {
this.cat = cat;
}
@Override
public String getCategory() {
return cat.getCategory();
}
}
------------------------------------------------------------------------------------------------------------
OUTPUT
------------------------------------------------------------------------------------------------------------
Feb 26, 2018 6:44:50 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@41975e01: startup date [Mon Feb 26 18:44:50 IST 2018]; root of context hierarchy
Feb 26, 2018 6:44:50 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
true
com.demo.spring.Bollywood@4de5031f
com.demo.spring.Bollywood@4de5031f
Feb 26, 2018 6:44:51 PM org.springframework.context.support.AbstractApplicationContext doClose
INFO: Closing org.springframework.context.support.ClassPathXmlApplicationContext@41975e01: startup date [Mon Feb 26 18:44:50 IST 2018]; root of context hierarchy
------------------------------------------------------------------------------------------------------------
Bollywood.java
------------------------------------------------------------------------------------------------------------
package com.demo.spring;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Component("bollywood")
@Scope("prototype")
public class Bollywood implements Movie {
private Category cat;
@Override
public String getMovie() {
return "Bollywood id famous for acting and songs";
}
//Setter injection
@Autowired
@Qualifier("comedy")
public void setCat(Category cat) {
this.cat = cat;
}
@Override
public String getCategory() {
return cat.getCategory();
}
}
------------------------------------------------------------------------------------------------------------
OUTPUT
------------------------------------------------------------------------------------------------------------
Feb 26, 2018 6:45:25 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@41975e01: startup date [Mon Feb 26 18:45:25 IST 2018]; root of context hierarchy
Feb 26, 2018 6:45:25 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
false
com.demo.spring.Bollywood@4de5031f
com.demo.spring.Bollywood@67e2d983
Feb 26, 2018 6:45:25 PM org.springframework.context.support.AbstractApplicationContext doClose
INFO: Closing org.springframework.context.support.ClassPathXmlApplicationContext@41975e01: startup date [Mon Feb 26 18:45:25 IST 2018]; root of context hierarchy
@postConstruct and @preDestroy annotations
------------------------------------------------------------------------------------------------------------
ScopeEntertainmentTesting .java
------------------------------------------------------------------------------------------------------------
package com.demo.spring;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ScopeEntertainmentTesting {
public static void main(String[] args) {
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
Movie obj=context.getBean("bollywood",Movie.class);
/* Movie obj1=context.getBean("bollywood",Movie.class);
boolean result=(obj1==obj);
System.out.println(result);
System.out.println(obj);
System.out.println(obj1);*/
System.out.println(obj.getMovie());
context.close();
}
}
------------------------------------------------------------------------------------------------------------
Bollywood.java
------------------------------------------------------------------------------------------------------------
package com.demo.spring;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PostMapping;
@Component("bollywood")
//@Scope("prototype")
public class Bollywood implements Movie {
private Category cat;
public Bollywood(){}
@PostConstruct
public void doStart(){
System.out.println("startup");
}
@PreDestroy
public void doFinish(){
System.out.println("Finish");
}
@Override
public String getMovie() {
return "Bollywood id famous for acting and songs";
}
//Setter injection
@Autowired
@Qualifier("comedy")
public void setCat(Category cat) {
this.cat = cat;
}
@Override
public String getCategory() {
return cat.getCategory();
}
}
------------------------------------------------------------------------------------------------------------
OUTPUT
------------------------------------------------------------------------------------------------------------
Feb 26, 2018 7:32:21 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@7dc5e7b4: startup date [Mon Feb 26 19:32:21 IST 2018]; root of context hierarchy
Feb 26, 2018 7:32:21 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
startup
Bollywood id famous for acting and songs
Feb 26, 2018 7:32:21 PM org.springframework.context.support.AbstractApplicationContext doClose
INFO: Closing org.springframework.context.support.ClassPathXmlApplicationContext@7dc5e7b4: startup date [Mon Feb 26 19:32:21 IST 2018]; root of context hierarchy
Finish