<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div style="color: #274e13;">In last <a href="https://dineshonjava.com/bean-autowiring-in-spring">Spring Bean Autowiring in Spring in XML</a> example, it will autowired the matched property of any bean in current <a href="https://dineshonjava.com/spring-ioc-container/">Spring container</a>. In most cases, you may need autowired property in a particular bean only.</div>
<div id="ads-id" align="center"></div>
<div style="background-color: #f2f9fc; border: 1px solid #c9e6f2; border-radius: 3px; padding: 16px; line-height: 1.45;"><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>@Autowired</b> annotation</h2>
<p>In Spring, you can use <b>@Autowired</b> annotation to auto wire bean on the <b>setter method, constructor or a field</b>. Moreover, it can autowired property in a particular bean.</p>
<div class="separator" style="clear: both; text-align: center;"><img class="alignnone" src="https://dineshonjava.com/wp-content/uploads/2012/07/Autowired-Annotation-in-Spring.jpg" width="558" height="308" border="0" /></div>
<p><i>@ImageSource-Slideshare.net</i><br />
<b>Note</b>:<br />
The <b style="color: blue;">@Autowired</b> annotation is auto wire the bean by matching <b style="color: blue;"><a href="https://dineshonjava.com/spring-autowiring-by-type/">data type</a> </b>if <b><a href="https://dineshonjava.com/spring-ioc-container/">spring container</a></b> find more than one beans same <a href="https://dineshonjava.com/spring-autowiring-by-type/"><b>data type</b></a> then it find <a href="https://dineshonjava.com/spring-autowiring-by-name/"><b>by name</b></a>.</p>
</div>
<p> ;</p>
<div style="color: #274e13;">
<h3><b>@Autowired Annotation on Setter Method:</b></h3>
<p>You can use <b>@Autowired</b> annotation on setter methods to get <i><b>ref id</b> </i>of the <b><;property>;</b> element in XML configuration file<b>(spring.xml)</b>. When Spring finds an<b> @Autowired </b>annotation used with setter methods, it tries to perform <b><a href="https://dineshonjava.com/spring-autowiring-by-type/">byType autowiring</a></b> on the method.</p>
<p>See following full example to demonstrate the use of <b>@Autowired</b>.</p>
</div>
<p><b>Circle.java</b></p>
<pre class="highlight">package com.dineshonjava.sdnext.autowiredannotation.tutorial; 
 
import org.springframework.beans.factory.annotation.Autowired; 
 
public class Circle 
{ 
 private Point center; 
 //using autowired annotation with setter method 
 @Autowired 
 public void setCenter(Point center) 
 { 
 this.center = center; 
 } 
 
 public void draw() 
 { 
System.out.println("Circle is drawn of center ("+center.getX()+", "+center.getY()+")"); 
 } 
} 
</pre>
<p><b>Point.java</b></p>
<pre class="highlight">package com.dineshonjava.sdnext.autowiredannotation.tutorial; 
 
public class Point 
{ 
 private int x; 
 private int y; 
 /** 
 * @return the x 
 */ 
 public int getX() { 
 return x; 
 } 
 /** 
 * @param x the x to set 
 */ 
 public void setX(int x) { 
 this.x = x; 
 } 
 /** 
 * @return the y 
 */ 
 public int getY() { 
 return y; 
 } 
 /** 
 * @param y the y to set 
 */ 
 public void setY(int y) { 
 this.y = y; 
 } 
} 
</pre>
<p><b>DrawingApp.java</b></p>
<pre class="highlight">package com.dineshonjava.sdnext.autowiredannotation.tutorial; 
 
import org.springframework.context.ApplicationContext; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 
 
/** 
 * @author Dinesh Rajput 
 * 
 */ 
