<p>There are many collaborating bean in Spring for develop an application, the <b>autowire </b>help in making the relationship between them. Bean <b>Autowiring </b>reduces the effort of writing <b>properties or constructor arguments</b>. Bean <b>Autowiring </b>is the feature provided by the spring framework to skip the some of configuration in XML file. The bean <b>autowiring </b>in specified at the <b>autowire </b>attribute inside <;bean>;<;/bean>; element.</p>
<div dir="ltr" style="text-align: left;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div id="ads-id" align="center"></div>
<pre class="highlight"><;bean autowire="byName" class="com.dineshonjava.sdnext.beanautowiring.Triangle" id="triangle">;<;/bean>; 
</pre>
<div style="color: #274e13;">
<p> ;</p>
<div class="separator" style="clear: both; text-align: center;"></div>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2012/06/spring-bean-autowiring.jpg" border="0" /></div>
<p> ;</p>
</div>
<div style="background-color: #f2f9fc; border: 1px solid #c9e6f2; border-radius: 3px; padding: 16px; line-height: 1.45;">
<p><span style="color: red; font-size: x-large; text-align: center;"><b>Popular Tutorials</b></span></p>
<ul style="text-align: left;">
<li><b><a href="https://dineshonjava.com/spring-tutorial/"><em><strong>Spring Tutorial</strong> </em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-web-mvc-framework-chapter-38/"><strong><em>Spring MVC Web Tutorial </em></strong></a></b></li>
<li><b><a href="https://dineshonjava.com/introduction-to-spring-boot-a-spring-boot-complete-guide/"><strong>Spring Boot Tutorial</strong> </a></b></li>
<li><b><a href="https://dineshonjava.com/spring-security-take-baby-step-to-secure/"><em>Spring Security Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-tutorial-with-example-aspect-advice-pointcut-joinpoint/"><em>Spring AOP Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/using-spring-jdbc-framework-chapter-32/"><em>Spring JDBC Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-hateoas-hypermedia-driven-restful-web-service/"><em><strong>Spring HATEOAS </strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/microservices-with-spring-boot/"><em><strong>Microservices with Spring Boot</strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/jax-rs-web-service-tutorial/"><strong><em>REST Webservice</em> </strong></a></b></li>
<li><b><a href="https://dineshonjava.com/core-java-baby-step-to-be-best-java-ian/"><em><strong>Core Java </strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/hibernate-3-on-baby-steps/"><em><strong>Hibernate Tutorial</strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-batch-process-with-example/"><strong><em>Spring Batch</em> </strong></a></b></li>
</ul>
</div>
<div style="color: #274e13;">
<h2><b>Bean Autowiring Modes</b></h2>
<p>There are five bean autowiring modes which can be used to instruct <a href="https://dineshonjava.com/spring-ioc-container/">Spring container</a> to use bean autowiring for <a href="https://dineshonjava.com/dependency-injection-in-spring/">dependency injection</a>. In above syntax we have used the autowire attribute of the element to specify autowire mode for a bean definition.</p>
<table border="2">
<tbody>
<tr>
<td style="background-color: #6aa84f; color: blue; width: 114px;"><b> Mode</b></td>
<td style="background-color: #6aa84f; color: blue;"><b>Explanation</b></td>
</tr>
<tr>
<td style="background-color: #b6d7a8; color: #274e13; width: 114px;"><i><b>no</b></i></td>
<td style="color: #274e13;">It is default which define no autowiring.</td>
</tr>
<tr>
<td style="background-color: #b6d7a8; color: #274e13; width: 114px;"><a href="https://dineshonjava.com/spring-autowiring-by-name/"><i><b>byName</b></i></a></td>
<td style="color: #274e13;">Autowiring is done by property name. <a href="https://dineshonjava.com/spring-ioc-container/">Spring container</a> looks at the properties of the beans on which <i>autowire</i> attribute is set to <a href="https://dineshonjava.com/spring-autowiring-by-name/"><i>byName</i> </a>in the XML configuration file. It then tries to match and wire its properties with the beans defined by the same names in the configuration file.</td>
</tr>
<tr>
<td style="background-color: #b6d7a8; color: #274e13; width: 114px;"><a href="https://dineshonjava.com/spring-autowiring-by-type/"><i><b>byType</b></i></a></td>
<td style="color: #274e13;">Autowiring is done by matching data type of property name. <a href="https://dineshonjava.com/spring-ioc-container/">Spring container</a> looks at the properties of the beans on which <i>autowire</i> attribute is set to <a href="https://dineshonjava.com/spring-autowiring-by-type/"><i>byType</i></a> in the XML configuration file. It then tries to match and wire a property if its <b>type</b> matches with exactly one of the beans name in configuration file. If more than one such beans exists, a fatal exception is thrown.</td>
</tr>
<tr>
<td style="background-color: #b6d7a8; color: #274e13; width: 114px;"><a href="https://dineshonjava.com/spring-autowiring-by-constructor/"><i><b>constructor</b></i></a></td>
<td style="color: #274e13;">Autowiring is done by matching data type of property name with the property constructor argument. i.e. Similar to <a href="https://dineshonjava.com/spring-autowiring-by-type/"><i>byType</i></a>, but type applies to constructor arguments. If there is not exactly one bean of the constructor argument type in the container, a fatal error is raised.</td>
</tr>
<tr>
<td style="background-color: #b6d7a8; color: #274e13; width: 114px;"><a href="https://dineshonjava.com/spring-autowiring-by-autodetect/"><i><b>autodetect</b></i></a></td>
<td style="color: #274e13;">When default constructor with no argument, it auto-wire by data type or it auto-wire by constructor means that Spring first tries to wire using <b><i>autowire </i></b>by <a href="https://dineshonjava.com/spring-autowiring-by-constructor/"><i>constructor</i></a>, if it does not work, Spring tries to <b><i>autowire </i></b>by <a href="https://dineshonjava.com/spring-autowiring-by-type/"><i>byType</i></a>..</td>
</tr>
</tbody>
</table>
</div>
<h2 style="color: blue;">1. Auto-Wiring &#8220;no&#8221;</h2>
<div style="color: #274e13;">This is the default mode, you need to wire your bean via <i><b>‘ref’</b></i> attribute.</div>
<pre class="highlight"><;bean class="com.dineshonjava.sdnext.autowiring.Circle" id="circle">; 
 <;property name="center" ref="center">; 
