Autowire Spring Bean By Name Examples

Besides declare spring bean dependencies in bean configuration file, spring framework provide autowire function to make bean dependency declaration easily. There are below autowire modes, they are no autowire, by bean name, by bean class type, by constructor. This article will show you examples about autowire bean by name.

This example contains three spring beans, they are studentBean, middleSchoolBean and highSchoolBean, they are declared in below spring bean configuration file. You can see studentBean declaration add a new property autowire=”byName”.

 <bean id="highSchoolBean" name="highSchoolBean" class="com.dev2qa.example.spring.bean.autowire.byname.SchoolBean"> 
        <property name="schoolName" value="Harvard University"></property>
        <property name="schoolAddress" value="United State"></property> 
        <property name="schoolLevel" value="1"></property> 

<bean id="middleSchoolBean" name="middleSchoolBean" class="com.dev2qa.example.spring.bean.autowire.byname.SchoolBean"> 
        <property name="schoolName" value="New York Middle School"></property> 
        <property name="schoolAddress" value="New York"></property> 
        <property name="schoolLevel" value="2"></property> 

<bean id="studentBean" name="studentBean" class="com.dev2qa.example.spring.bean.autowire.byname.StudentBean" autowire="byName"> 
        <property name="studentName" value="Jerry"></property> 
        <property name="studentAge" value="18"></property> 

middleSchoolBean and highSchoolBean are all instance of com.dev2qa.example.spring.bean.autowire.byname.SchoolBean class, but with different properties settings. middleSchoolBean represent to studentBean middle school info, and highSchoolBean represent to studentBean hign school info. studentBean only set two property studentName and studentAge’s value, and the highSchoolBean and middleSchoolBean property will be autowired to above two same name bean automatically.

Please Note, you should add autowire=”byName” in source bean.

// Store student information
public class StudentBean {

	private String studentName = "";
	private int studentAge = 10;
	private SchoolBean highSchoolBean;
	private SchoolBean middleSchoolBean;
	public String getStudentName() {
		return studentName;

	public void setStudentName(String studentName) {
		this.studentName = studentName;

	public int getStudentAge() {
		return studentAge;

	public void setStudentAge(int studentAge) {
		this.studentAge = studentAge;

	public SchoolBean getHighSchoolBean() {
		return highSchoolBean;

	public void setHighSchoolBean(SchoolBean highSchoolBean) {
		this.highSchoolBean = highSchoolBean;

	public SchoolBean getMiddleSchoolBean() {
		return middleSchoolBean;

	public void setMiddleSchoolBean(SchoolBean middleSchoolBean) {
		this.middleSchoolBean = middleSchoolBean;

	public static void main(String[] args) {
	   	// Initiate Spring application context, this line code will invoke bean initialize method.
		ApplicationContext springAppContext = new ClassPathXmlApplicationContext("BeanSettings.xml");

		// Get InitAndDestroyByAnnotation by id. 
		StudentBean studentBean = (StudentBean) springAppContext.getBean("studentBean");
                System.out.println("Student name : " + studentBean.getStudentName());
                System.out.println("Student age : " + studentBean.getStudentAge());
                System.out.println("High School name : " + studentBean.getHighSchoolBean().getSchoolName());
                System.out.println("High School address : " + studentBean.getHighSchoolBean().getSchoolAddress());
                System.out.println("High School level : " + studentBean.getHighSchoolBean().getSchoolLevel());
                System.out.println("Middle School name : " + studentBean.getMiddleSchoolBean().getSchoolName());
                System.out.println("Middle School address : " + studentBean.getMiddleSchoolBean().getSchoolAddress());
                System.out.println("Middle School level : " + studentBean.getMiddleSchoolBean().getSchoolLevel());

// Store student belongs school information. 
public class SchoolBean {
	private String schoolName = "";
	private String schoolAddress = "";

	private int schoolLevel = 1;

	public String getSchoolName() {
		return schoolName;

	public void setSchoolName(String schoolName) {
		this.schoolName = schoolName;

	public String getSchoolAddress() {
		return schoolAddress;

	public void setSchoolAddress(String schoolAddress) {
		this.schoolAddress = schoolAddress;

	public int getSchoolLevel() {
		return schoolLevel;

	public void setSchoolLevel(int schoolLevel) {
		this.schoolLevel = schoolLevel;

Execution Output

If you run above example, you can find the output as below. All the highSchoolBean and middleSchoolBean property value will be print out.

Source Code Download

  1. [download id=”2732″]
0 0 votes
Article Rating
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x