Newsletter

Example On Spring Autowiring by Constructor

Spring » on Feb 8, 2012 { 14 Comments } By Sivateja

Actually Spring Autowiring by constructor is similar to spring autowiring byType [ internally it will considers as byType only ]  but with little difference, in byType we used setter injection here we have to use constructor injection 🙂  nothing more than that.

Friends am using same application to show the difference between byName, byType, constructor, autodetect to avoid confusions [ Out put is same for all these 4 applications 😉 ]

Example On Spring Autowiring Constructor

Files required…

  • Book.java
  • ClientLogic.java
  • Categories.java
  • spconfig.xml

Book.java

package java4s;
public class Book {

       private String bookname;
       private int bookprice;
       
    public String getBookname() {
        return bookname;
    }
    public void setBookname(String bookname) {
        this.bookname = bookname;
    }
    public int getBookprice() {
        return bookprice;
    }
    public void setBookprice(int bookprice) {
        this.bookprice = bookprice;
    }
          
}

Categories.java

package java4s;
public class Categories {

    private String name;
    private Book bk;
       
    public Categories(Book bk)
    {
           this.bk=bk;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    

    public void show()
    {
        System.out.println("Categories name :"+name);
        System.out.println("Book name :"+bk.getBookname()+" and Book Price :"+bk.getBookprice());
    }
        
}

ClientLogic.java

package java4s;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

public class ClientLogic {
    
    public static void main(String[] args)
    {
        Resource res = new ClassPathResource("spconfig.xml");
        BeanFactory factory = new XmlBeanFactory(res);
        
        Object o = factory.getBean("id1");
        Categories wb = (Categories)o;
        
        wb.show();
        
    }

}

spconfig.xml

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">

<beans>
  <bean id="id1" class="java4s.Categories" autowire="constructor">
      <property name="name" value="General Books" />
  </bean>
  <bean id="SomeThing" class="java4s.Book">
      <property name="bookname" value="The Kids" />
      <property name="bookprice" value="300" />
  </bean>         
</beans>

Notes:  Friends am not explaining this program, as i told you earlier Spring Autowiring constructor is similar to Spring Autowiring byType but here we use constructor injection, please refer byType if you still have any doubts.

Output

​​

You Might Also Like

  ::. About the Author .::

Java4s_Author
Sivateja Kandula - Java/J2EE Full Stack Developer
Founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog, Love Java and UI frameworks.
You can sign-up for the Email Newsletter for your daily dose of Java tutorials.

Comments

14 Responses to “Example On Spring Autowiring by Constructor”
  1. Rajesh says:

    where is constructor injection happened here???

  2. Java4s says:

    @Rajesh

    Steps:

    – In ClientLogic.java, line number 15 we have called id1
    – So controller comes to spconfig.xml line number 5, there it will inserts ‘general books‘ into ‘name‘ normally

    Now your doubt__ ?
    – In spconfig.xml line number 5, we have given autowire=”constructor”, hence check Categories.java(line number 7), we have property bk of type Book, so in spconfig.xml controller verifies class attribute contains ‘Book’ which is at line number 8, and bla bla..

    Hope you are clear 🙂 Please refer previous concepts.

  3. still not clear on autowiring “byType”

  4. sundar says:

    I think constructor not use byType it will use byName.
    Please confirm i tested.

    • Khushwant says:

      its very late response but you are right , If constructor argument is matching with bean id then it goes for byName. But if id is not matching then it will check for byType.

  5. Here in spconfig.xml line no 9 & 10 you are using property injection and in your above explanation you have mentioned that autowire by constructor using injection type is constructor not property.so don’t you think in line 9 & 10 it should constructor-arg instead of property?

    Please correct me if i am wrong.

    Surendra Singh

  6. Rishabh says:

    Hi all,

    From my understanding I try to clear it.
    In the xml file controllers goes to the next bean(id=SomeThing) and check its type -(Line no -8)
    The type is of Book type.
    Now It will go to the Categories class since for this class U have set the auto wire =constructor and will try to find if this class has any constructor which must have Book type parameter.
    It find the Book type parameter constructor and hence now the properties of the id=SomeThing would be automatically wired with the first bean(bean id=”id1″).

  7. mohan says:

    its pretty clear friend .. and ur rocking !!!

  8. Azhagumuthu G says:

    Thanks

  9. rao says:

    thanks now i understood @Rishab

  10. Yogesh Joshi says:

    I didnt get exact difference between by Type and by Constructor. Can you describe step wise process for by Constructor example

  11. somireddi ganesh says:

    Hi sivateja

    Acutally I didnt get byType autowire please explain clrearly.

  12. Nandhini says:

    Hi
    Why second bean id="Something" not Book and <constuctor-arg ref="Book"> is not used as first bean property.
    Please clarify.
    Thanks

  13. arun singh says:

    Thanks

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
Most Recent Posts from Top Categories
Spring Boot Hibernate Spring
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2024 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.