<div dir="ltr" style="text-align: justify;" trbidi="on">
<div dir="ltr" style="text-align: justify;" trbidi="on">
<div dir="ltr" style="text-align: justify;" trbidi="on">
<div style="color: #274e13;">You can use <b>@Resource</b> annotation on fields or setter methods and it works the same as in Java EE 5. The @Resource annotation takes a &#8216;<b>name</b>&#8216; attribute which will be interpreted as the bean name to be injected. You can say, it follows <b>by-name</b> autowiring semantics as demonstrated in the below example:</p>
</div>
<div align='center' id="ads-id"></div>
<p><b>Circle.java</b></p>
<pre class="highlight" name="code">package com.dineshonjava.sdnext.jsr.tutorial;

import javax.annotation.Resource;

public class Circle
{ 
 private Point center;
 
 @Resource(name="pointB")
 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="background-color: #f2f9fc; border:1px solid #c9e6f2;border-radius:3px;padding:16px;line-height:1.45;"><b>Popular Tutorials</b></p>
<ul style="text-align: left;">
<li><b><a href="https://dineshonjava.com/2012/06/spring-30-baby-step-to-learn.html"><em><strong>Spring Tutorial</strong> </em></a></b></li>
<li><b><a href="https://dineshonjava.com/2012/12/spring-web-mvc-framework-chapter-38.html"><strong><em>Spring MVC Web Tutorial </em></strong></a></b></li>
<li><b><a href="https://dineshonjava.com/2016/06/introduction-to-spring-boot-a-spring-boot-complete-guide.html"><em><strong>Spring Boot Tutorial</strong> </em></a></b></li>
<li><b><a href="https://dineshonjava.com/2013/02/spring-security-take-baby-step-to-secure.html"><em>Spring Security Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/2017/03/spring-aop-tutorial-with-example-aspect-advice-pointcut-joinpoint.html"><em>Spring AOP Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/2012/12/using-spring-jdbc-framework-chapter-32.html"><em>Spring JDBC Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/2017/01/spring-hateoas-hypermedia-driven-restful-web-service.html"><em><strong>Spring HATEOAS </strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/2017/01/microservices-with-spring-boot.html"><em><strong>Microservices with Spring Boot</strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/2013/06/jax-rs-web-service-tutorial.html"><strong><em>REST Webservice</em> </strong></a></b></li>
<li><b><a href="https://dineshonjava.com/2013/01/core-java-baby-step-to-be-best-java-ian.html"><em><strong>Core Java </strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/2012/03/hibernate-3-on-baby-steps.html"><em><strong>Hibernate Tutorial</strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/2012/12/spring-batch-process-with-example.html"><strong><em>Spring Batch</em> </strong></a></b></li>
</ul>
</div>
<p></p>
<div style="color: #274e13;">If no <b>&#8220;name&#8221;</b> is specified explicitly, the default name is derived from the field name or setter method. In case of a field, it takes the field name; in case of a setter method, it takes the bean property name.</p>
</div>
<p><b>Point.java</b></p>
<pre class="highlight" name="code">package com.dineshonjava.sdnext.jsr.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><br />
</b> <b>DrawingApp.java</b></p>
<pre class="highlight" name="code">package com.dineshonjava.sdnext.jsr.tutorial;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author Dinesh
 *
 */
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" name="code"><;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:></p>
<div style="color: #274e13;">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:</p>
</div>
<div style="background-color: #ff99cc; border-width: thin; border: solid;">
<div style="color: blue;"><b>Output:</b></div>
<div style="color: red;">Jul 14, 2012 12:40:59 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh</div>
<div style="color: red;">INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@ab50cd: startup date [Sat Jul 14 00:40:59 IST 2012]; root of context hierarchy</div>
<div style="color: red;">Jul 14, 2012 12:40:59 AM 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 14, 2012 12:41:00 AM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons</div>
<div style="color: red;">INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@bd928a: 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>
<p><b>Circle is drawn of center (-20, 0)</b></div>
</div>
<p></p>
<div style="background-color: #f2f9fc; border-radius: 3px; border: 1px solid #c9e6f2; line-height: 1.45; padding: 16px;"><b>Spring Related Topics you may like</b></p>
<ol style="text-align: left;">
<li><b><a href="https://dineshonjava.com/2012/10/interview-questions-on-spring.html">Spring Interview Questions and Answers</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/03/spring-aop-interview-questions-and-answers.html">Spring AOP Interview Questions and Answers</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/02/spring-mvc-interview-questions-and-answers.html">Spring MVC Interview Questions</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/02/spring-security-interview-questions-and-answers.html">Spring Security Interview Questions and Answers</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/03/spring-rest-web-services-interview-questions-and-answers.html">Spring REST Interview Questions and Answers</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/04/spring-boot-interview-questions-and-answers.html">Spring Boot Interview Questions and Answers</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/04/spring-boot-microservices-interview-questions-answers.html">Spring Boot Microservices Interview Questions and Answers</a></b></li>
<li><b><a href="https://dineshonjava.com/2012/06/dependency-injection-in-spring.html">Dependency Injection (DI) in Spring </a></b></li>
<li><b><a href="https://dineshonjava.com/2012/06/spring-ioc-container.html">Spring IoC Container</a></b></li>
<li><b><a href="https://dineshonjava.com/2012/06/what-is-bean-factory-in-spring.html">What is Bean Factory in Spring</a></b></li>
<li><b><a href="https://dineshonjava.com/2012/06/application-context-in-spring.html">ApplicationContext in Spring</a></b></li>
<li><b><a href="https://dineshonjava.com/2012/06/bean-autowiring-in-spring.html">Bean Autowiring in Spring</a></b></li>
<li><b><a href="https://dineshonjava.com/2012/06/understanding-bean-scopes.html">Spring Bean Scopes</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/03/create-custom-bean-scope-in-spring-example.html">Create Custom Bean Scope in Spring Example</a></b></li>
<li><b><a href="https://dineshonjava.com/2012/06/using-applicationcontextaware-in-spring.html">Using ApplicationContextAware in Spring</a></b></li>
<li><b><a href="https://dineshonjava.com/2012/07/bean-lifecycle-and-callbacks.html">Spring Bean Life Cycle and Callbacks</a></b></li>
<li><b><a href="https://dineshonjava.com/2012/07/writing-beanpostprocessor-in-spring.html">BeanPostProcessor in Spring</a></b></li>
<li><b><a href="https://dineshonjava.com/2012/07/writing-beanfactorypostprocessor-in.html">BeanFactoryPostProcessor in Spring</a></b></li>
<li><b><a href="https://dineshonjava.com/2012/07/annotations-in-spring-and-based.html"> Annotations in Spring and Based Configuration</a></b></li>
<li><b><a href="https://dineshonjava.com/2012/07/spring-jsr-250-annotations.html">Spring JSR-250 Annotations</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/01/jsr-330-annotations-in-spring.html">JSR 330 Annotations in Spring</a></b></li>
<li><b><a href="https://dineshonjava.com/2012/07/stereotype-annotations-in-spring.html">Spring @Component, @Repository, @Service and @Controller Stereotype Annotations</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/01/method-injection-with-spring-using-lookup-method-property.html">Method injection with Spring using Lookup method property</a></b></li>
<li><b><a href="https://dineshonjava.com/2012/07/introduction-to-aop-in-spring.html">Spring AOP-Introduction to Aspect Oriented Programming</a></b></li>
<li><b><a href="https://dineshonjava.com/2012/07/spring-aspect-annotation.html">@Aspect Annotation in Spring</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/03/spring-aop-aspectj-before-annotation-advice-example.html"> Spring AOP AspectJ @Before Annotation Advice Example<br />
</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/03/spring-aop-before-advice-example-using-xml-config.html"> Spring AOP Before Advice Example using XML Config</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/03/spring-aop-aspectj-after-annotation-advice-example.html"> Spring AOP AspectJ @After Annotation Advice Example</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/03/spring-aop-after-advice-example-using-xml-config.html"> Spring AOP After Advice Example using XML Config</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/03/spring-aop-aspectj-after-returning-annotation-advice-example.html"> Spring AOP AspectJ @AfterReturning Annotation Advice Example</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/03/spring-aop-after-returning-advice-example-using-xml-config.html"> Spring AOP After-Returning Advice Example using XML Config</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/03/spring-aop-aspectj-after-throwing-annotation-advice-example.html"> Spring AOP AspectJ @AfterThrowing Annotation Advice Example</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/03/spring-aop-after-throwing-advice-example-using-xml-config.html"> Spring AOP After Throwing Advice Example using XML Config</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/03/spring-aop-aspectj-around-annotation-advice-example.html"> Spring AOP AspectJ @Around Annotation Advice Example</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/03/spring-aop-around-advice-example-using-xml-config.html"> Spring AOP Around Advice Example using XML Config</a></b></li>
<li><b><a href="https://dineshonjava.com/2012/12/understanding-aop-proxies-chapter-31.html">Spring AOP Proxies in Spring</a></b></li>
<li><b><a href="https://dineshonjava.com/2012/12/spring-aop-transaction-management-in.html"> Spring AOP Transaction Management in Hibernate</a></b></li>
<li><b><a href="https://dineshonjava.com/2012/12/transaction-management-in-spring.html">Spring Transaction Management</a></b></li>
<li><b><a href="https://dineshonjava.com/2012/12/declarative-transaction-management.html">Spring Declarative Transaction Management Example</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/03/spring-aop-ordering-of-aspects-with-example.html">Spring AOP-Ordering of Aspects with Example</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/02/spring-security-java-based-configuration-with-example.html">Spring Security Java Based Configuration with Example</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/02/spring-security-xml-namespace-configuration-example.html">Spring Security XML Namespace Configuration Example</a></b></li>
</ol>
</div>
<div style="text-align: center;"><b><a href="https://dineshonjava.com/2012/07/annotations-in-spring-and-based.html"><;<; </a></b><a href="https://dineshonjava.com/2012/07/annotations-in-spring-and-based.html"><b></b></a><b><a href="https://dineshonjava.com/2012/07/spring-jsr-250-annotations.html">Some JSR-250 Annotations</a></b><b> |<a href="https://dineshonjava.com/2012/06/spring-30-baby-step-to-learn.html">index</a>| <a href="https://dineshonjava.com/2012/07/spring-postconstruct-predestroy.html">Spring @PostConstruct &;@PreDestroy Annotation>;>;</a></b></p>
<p>
</div>
</div>
<p>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/stereotype-annotations-in-spring/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/spring-postconstruct-predestroy/">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: '652'});
});
</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…