<;/property>;<;/bean>; 
<;bean class="com.dineshonjava.sdnext.autowiring.Point" id="center">;<;/bean>; 
</pre>
<h2 style="color: blue;">2. <a href="https://dineshonjava.com/spring-autowiring-by-name/">Auto-Wiring &#8220;byName&#8221;</a></h2>
<div style="color: #274e13;">Auto-wire a bean by property name. In this case, since the name of &#8220;center&#8221; bean is same with the name of the &#8220;circle&#8221; bean’s property (&#8220;center&#8221;), so, Spring will auto wired it via setter method – &#8220;setCenter(Point center)&#8221;.</div>
<pre class="highlight"><;bean autowire="byName" class="com.dineshonjava.sdnext.autowiring.Circle" id="circle">;<;/bean>; 
<;bean class="com.dineshonjava.sdnext.autowiring.Point" id="center">;<;/bean>; 
</pre>
<p><b>Circle.java</b></p>
<pre class="highlight">public class Circle{ 
 private Point center; 
 -- 
 public void setCenter(Point center){ 
 this.center = center; 
 } 
</pre>
<p><a href="https://dineshonjava.com/spring-autowiring-by-name/">Click for see the full Example with Triangle class.</a></p>
<h2 style="color: blue;">3. <a href="https://dineshonjava.com/spring-autowiring-by-type/">Auto-Wiring &#8220;byType&#8221;</a></h2>
<div style="color: #274e13;">Auto-wire a bean by property data type. In this case the data type of &#8220;center&#8221; bean is same with the data type of the &#8220;circle&#8221; bean’s property (&#8220;center&#8221;), so, Spring will auto wired it via setter method – &#8220;setCenter(Point center)&#8221;.</div>
<pre class="highlight"><;bean autowire="byType" class="com.dineshonjava.sdnext.autowiring.Circle" id="circle">;<;/bean>; 
<;bean class="com.dineshonjava.sdnext.autowiring.Point" id="center">;<;/bean>; 
</pre>
<p><b>Circle.java</b></p>
<pre class="highlight">public class Circle{ 
 private Point center; 
 -- 
 public void setCenter(Point center){ 
 this.center = center; 
 } 
</pre>
<p><a href="https://dineshonjava.com/spring-autowiring-by-type/">Click for see the full Example with Circle class.</a></p>
<h2 style="color: blue;">4. <a href="https://dineshonjava.com/spring-autowiring-by-constructor/">Auto-Wiring &#8220;constructor&#8221;</a></h2>
<div style="color: #274e13;">Auto-wire a bean by property data type in constructor argument. In this case, since the data type of &#8220;center&#8221; bean is same as the constructor argument data type in &#8220;circle&#8221; bean’s property (Point center), so, Spring auto wired it via constructor method – &#8220;public Circle(Point center)&#8221;.</div>
<pre class="highlight"><;bean autowire="constructor" class="com.dineshonjava.sdnext.autowiring.Circle" id="circle">;<;/bean>; 
<;bean class="com.dineshonjava.sdnext.autowiring.Point" id="center">;<;/bean>; 
</pre>
<p><b>Circle.java</b></p>
<pre class="highlight">public class Circle{ 
 private Point center; 
 -- 
 public Center(Point center){ 
 this.center = center; 
 } 
</pre>
<p><a href="https://dineshonjava.com/spring-autowiring-by-constructor/">Click for see the full Example with Circle class.</a></p>
<h2 style="color: blue;">5. <a href="https://dineshonjava.com/spring-autowiring-by-autodetect/">Auto-Wiring &#8220;autodetect&#8221;</a></h2>
<div style="color: #274e13;">If a default constructor is found, uses &#8220;constructor&#8221;; Otherwise, uses &#8220;<a href="https://dineshonjava.com/spring-autowiring-by-type/"><i><b>byType</b></i></a>&#8220;. In this case, since there is a default constructor in &#8220;<b>Circle</b>&#8221; class, so, Spring auto wired it via <b><i>constructor</i></b> method – &#8220;public Circle(Point center)&#8221;.</div>
<pre class="highlight"><;bean autowire="autodetect" class="com.dineshonjava.sdnext.autowiring.Circle" id="circle">;<;/bean>; 
<;bean class="com.dineshonjava.sdnext.autowiring.Point" id="center">;<;/bean>; 
</pre>
<p><b>Circle.java</b></p>
<pre class="highlight">public class Circle{ 
 private Point center; 
 -- 
 public Center(Point center){ 
 this.center = center; 
 } 
</pre>
<p><a href="https://dineshonjava.com/spring-autowiring-by-autodetect/">Click for see the full Example with Circle class.</a></p>
</div>
<div style="color: #274e13;">Autowiring works best when it is used consistently across a project. If <b>autowiring </b>is not used in general, it might be confusing to developers to use it to wire only one or two bean definitions. Though, <b>autowiring </b>can significantly reduce the need to specify properties or constructor arguments but you should consider the <b>limitations and disadvantages</b> of <b>autowiring </b>before using them. Following limitations are</div>
<ul style="text-align: left;">
<li style="color: #274e13;">confusing nature</li>
<li style="color: #274e13;">primitive data type</li>
<li>overriding possibility</li>
</ul>
</div>
<div style="background-color: #f2f9fc; border-radius: 3px; border: 1px solid #c9e6f2; line-height: 1.45; padding: 16px;">
<p><span style="color: red; font-size: x-large; text-align: center;"><b>Spring Related Topics you may like</b></span></p>
<ul>
<li><b><a href="https://dineshonjava.com/interview-questions-on-spring/"><span style="color: red;">Spring Interview Questions and Answers</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-interview-questions-and-answers/"><span style="color: red;">Spring AOP Interview Questions and Answers</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-mvc-interview-questions-and-answers/"><span style="color: red;">Spring MVC Interview Questions</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-security-interview-questions-and-answers/"><span style="color: red;">Spring Security Interview Questions and Answers</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-rest-web-services-interview-questions-and-answers/"><span style="color: red;">Spring REST Interview Questions and Answers</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-boot-interview-questions-and-answers/"><span style="color: red;">Spring Boot Interview Questions and Answers</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-boot-microservices-interview-questions-answers/"><span style="color: red;">Spring Boot Microservices Interview Questions and Answers</span></a></b></li>
<li><b><a href="https://dineshonjava.com/dependency-injection-in-spring/"><span style="color: red;">Dependency Injection (DI) in Spring </span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-ioc-container/"><span style="color: red;">Spring IoC Container</span></a></b></li>
<li><b><a href="https://dineshonjava.com/what-is-bean-factory-in-spring/"><span style="color: red;">What is Bean Factory in Spring</span></a></b></li>
<li><b><a href="https://dineshonjava.com/application-context-in-spring/"><span style="color: red;">ApplicationContext in Spring</span></a></b></li>
<li><b><a href="https://dineshonjava.com/bean-autowiring-in-spring/"><span style="color: red;">Bean Autowiring in Spring</span></a></b></li>
<li><b><a href="https://dineshonjava.com/understanding-bean-scopes/"><span style="color: red;">Spring Bean Scopes</span></a></b></li>
<li><b><a href="https://dineshonjava.com/create-custom-bean-scope-in-spring-example/"><span style="color: red;">Create Custom Bean Scope in Spring Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/using-applicationcontextaware-in-spring/"><span style="color: red;">Using ApplicationContextAware in Spring</span></a></b></li>
<li><b><a href="https://dineshonjava.com/bean-lifecycle-and-callbacks/"><span style="color: red;">Spring Bean Life Cycle and Callbacks</span></a></b></li>
<li><b><a href="https://dineshonjava.com/writing-beanpostprocessor-in-spring/"><span style="color: red;">BeanPostProcessor in Spring</span></a></b></li>
<li><b><a href="https://dineshonjava.com/writing-beanfactorypostprocessor-in/"><span style="color: red;">BeanFactoryPostProcessor in Spring</span></a></b></li>
<li><b><a href="https://dineshonjava.com/annotations-in-spring-and-based/"><span style="color: red;"> Annotations in Spring and Based Configuration</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-jsr-250-annotations/"><span style="color: red;">Spring JSR-250 Annotations</span></a></b></li>
<li><b><a href="https://dineshonjava.com/jsr-330-annotations-in-spring/"><span style="color: red;">JSR 330 Annotations in Spring</span></a></b></li>
<li><b><a href="https://dineshonjava.com/stereotype-annotations-in-spring/"><span style="color: red;">Spring @Component, @Repository, @Service and @Controller Stereotype Annotations</span></a></b></li>
<li><b><a href="https://dineshonjava.com/method-injection-with-spring-using-lookup-method-property/"><span style="color: red;">Method injection with Spring using Lookup method property</span></a></b></li>
<li><b><a href="https://dineshonjava.com/introduction-to-aop-in-spring/"><span style="color: red;">Spring AOP-Introduction to Aspect Oriented Programming</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aspect-annotation/"><span style="color: red;">@Aspect Annotation in Spring</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-aspectj-before-annotation-advice-example/"><span style="color: red;"> Spring AOP AspectJ @Before Annotation Advice Example<br />
</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-before-advice-example-using-xml-config/"><span style="color: red;"> Spring AOP Before Advice Example using XML Config</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-aspectj-after-annotation-advice-example/"><span style="color: red;"> Spring AOP AspectJ @After Annotation Advice Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-after-advice-example-using-xml-config/"><span style="color: red;"> Spring AOP After Advice Example using XML Config</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-aspectj-after-returning-annotation-advice-example/"><span style="color: red;"> Spring AOP AspectJ @AfterReturning Annotation Advice Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-after-returning-advice-example-using-xml-config/"><span style="color: red;"> Spring AOP After-Returning Advice Example using XML Config</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-aspectj-after-throwing-annotation-advice-example/"><span style="color: red;"> Spring AOP AspectJ @AfterThrowing Annotation Advice Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-after-throwing-advice-example-using-xml-config/"><span style="color: red;"> Spring AOP After Throwing Advice Example using XML Config</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-aspectj-around-annotation-advice-example/"><span style="color: red;"> Spring AOP AspectJ @Around Annotation Advice Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-around-advice-example-using-xml-config/"><span style="color: red;"> Spring AOP Around Advice Example using XML Config</span></a></b></li>
<li><b><a href="https://dineshonjava.com/understanding-aop-proxies-chapter-31/"><span style="color: red;">Spring AOP Proxies in Spring</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-transaction-management-in/"><span style="color: red;"> Spring AOP Transaction Management in Hibernate</span></a></b></li>
<li><b><a href="https://dineshonjava.com/transaction-management-in-spring/"><span style="color: red;">Spring Transaction Management</span></a></b></li>
<li><b><a href="https://dineshonjava.com/declarative-transaction-management/"><span style="color: red;">Spring Declarative Transaction Management Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-ordering-of-aspects-with-example/"><span style="color: red;">Spring AOP-Ordering of Aspects with Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-security-java-based-configuration-with-example/"><span style="color: red;">Spring Security Java Based Configuration with Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-security-xml-namespace-configuration-example/"><span style="color: red;">Spring Security XML Namespace Configuration Example</span></a></b></li>
</ul>
<p> ;</p>
</div>
</div>
<p> ;</p>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/injecting-collection-in-spring/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/spring-autowiring-by-name/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '673'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…