Autowire Spring Bean By Name Examples

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

1. Spring Autowire Bean By Name Example.

  1. This example contains three spring beans, they are studentBean, middleSchoolBean, and highSchoolBean, they are declared in the below spring bean configuration file.
  2. You can see the 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> 
  3. The middleSchoolBean and highSchoolBean are all instances of the class com.dev2qa.example.spring.bean.autowire.byname.SchoolBean, but with different properties settings.
  4. The middleSchoolBean represents to studentBean middle school info, and the highSchoolBean represents to studentBean high school info.
  5. The studentBean only set two properties’ studentName and studentAge‘s value, and the highSchoolBean and middleSchoolBean property will be autowired to above two same name bean automatically.
  6. Please note, you should add autowire=”byName” in the source bean.

2. Spring Autowire Bean By Name Example Source Code.

    // 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;
  3. Execution Output: If you run the above example, you can find the output as below. All the highSchoolBean and middleSchoolBean property’s values will be print out.
    Student name : Jerry
    Student age : 18
    High School name : Harvard University
    High School address : United State
    High School level : 1
    Middle School name : New York Middle School
    Middle School address : New York
    Middle School level : 2

Leave a Comment

Your email address will not be published. Required fields are marked *

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