public class DrawingApp 
{ 
 /** 
 * @param args 
 */ 
 public static void main(String[] args) 
 { 
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); 
Circle circle = (Circle) context.getBean("circle"); 
circle.draw(); 
 } 
} 
</pre>
<p><b>spring.xml</b></p>
<pre class="highlight"><;beans xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:> 
Once you are done with creating source and bean configuration files, let us run the application. If everything is fine with your application, this will print the following message: 
<div style="background-color: #ff99cc; border-width: thin; border: solid;"> 
<div style="color: blue;"><b>Output:</b></div> 
<div style="color: red;">Jul 10, 2012 7:41:44 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh</div> 
<div style="color: red;">INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@ab50cd: startup date [Tue Jul 10 19:41:44 IST 2012]; root of context hierarchy</div> 
<div style="color: red;">Jul 10, 2012 7:41:44 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions</div> 
<div style="color: red;">INFO: Loading XML bean definitions from class path resource [spring.xml]</div> 
<div style="color: red;">Jul 10, 2012 7:41:44 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons</div> 
<div style="color: red;">INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1d80e6d: defining beans [circle,pointA,pointB,pointC,center,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0]; root of factory hierarchy</div> 
<b>Circle is drawn of center (10, 10)</b> 
 
</div> 
</div> 
 ; 
<h3 style="color: #274e13;"><b>@Autowired Annotation on Properties:</b></h3> 
<div style="color: #274e13;">You can use <b>@Autowired</b> annotation on <b>properties </b>to get <i><b>ref id </b></i>of the setter methods. When you will pass values of autowired properties using Spring will automatically assign those properties with the passed values or references.</div> 
</div> 
<b>Circle.java</b> 
<pre class="highlight">package com.dineshonjava.sdnext.autowiredannotation.tutorial; 
 
import org.springframework.beans.factory.annotation.Autowired; 
 
public class Circle 
{ 
 //using autowired annotation with property 
 @Autowired 
 private Point center; 
 
 public void setCenter(Point center) 
 { 
 this.center = center; 
 } 
 
 public void draw() 
 { 
System.out.println("Circle is drawn of center ("+center.getX()+", "+center.getY()+")"); 
 } 
} 
</pre>
<div style="color: #274e13;"></div>
<h3 style="color: #274e13;"><b>@Autowired Annotation on Constructors:</b></h3>
<div style="color: #274e13;">You can apply <b>@Autowired</b> to constructors as well. A constructor <b>@Autowired</b> annotation indicates that the <b>constructor </b>should be autowired when creating the bean, even if no <b><;constructor-arg/>;</b> elements are used while configuring the bean in XML file.</div>
</div>
<p><b>Circle.java</b></p>
<pre class="highlight">package com.dineshonjava.sdnext.autowiredannotation.tutorial; 
 
import org.springframework.beans.factory.annotation.Autowired; 
 
public class Circle 
{ 
 private Point center; 
 
 //using autowired annotation with constructor 
 @Autowired 
 public Center(Point center) 
 { 
 this.center = center; 
 } 
 
 public void draw() 
 { 
System.out.println("Circle is drawn of center ("+center.getX()+", "+center.getY()+")"); 
 } 
} 
</pre>
</div>
<h3><b>@Autowired Annotation with (required=false) option: </b></h3>
<div style="color: #274e13;">By default, the @Autowired annotation has the required dependency but we can set the required dependency to false as below:</div>
<p> ;</p>
</div>
<p><b>Circle.java</b></p>
<pre class="highlight">package com.dineshonjava.sdnext.autowiredannotation.tutorial; 
 
import org.springframework.beans.factory.annotation.Autowired; 
 
public class Circle 
{ 
 //using autowired annotation with property 
 @Autowired(required=false) 
 private Point center; 
 
 public void setCenter(Point center) 
 { 
 this.center = center; 
 } 
 
 public void draw() 
 { 
System.out.println("Circle is drawn of center ("+center.getX()+", "+center.getY()+")"); 
 } 
}</pre>
</div>
<p> ;</p>
<div style="background-color: #f2f9fc; border-radius: 3px; border: 1px solid #c9e6f2; line-height: 1.45; padding: 16px;"><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>
<p> ;</p>
</div>
<p> ;</p>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/annotations-in-spring-and-based/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/spring-required-annotation/">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: '658'});
});
</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…