1. When you create multiple beans of the same type and want to assemble only one of them, in this case, you can use @Qualifier annotations and @Autowired annotations to configure them precisely.
2. Example
Java
Public classStudent {PrivateInteger age; PrivateString name; Public voidsetage (Integer age) { This. Age =Age ; } PublicInteger getage () {returnAge ; } Public voidsetName (String name) { This. Name =name; } PublicString GetName () {returnname; }}
public class profile {@Autowired @Qualifier ( "student1" ) private Student Student; public profile () {System.out.println ( /span> "Inside profile constructor." ); public void Printage () {System.out.println ( Age: "+ Student.getage ()); public void Printname () {System.out.println ( "Name:" + Student.getname ()); }}
Public class Mainapp { publicstaticvoid main (string[] args) { new Classpathxmlapplicationcontext ("Beans.xml"); = (profile) Context.getbean ("Profile"); Profile.printage (); Profile.printname (); }}
Beans.xml
<?XML version= "1.0" encoding= "UTF-8"?><Beans> <Context:annotation-config/>
<BeanID= "Profile"class= "Com.tutorialspoint.Profile"></Bean>
<BeanID= "Student1"class= "Com.tutorialspoint.Student"> < Propertyname= "Name"value= "Zara" /> < Propertyname= "Age"value= "One"/> </Bean>
<BeanID= "Student2"class= "Com.tutorialspoint.Student"> < Propertyname= "Name"value= "Nuha" /> < Propertyname= "Age"value= "2"/> </Bean>
</Beans>
Print output
Inside profile constructor. Age:11name:zara
Use @Qualifier comments and @Autowired comments by specifying which real bean will be assembled to eliminate clutter