<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 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 dir="ltr" style="text-align: justify;" trbidi="on">
<div dir="ltr" style="text-align: justify;" trbidi="on">
<div style="color: #274e13;">In this tutorial we would discuss about the <b>Stereotype Annotations</b> in Spring. Spring @Component, @Repository, @Service and @Controller are Stereotype Annotations. @Component is generic stereotype annotation for any Spring-managed component. In the previous version <b>Spring 2.0</b> introduce the first <b>Stereotype Annotations </b>name as <i><b>@Repository.</b></i> The <a href="https://dineshonjava.com/spring-component-annotation/"><b>@Component annotations</b></a> introduced in <b>Spring 2.5 </b>are really just a continuation of the <b>&#8220;stereotype&#8221;</b> annotations introduced in <b>Spring 2.0</b>. <b>Stereotype annotations</b> are markers for any class that fulfills a role within an application. This helps remove, or at least greatly reduce, the <b>Spring XML</b> configuration required for these components.</div>
<div align="center" id="ads-id"></div>
<div style="color: #274e13;">
<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>
<p>
</div>
<div class="separator" style="clear: both; text-align: center;"><a href="https://certification-questions.com/buy-mock-exams.html?affiliateCode=4f5a35d8-0022-4c9c-b8d3-265ed0b825a2&#038;utm_source=Dineshh&#038;utm_medium=affiliate&#038;utm_campaign=affiliate" target="_blank"><img border="0" src="https://dineshonjava.com/wp-content/uploads/2012/07/spring-certification-dumps.png" /></a></div>
<p>These annotations are used to stereotype classes with regard to the application tier that they belong to. Classes that are annotated with one of these annotations will automatically be registered in the Spring application context if <b style="color: #274e13;"><;context:component-scan>;</b> is in the <b style="color: #274e13;">Spring XML configuration(spring.xml). </b></div>
<p></p>
<div style="color: #274e13;"><b>The Four Types of Spring Stereotype Components and Their Purposes:</b></div>
<pre class="lang-java prettyprint">+------------+-----------------------------------------------------+</pre>
<pre class="lang-java prettyprint"><b style="color: blue;">| </b><b style="color: blue;">Annotation | Meaning  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ; |</b> 
+------------+-----------------------------------------------------+ 
| <a href="https://dineshonjava.com/spring-component-annotation/"><b style="color: #0c343d;">@Component</b></a> | <b>generic stereotype </b><b>for any Spring-</b><b>managed component</b> | 
| <b style="color: #0c343d;">@Repository</b>|<b style="color: #274e13;"> stereotype for</b><b style="color: #274e13;"> persistence layer</b>  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;| 
| <b>@Service  ; </b>| <b style="color: #274e13;">stereotype </b><b style="color: #274e13;">for</b><b style="color: #274e13;"> service layer</b>  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;| 
| <b style="color: #0c343d;">@Controller|</b> <b style="color: #274e13;">stereotype </b><b style="color: #274e13;">for presentation layer (spring-mvc)</b>  ;  ;  ;| 
</pre>
<pre class="lang-java prettyprint">+------------+-----------------------------------------------------+</pre>
</div>
<div style="color: #274e13;"><b><a href="https://dineshonjava.com/spring-component-annotation/">Annotation @Component</a>:</b></div>
<h3 style="color: #274e13; text-align: left;"><b>Target:</b></h3>
<div style="color: #274e13;"><b> ; ; ; ; ; ; ; ; ; Class</b></div>
<div style="color: #274e13;"><b>Description:</b></div>
</div>
<p></p>
<div style="color: #274e13;"><a href="https://dineshonjava.com/spring-component-annotation/"><b>@Component</b></a> is a generic stereotype for any Spring-managed component.<b> @Repository, @Service, and @Controller</b> are specializations of <a href="https://dineshonjava.com/spring-component-annotation/"><b>@Component</b></a> for more specific use cases, for example, in the persistence, service, and presentation layers, respectively.</p>
<div class="separator" style="clear: both; text-align: center;"><img border="0" src="https://dineshonjava.com/wp-content/uploads/2012/07/SpringComponentAnnotations-1.jpg" title="Stereotype Annotations in Spring" /></div>
<p></p>
<div class="separator" style="clear: both; text-align: center;"></div>
</div>
<p></p>
<div class="separator" style="clear: both; text-align: center;"></div>
<pre class="highlight" name="code">@Component 
public class Circle 
{ 
 private Point center; 
 ---- 
} 
</pre>
<div style="color: #274e13;"><a href="https://dineshonjava.com/spring-component-annotation/"><u><b>See the full Example for Component annotation:</b></u></a></div>
<p></p>
<div style="color: #274e13;"><b>Annotation @Repository:</b></div>
<h3 style="color: #274e13; text-align: left;"><b>Target:</b></h3>
<div style="color: #274e13;"><b> ; ; ; ; ; ; ; ; ; Class</b></div>
<div style="color: #274e13;"><b>Description:</b><br />
In <b>Spring 2.0</b> and later, the <i><b>@Repository</b></i> annotation is a marker for any class that fulfills the role or stereotype (<b>also known as Data Access Object or DAO</b>) of a repository. Among the uses of this marker is the automatic translation of exceptions. <br />
A class that serves in the persistence layer of the application as a data access object (<b>DAO</b>), otherwise known as a repository in some other technologies. Annotate all your <b>DAO </b>classes with <i><b>@Repository</b></i>. All your database access logic should be in <b>DAO </b>classes. </div>
</div>
<pre class="highlight" name="code">@Repository 
public class CircleDaoImpl implements CircleDao 
{ 
 private Point center; 
 ---- 
} 
</pre>
</div>
<p></p>
<div style="color: #274e13;"><b>Annotation @Service:</b></div>
<h3 style="color: #274e13; text-align: left;"><b>Target:</b></h3>
<div style="color: #274e13;"><b> ; ; ; ; ; ; ; ; ; Class</b></div>
<div style="color: #274e13;"><b>Description:</b><br />
Annotate all your service classes with <i><b>@Service</b></i>. All your business logic should be in Service classes.</div>
<pre class="highlight" name="code">@Service 
public class CircleServiceImpl implements CircleService 
{ 
 private Point center; 
 ---- 
} 
</pre>
<p></p>
<div style="color: #274e13;"><b>Annotation @Controller:</b></div>
<h3 style="color: #274e13; text-align: left;"><b>Target:</b></h3>
<div style="color: #274e13;"><b> ; ; ; ; ; ; ; ; ; Class</b></div>
<div style="color: #274e13;"><b>Description:</b><br />
The <i><b>@Controller</b></i> is a class level annotation, which indicates that the annotated class is a Spring component of type &#8220;<b>controller</b>&#8220;.<br />
The <i><b>@Controller</b></i> annotation indicates that a particular class serves the role of a controller. Spring does not require you to extend any controller base class or reference the <b>Servlet API</b>. However, you can still reference Servlet-specific features if you need to. In Spring MVC you can make controller class very easily by prefixing <i><b>@Controller</b></i> before any class declaration.</div>
<pre class="highlight" name="code">@Controller 
public class CircleController 
{ 
 private Point center; 
 ---- 
} 
</pre>
<p>
<b>Enable component scanning</b><br />
Spring by default does not scan means Spring container does create bean for those classes whose annotated with above for stereotype annotations. So we have to enable component scanning explicity by using &#8220;context:component-scan&#8221; tag in your applicationContext.xml file. So stereotype annotations will be scanned and configured only when they are scanned by DI container of spring framework. </p>
<pre class="highlight" name="code"><;context:component-scan base-package="com.dineshonjava.app.service" />; 
<;context:component-scan base-package="com.dineshonjava.app.dao" />; 
<;context:component-scan base-package="com.dineshonjava.app.controller" />; 
</pre>
<p>
The context:component-scan element requires a base-package attribute, the value of base-package attribute should specifies a starting point for a recursive component search. Spring recommends do not use your top package for scanning, so you should declare specific component-scan elements.</p>
<p><b>Note: </b>If you are using component-scan property for context namespace then you no longer need to declare context:annotation-config, because autowiring is implicitly enabled when component scanning is enabled.</p>
<p><b>Where to use stereotype annotations?</b><br />
Always use these annotations over concrete classes; not over interfaces.</p>
<ul>
<li><b>@Controller </b>annotation is for a class as a Spring Web MVC controller. It is a meta annotation of @Component, so beans annotated with it are automatically imported into the Spring container. If you add the @Controller annotation to a class then you can use handler mappling annotation i.e. @RequestMapping; to map URLs to instance methods of a class.</li>
<li><b>@Service </b>annotation is for a class as a Service of application.</li>
<li><b>@Repository</b> annotation is more suitable annotation that provides additional benefits specifically for DAOs. The @Repository annotation is a meta annotation of the @Component annotation with similar use and functionality. In addition to importing the DAOs into the DI container, it also makes the unchecked exceptions eligible for translation into Spring DataAccessException.</li>
<li><b>@Component</b> should be used when your class does not fall into either of three categories i.e. <b><i>Controllers</i></b>, <b><i>Services </i></b>and <i><b>DAOs</b></i>.</li>
</ul>
</div>
<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>
<ol style="text-align: left;"><span style="color: #274e13;"></p>
<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>
<p></span></ol>
</div>
<p></p>
<div style="text-align: center;"><b><;<; </b><b><a href="https://dineshonjava.com/spring-jsr-250-annotations/">Some JSR 250 Annotation</a></b><b> |<a href="https://dineshonjava.com/spring-tutorial/">index</a>| <a href="https://dineshonjava.com/using-property-files-in-spring/">Using Property File in Spring</a>>;>;</b></div>
</div>
<p>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/spring-aspect-annotation/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/spring-resource-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: '653'});
});
</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…