May 14, 2021 Spring
There may be a situation where when you create multiple beans of the same type and want to assemble only one of them with one property, in which case you can use @Qualifier comments and @Autowired comments to eliminate confusion by specifying which real beans will be assembled. An example of using @Qualifier comments is shown below.
Let's put the Eclipse IDE to work, follow these steps to create a Spring application:
Steps | Describe |
---|---|
1 | Create a project called SpringExample, and create a package called com.tutorialspoint under the src folder of the project you created. |
2 | Use the Add External JARs option to add the required Spring library files, as explained in the Spring Hello World Example section. |
3 | Create Java Class Student, Profile, and MainApp under the com.tutorialspoint package. |
4 | Create the Beans profile Beans file under the src .xml. |
5 | The final step is to create the contents of all Java files and bean profiles and run the application as explained below. |
Here's .java the Student file:
package com.tutorialspoint;
public class Student {
private Integer age;
private String name;
public void setAge(Integer age) {
this.age = age;
}
public Integer getAge() {
return age;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
Here's .java profile file:
package com.tutorialspoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
public class Profile {
@Autowired
@Qualifier("student1")
private Student student;
public Profile(){
System.out.println("Inside Profile constructor." );
}
public void printAge() {
System.out.println("Age : " + student.getAge() );
}
public void printName() {
System.out.println("Name : " + student.getName() );
}
}
Here's what .java mainApp files are:
package com.tutorialspoint;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
Profile profile = (Profile) context.getBean("profile");
profile.printAge();
profile.printName();
}
}
Consider the following example of .xml Beans:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config/>
<!-- Definition for profile bean -->
<bean id="profile" class="com.tutorialspoint.Profile">
</bean>
<!-- Definition for student1 bean -->
<bean id="student1" class="com.tutorialspoint.Student">
<property name="name" value="Zara" />
<property name="age" value="11"/>
</bean>
<!-- Definition for student2 bean -->
<bean id="student2" class="com.tutorialspoint.Student">
<property name="name" value="Nuha" />
<property name="age" value="2"/>
</bean>
</beans>
Once you've made the first two changes in the source file and bean profile, let's run the application. If your application is all right, this will output the following message:
Inside Profile constructor.
Age : 11
Name : Zara