{"id":941,"date":"2022-07-23T05:49:49","date_gmt":"2023-05-08T23:56:06","guid":{"rendered":"https:\/\/www.silicloud.com\/blog\/uncategorized\/spring-component-annotation\/"},"modified":"2024-03-06T14:40:29","modified_gmt":"2024-03-06T14:40:29","slug":"spring-component-annotation","status":"publish","type":"post","link":"https:\/\/www.silicloud.com\/blog\/spring-component-annotation\/","title":{"rendered":"Spring Component annotation"},"content":{"rendered":"<p>The Spring Component annotation designates a class as a Component. This implies that the Spring framework will automatically detect these classes for dependency injection when utilizing annotation-based configuration and classpath scanning.<\/p>\n<h2>Spring Component -&gt; Component in Spring framework.<\/h2>\n<p>Simply put, a Component is accountable for certain tasks. The Spring framework offers three distinct annotations that can be employed to designate a class as a Component.<\/p>\n<ol>\n<li style=\"list-style-type: none;\">\n<ol>Service: Indicates that the class offers certain services. Our utility classes can be designated as Service classes.<\/ol>\n<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<ol>\n<li style=\"list-style-type: none;\">\n<ol>\n<li style=\"list-style-type: none;\">\n<ol>\n<li style=\"list-style-type: none;\">\n<ol>Repository: This annotation signifies that the class manages CRUD operations, typically used with<\/ol>\n<\/li>\n<\/ol>\n<p><a href=\"https:\/\/decentraland.org\/dao\/\">DAO<\/a><\/li>\n<\/ol>\n<ol>implementations handling database tables.<\/ol>\n<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<ol>Controller: Primarily employed in web applications or REST web services to declare that the class serves as a front controller, responsible for managing user requests and providing suitable responses.<\/ol>\n<p>Please keep in mind that all of these four annotations are present in the org.springframework.stereotype package and are included in the spring-context jar. In most cases, our component classes will belong to one of these three specific annotations, which means that the @Component annotation may not be used frequently.<\/p>\n<h2>An instance of a Spring component as an example.<\/h2>\n<p>We can create a basic Maven application using Spring framework to highlight the usage of Spring Component annotation and how Spring automatically detects it through annotation-based configuration and classpath scanning. To begin, generate a Maven project and include the specified spring core dependency accordingly.<\/p>\n<pre class=\"post-pre\"><code>&lt;dependency&gt;\r\n\t&lt;groupId&gt;org.springframework&lt;\/groupId&gt;\r\n\t&lt;artifactId&gt;spring-context&lt;\/artifactId&gt;\r\n\t&lt;version&gt;5.0.6.RELEASE&lt;\/version&gt;\r\n&lt;\/dependency&gt;\r\n<\/code><\/pre>\n<p>To acquire the core features of the spring framework, all we require is to create a basic component class and designate it with the @Component annotation.<\/p>\n<pre class=\"post-pre\"><code>package com.scdev.spring;\r\n\r\nimport org.springframework.stereotype.Component;\r\n\r\n@Component\r\npublic class MathComponent {\r\n\r\n\tpublic int add(int x, int y) {\r\n\t\treturn x + y;\r\n\t}\r\n}\r\n<\/code><\/pre>\n<p>We are now able to establish a spring context based on annotations and retrieve the MathComponent bean from it.<\/p>\n<pre class=\"post-pre\"><code>package com.scdev.spring;\r\n\r\nimport org.springframework.context.annotation.AnnotationConfigApplicationContext;\r\n\r\npublic class SpringMainClass {\r\n\r\n\tpublic static void main(String[] args) {\r\n\t\tAnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();\r\n\t\tcontext.scan(\"com.scdev.spring\");\r\n\t\tcontext.refresh();\r\n\r\n\t\tMathComponent ms = context.getBean(MathComponent.class);\r\n\r\n\t\tint result = ms.add(1, 2);\r\n\t\tSystem.out.println(\"Addition of 1 and 2 = \" + result);\r\n\r\n\t\tcontext.close();\r\n\t}\r\n\r\n}\r\n<\/code><\/pre>\n<p>Simply execute the aforementioned class as a regular java application and you will receive the subsequent output in the console.<\/p>\n<pre class=\"post-pre\"><code>Jun 05, 2018 12:49:26 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh\r\nINFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@ff5b51f: startup date [Tue Jun 05 12:49:26 IST 2018]; root of context hierarchy\r\nAddition of 1 and 2 = 3\r\nJun 05, 2018 12:49:26 PM org.springframework.context.support.AbstractApplicationContext doClose\r\nINFO: Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@ff5b51f: startup date [Tue Jun 05 12:49:26 IST 2018]; root of context hierarchy\r\n<\/code><\/pre>\n<div><img decoding=\"async\" class=\"post-images\" title=\"\" src=\"https:\/\/cdn.silicloud.com\/blog-img\/blog\/img\/655cf4bfc40ba52feef2c979\/14-0.png\" alt=\"spring component example\" \/><\/div>\n<pre class=\"post-pre\"><code>@Component(\"mc\")\r\npublic class MathComponent {\r\n}\r\n<\/code><\/pre>\n<pre class=\"post-pre\"><code>MathComponent ms = (MathComponent) context.getBean(\"mc\");\r\n<\/code><\/pre>\n<p>Even though I applied the @Component annotation to MathComponent, it should actually be labeled as a service class with the @Service annotation. The outcome will remain unchanged.<\/p>\n<p>You have the option to retrieve the project from our GitHub Repository.<\/p>\n<p>Source: API Documentation<\/p>\n<p>Paraphrased: Documentation on Application Programming Interface<\/p>\n<p>&nbsp;<\/p>\n<p>more tutorials<\/p>\n<p><a class=\"LinkSuggestion__Link-sc-1gewdgc-4 cLBplk\" href=\"https:\/\/www.silicloud.com\/blog\/how-to-integrate-playwright-for-react-application-component-testing\/\" target=\"_blank\" rel=\"noopener\">React Application Component Testing Integrate with Playwright<span class=\"sc-gswNZR eASTkv\">(Opens in a new browser tab)<\/span><\/a><\/p>\n<p><a class=\"LinkSuggestion__Link-sc-1gewdgc-4 cLBplk\" href=\"https:\/\/www.silicloud.com\/blog\/spring-mvc-handlerinterceptoradapter-and-handlerinterceptor\/\" target=\"_blank\" rel=\"noopener\">Spring MVC HandlerInterceptorAdapter and HandlerInterceptor.<span class=\"sc-gswNZR eASTkv\">(Opens in a new browser tab)<\/span><\/a><\/p>\n<p><a class=\"LinkSuggestion__Link-sc-1gewdgc-4 cLBplk\" href=\"https:\/\/www.silicloud.com\/blog\/spring-boot-cli\/\" target=\"_blank\" rel=\"noopener\">Spring Boot CLI<span class=\"sc-gswNZR eASTkv\">(Opens in a new browser tab)<\/span><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Spring Component annotation designates a class as a Component. This implies that the Spring framework will automatically detect these classes for dependency injection when utilizing annotation-based configuration and classpath scanning. Spring Component -&gt; Component in Spring framework. Simply put, a Component is accountable for certain tasks. The Spring framework offers three distinct annotations that [&hellip;]<\/p>\n","protected":false},"author":11,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-941","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v21.5 (Yoast SEO v21.5) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Spring Component annotation - Blog - Silicon Cloud<\/title>\n<meta name=\"description\" content=\"The Spring Component annotation designates a class as a Component. This implies that the Spring framework will automatically detect\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.silicloud.com\/blog\/spring-component-annotation\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Spring Component annotation\" \/>\n<meta property=\"og:description\" content=\"The Spring Component annotation designates a class as a Component. This implies that the Spring framework will automatically detect\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.silicloud.com\/blog\/spring-component-annotation\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog - Silicon Cloud\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-05-08T23:56:06+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-03-06T14:40:29+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/cdn.silicloud.com\/blog-img\/blog\/img\/655cf4bfc40ba52feef2c979\/14-0.png\" \/>\n<meta name=\"author\" content=\"Olivia Parker\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:site\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Olivia Parker\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/spring-component-annotation\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/spring-component-annotation\/\"},\"author\":{\"name\":\"Olivia Parker\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/3ff7b3da0e45ac5dbbef2502f3cea8d9\"},\"headline\":\"Spring Component annotation\",\"datePublished\":\"2023-05-08T23:56:06+00:00\",\"dateModified\":\"2024-03-06T14:40:29+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/spring-component-annotation\/\"},\"wordCount\":388,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/spring-component-annotation\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/spring-component-annotation\/\",\"name\":\"Spring Component annotation - Blog - Silicon Cloud\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\"},\"datePublished\":\"2023-05-08T23:56:06+00:00\",\"dateModified\":\"2024-03-06T14:40:29+00:00\",\"description\":\"The Spring Component annotation designates a class as a Component. This implies that the Spring framework will automatically detect\",\"breadcrumb\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/spring-component-annotation\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.silicloud.com\/blog\/spring-component-annotation\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/spring-component-annotation\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.silicloud.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Spring Component annotation\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"name\":\"Silicon Cloud Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\",\"name\":\"Silicon Cloud Blog\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"contentUrl\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"width\":1024,\"height\":1024,\"caption\":\"Silicon Cloud Blog\"},\"image\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\",\"https:\/\/twitter.com\/SiliCloudGlobal\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/3ff7b3da0e45ac5dbbef2502f3cea8d9\",\"name\":\"Olivia Parker\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/56c66f189ba32a6f9eb50f31a38fe774e2a725c213d4070835ccc51b8fbbc54b?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/56c66f189ba32a6f9eb50f31a38fe774e2a725c213d4070835ccc51b8fbbc54b?s=96&d=mm&r=g\",\"caption\":\"Olivia Parker\"},\"url\":\"https:\/\/www.silicloud.com\/blog\/author\/oliviaparker\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Spring Component annotation - Blog - Silicon Cloud","description":"The Spring Component annotation designates a class as a Component. This implies that the Spring framework will automatically detect","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.silicloud.com\/blog\/spring-component-annotation\/","og_locale":"en_US","og_type":"article","og_title":"Spring Component annotation","og_description":"The Spring Component annotation designates a class as a Component. This implies that the Spring framework will automatically detect","og_url":"https:\/\/www.silicloud.com\/blog\/spring-component-annotation\/","og_site_name":"Blog - Silicon Cloud","article_publisher":"https:\/\/www.facebook.com\/SiliCloudGlobal\/","article_published_time":"2023-05-08T23:56:06+00:00","article_modified_time":"2024-03-06T14:40:29+00:00","og_image":[{"url":"https:\/\/cdn.silicloud.com\/blog-img\/blog\/img\/655cf4bfc40ba52feef2c979\/14-0.png"}],"author":"Olivia Parker","twitter_card":"summary_large_image","twitter_creator":"@SiliCloudGlobal","twitter_site":"@SiliCloudGlobal","twitter_misc":{"Written by":"Olivia Parker","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.silicloud.com\/blog\/spring-component-annotation\/#article","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/spring-component-annotation\/"},"author":{"name":"Olivia Parker","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/3ff7b3da0e45ac5dbbef2502f3cea8d9"},"headline":"Spring Component annotation","datePublished":"2023-05-08T23:56:06+00:00","dateModified":"2024-03-06T14:40:29+00:00","mainEntityOfPage":{"@id":"https:\/\/www.silicloud.com\/blog\/spring-component-annotation\/"},"wordCount":388,"commentCount":0,"publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.silicloud.com\/blog\/spring-component-annotation\/","url":"https:\/\/www.silicloud.com\/blog\/spring-component-annotation\/","name":"Spring Component annotation - Blog - Silicon Cloud","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/#website"},"datePublished":"2023-05-08T23:56:06+00:00","dateModified":"2024-03-06T14:40:29+00:00","description":"The Spring Component annotation designates a class as a Component. This implies that the Spring framework will automatically detect","breadcrumb":{"@id":"https:\/\/www.silicloud.com\/blog\/spring-component-annotation\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.silicloud.com\/blog\/spring-component-annotation\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.silicloud.com\/blog\/spring-component-annotation\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.silicloud.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Spring Component annotation"}]},{"@type":"WebSite","@id":"https:\/\/www.silicloud.com\/blog\/#website","url":"https:\/\/www.silicloud.com\/blog\/","name":"Silicon Cloud Blog","description":"","publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.silicloud.com\/blog\/#organization","name":"Silicon Cloud Blog","url":"https:\/\/www.silicloud.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","contentUrl":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","width":1024,"height":1024,"caption":"Silicon Cloud Blog"},"image":{"@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/SiliCloudGlobal\/","https:\/\/twitter.com\/SiliCloudGlobal"]},{"@type":"Person","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/3ff7b3da0e45ac5dbbef2502f3cea8d9","name":"Olivia Parker","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/56c66f189ba32a6f9eb50f31a38fe774e2a725c213d4070835ccc51b8fbbc54b?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/56c66f189ba32a6f9eb50f31a38fe774e2a725c213d4070835ccc51b8fbbc54b?s=96&d=mm&r=g","caption":"Olivia Parker"},"url":"https:\/\/www.silicloud.com\/blog\/author\/oliviaparker\/"}]}},"_links":{"self":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/941","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/users\/11"}],"replies":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/comments?post=941"}],"version-history":[{"count":2,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/941\/revisions"}],"predecessor-version":[{"id":1689,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/941\/revisions\/1689"}],"wp:attachment":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/media?parent=941"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/categories?post=941"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/tags?post=941"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}