Newsletter

Example On Spring Autowiring byName

Spring » on Sep 3, 2011 { 24 Comments } By Sivateja

In this case, spring framework attempts to find out a bean in the configuration file, whose id is matching with the property name to be wired.  If a bean found with id as property name then that class object will be injected into that property by calling setter injection.  If no id is found then that property remains un-wired, but never throws any exception.

Example On Autowiring byName

public class MyBean
{
     private DemoBean db;
     public void setDb(DemoBean db)
     {
        this.db=db;
     }
}

In the xml file

<beans>
  <bean id="id1" class="MyBean" autowire="byName" />
  <bean id="db" class="DemoBean" />  
</beans>

Explanation:

See line number 3 in MyBean, our class depends on DemoBean class object right,  now see in the xml file line number 2 we have given autowire=”byName“, means when ever spring container notice autowire=”byName” then it will verifies whether the id in xml file is matching with the property name in the MyBean or not, if yes it will wired  automatically else unwired

Am giving one figure to make you understand better 🙂

 

 

Complete Example

Files required…

  • Book.java
  • Categories.java
  • ClientLogic.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 String getName() {
		return name;
	}

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

	public Book getBk() {
		return bk;
	}

	public void setBk(Book bk) {
		this.bk = bk;
	}

	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="byName">
        <property name="name" value="General Books" />
  </bean>
  <bean id="bk" class="java4s.Book">
        <property name="bookname" value="The Kids" />
        <property name="bookprice" value="300" />
  </bean>
</beans>

Notes: We called id1 from ClientLogic.java [line number 15], and in spconfig.xml we have written autowire=byName, so first it will checks for the class with id name bk [as we have written private Book bk in Categories.java ] and inserts bk class [ jaa4s.Book ] properties into that object and then injects value “General Books” into name property of Categories class.

Finally in ClientLogic.java we used to type cast to get our output.

Output

​​

You Might Also Like

  ::. About the Author .::

Java4s_Author
- 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

24 Responses to “Example On Spring Autowiring byName”
  1. venkatesh says:

    Hi,

    Is spring web applications supports only tomcat server or all the java application deployment servers ??

    thanks,
    venkatesh

  2. Thank you “Sivateja” for sharing your knowledge.

  3. sudha says:

    Hi Curser movement is excellent go on……………..

  4. Raghu says:

    Excellent shiva!!!. I was very much confused with autowiring concept.
    After reading your example i got clear idea on Autowiring.
    Thanks a lot for sharing knowledge.

  5. At cursor movement, Bean in the line 2 is also closed and line 4 is also closed.
    Is the id1 is composite of db?

  6. Akila says:

    You are an awesome teacher…. Thanks so much for all this in-depth explanation !

  7. selvakumar.g says:

    Hi Siva,

    It throws java.lang.NullPointerException when no id is found.

  8. Vaibhav says:

    Hi Sivateja thanks for your efforts , your articals are very easy to understand,.thanks lot again.

  9. Ashu Ranjan says:

    love u Sir.. thnks a lot..

  10. Pramod Kumar says:

    Thanku,
    You are explaining very neat & clean with taking some beautiful example,
    I am going to fan of your web site.

  11. Arunkumar Papena says:

    Awesome..!Sweet and simple..Thanks to Sivateja kandula..

  12. raj says:

    thanks sir for providing this tutorial in a simple way that can be easily understood by fresher also

  13. gowtham says:

    Hi,
    Sivateja the above example explanation was awesome.
    Thanks..

  14. pavan kumar says:

    How auto wire byName works when Interface is used .

  15. praveen says:

    Easy to understand with cursor moment ,Keep it up

  16. Sandeep Verma says:

    Thanks for sweet and simple way of explaining.!!!!

  17. Hitesh Upreti says:

    Hi shiva, I will be very thankful if you add some examples of autowiring using annotations because in real projects we mainly use annotations and i am not clear about the autowiring with annotations in java bean class.

  18. Avi Iname says:

    If bean property name and bean id name didn’t match, it will throw

    java.lang.NullPointerException

  19. Onkar Ketkar says:

    What if I need to define one more book information which if of different category. I can’t create one more bean with same id as ‘bk’. How to resolve that?

  20. Narendra says:

    in the above example we are defining
    autowire="byName" for been id="id1" class="java4s.Categories". Then how it will search "byName" for
    bead id = "bk"? will you please explain ???

  21. Narasimman P says:

    Hi,

    If we have multiple spring configuration xml files in our application then autowire will work like as per above example?

  22. prashanth says:

    is autowiring byname & bytype is only for setter injection..

  23. siva says:

    <bean id="Categories" class="com.spring.Categories" autowire="byName">
    <property name="name" value="General Books"/>
    ======> <property name="book" ref="Book"></property>
    </bean>
    <bean id="Book" class="com.spring.Book">
    <property name="bname" value="The Kids"/>
    <property name="price" value="300"/>

    i have doubt. y u not mention ref tag in "id1". isn't it required or not. please give me solution to my doubt

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 - 2018 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.