<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;">The <i><b>@Component </b></i>Annotation Indicates that an annotated class is a &#8220;<b>component</b>&#8220;. Such classes are considered as candidates for <b>auto-detection</b> when using<b> annotation-based configuration</b> and classpath scanning. ;</div>
<div align='center' id="ads-id"></div>
<div style="color: #274e13;"><b>The @Component</b> is a generic stereotype for any Spring-managed component.<b> @Repository, @Service, and @Controller</b> are specializations of <b>@Component</b> for more specific use cases, for example, in the persistence, service, and presentation layers, respectively.</div>
<div class="separator" style="clear: both; text-align: center;"><img border="0" src="http://50.87.249.200/~dineshon/wp-content/uploads/2012/07/SpringComponentAnnotations.jpg"/></div>
<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>
<div class="separator" style="clear: both; text-align: center;"></div>
<p><b> ;See the following example using component annotation:</b></p>
<p><b>Triangle.java</b></p>
<pre class="highlight" name="code">package com.dineshonjava.sdnext.componentstreatypeannotation.tutorail;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

@Component
public class Triangle
{
 private Point pointA;
 private Point pointB;
 private Point pointC;
 
 /**
 * Using @Resource annotation for wiring with bean dependency
 * @param pointA the pointA to set
 */
 @Resource
 public void setPointA(Point pointA) {
 this.pointA = pointA;
 }

 /**
 * Using @Resource annotation for wiring with bean dependency
 * @param pointB the pointB to set
 */
 @Resource
 public void setPointB(Point pointB) {
 this.pointB = pointB;
 }

 /**
 * Using @Resource annotation for wiring with bean dependency
 * @param pointC the pointC to set
 */
 @Resource
 public void setPointC(Point pointC) {
 this.pointC = pointC;
 }

 public void draw()
 {
System.out.println("Drawing Triangle");
System.out.println("PointA is ("+pointA.getX()+", "+pointA.getY()+")");
System.out.println("PointB is ("+pointB.getX()+", "+pointB.getY()+")");
System.out.println("PointC is ("+pointC.getX()+", "+pointC.getY()+")");
 }
}
</pre>
<p></p>
<div style="color: #274e13;">Here we are using <i><b>@Component</b></i> annotation with the <b>Triangle </b>class which auto detect as bean in <a href="https://dineshonjava.com/2012/06/spring-ioc-container.html"><u><b>Spring IoC Container</b></u></a>.</div>
<p>
<b>Point.java</b></p>
<pre class="highlight" name="code">package com.dineshonjava.sdnext.componentstreatypeannotation.tutorail;

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" name="code">package com.dineshonjava.sdnext.componentstreatypeannotation.tutorail;

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");
Triangle triangle= (Triangle) context.getBean("triangle");
triangle.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;">Here <b><;context:component-scan/>;</b> scan the all classes in the <b>base package</b> given as "<b>com.dineshonjava.sdnext.componentstreatypeannotation.tutorail</b>" which are annotated with the <i><b>@Component</b></i> annotation or its specific annotations like <i><b>@Component, @Repository, @Service &; @Controller etc</b></i> then these classes detect as bean of <a href="https://dineshonjava.com/2012/06/spring-ioc-container.html"><u><b>Spring IoC Container</b></u></a>.</div>
<div style="color: #274e13;">
</div>
<div style="color: #274e13;"><b>If every thing is OK then run that application we will get the following output.</b></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 8:54:13 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh</div>
<div style="color: red;">INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@ab50cd: startup date [Sat Jul 14 20:54:13 IST 2012]; root of context hierarchy</div>
<div style="color: red;">Jul 14, 2012 8:54:13 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 14, 2012 8:54:14 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons</div>
<div style="color: red;">INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1198891: defining beans [pointA,pointB,pointC,center,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,circle,triangle,org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0]; root of factory hierarchy</div>
<p><b>Drawing Triangle</b><br />
<b>PointA is (0, 0)</b><br />
<b>PointB is (-20, 0)</b><br />
<b>PointC is (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><;<; </b><a href="http://50.87.249.200/~dineshon/2012/07/annotations-in-spring-and-based/"><b></b></a><b><a href="https://dineshonjava.com/2012/07/stereotype-annotations-in-spring.html">Stereotype Annotations</a></b><b> |<a href="https://dineshonjava.com/2012/06/spring-30-baby-step-to-learn.html">index</a>| ;</b><b><a href="https://dineshonjava.com/2012/07/stereotype-annotations-in-spring.html">Spring @Service Annotation</a></b><b>>;>;</b></div>
</div>
<p>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/spring-postconstruct-predestroy/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/using-property-files-in-spring/">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: '650'});
});
</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…