{"id":283,"date":"2025-05-20T13:00:00","date_gmt":"2025-05-20T13:00:00","guid":{"rendered":"https:\/\/springmasteryhub.com\/?p=283"},"modified":"2025-04-16T16:32:58","modified_gmt":"2025-04-16T16:32:58","slug":"what-is-component-and-how-to-use-it-in-spring","status":"publish","type":"post","link":"https:\/\/springmasteryhub.com\/?p=283","title":{"rendered":"What is @Component and How to Use It in Spring"},"content":{"rendered":"\n<h1 class=\"wp-block-heading\">What is <code>@Component<\/code>?<\/h1>\n\n\n\n<p><code>@Component<\/code> is a stereotype annotation from the Spring framework that allows you to mark your classes as Spring components. These classes will be managed by Spring and will be available in the Spring IoC container, so they can be injected into other classes (also managed by Spring) through the <code>@Autowired<\/code> annotation.<\/p>\n\n\n\n<p>Use this annotation when you need to create an application component that does not deal with business logic or when it does not fit into web, service, or repository layers. It&#8217;s a more generic type. If you&#8217;re not sure which stereotype to use, <code>@Component<\/code> is a good choice.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Why use <code>@Component<\/code>?<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li>It marks your class for Spring to auto-detect and register it into the Spring IoC container.<\/li>\n\n\n\n<li>It facilitates component injection and dependency management.<\/li>\n\n\n\n<li>It makes it easier to create more flexible classes due to the inversion of control. You can add properties to your component and manage these properties from an external configuration, allowing you to change values without recompiling and redeploying your application.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">How to use <code>@Component<\/code><\/h3>\n\n\n\n<p>This annotation is simple to use. Define a class with <code>@Component<\/code>:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282A36\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"@Component\npublic class MyComponent {\n    public void doSomething() {\n        System.out.println(&quot;Doing something...&quot;);\n    }\n}\n\n\" style=\"color:#f6f6f4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dracula-soft\" style=\"background-color: #282A36\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F6F6F4\">@<\/span><span style=\"color: #97E1F1; font-style: italic\">Component<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F286C4\">public<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">class<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #97E1F1\">MyComponent<\/span><span style=\"color: #F6F6F4\"> {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    <\/span><span style=\"color: #F286C4\">public<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #97E1F1; font-style: italic\">void<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #62E884\">doSomething<\/span><span style=\"color: #F6F6F4\">() {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">        System.out.<\/span><span style=\"color: #62E884\">println<\/span><span style=\"color: #F6F6F4\">(<\/span><span style=\"color: #DEE492\">&quot;<\/span><span style=\"color: #E7EE98\">Doing something...<\/span><span style=\"color: #DEE492\">&quot;<\/span><span style=\"color: #F6F6F4\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p>Now your bean is already managed by Spring (if it&#8217;s been scanned by Spring; check the <code>@ComponentScan<\/code> article to understand how this works). It will be available for injection with <code>@Autowired<\/code> like this:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282A36\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"@Service\npublic class MyService {\n    private final MyComponent myComponent;\n\n    @Autowired\n    public MyService(MyComponent myComponent) {\n        this.myComponent = myComponent;\n    }\n\n    public void performAction() {\n        myComponent.doSomething();\n    }\n}\n\n\" style=\"color:#f6f6f4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dracula-soft\" style=\"background-color: #282A36\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F6F6F4\">@<\/span><span style=\"color: #97E1F1; font-style: italic\">Service<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F286C4\">public<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">class<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #97E1F1\">MyService<\/span><span style=\"color: #F6F6F4\"> {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    <\/span><span style=\"color: #F286C4\">private<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">final<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #97E1F1; font-style: italic\">MyComponent<\/span><span style=\"color: #F6F6F4\"> myComponent;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    @<\/span><span style=\"color: #97E1F1; font-style: italic\">Autowired<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    <\/span><span style=\"color: #F286C4\">public<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #62E884\">MyService<\/span><span style=\"color: #F6F6F4\">(<\/span><span style=\"color: #97E1F1; font-style: italic\">MyComponent<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #FFB86C; font-style: italic\">myComponent<\/span><span style=\"color: #F6F6F4\">) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">        <\/span><span style=\"color: #BF9EEE; font-style: italic\">this<\/span><span style=\"color: #F6F6F4\">.myComponent <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> myComponent;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    }<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    <\/span><span style=\"color: #F286C4\">public<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #97E1F1; font-style: italic\">void<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #62E884\">performAction<\/span><span style=\"color: #F6F6F4\">() {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">        myComponent.<\/span><span style=\"color: #62E884\">doSomething<\/span><span style=\"color: #F6F6F4\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p>In the example above, <code>MyService<\/code> uses <code>@Autowired<\/code> to inject <code>MyComponent<\/code>.<\/p>\n\n\n\n<p><code>@Component<\/code> is an important Spring tool that helps create and manage parts of your program. It&#8217;s useful when you need to connect different parts of your code. Use <code>@Component<\/code> when you&#8217;re unsure if your class should be a controller, service, or repository.<\/p>\n\n\n\n<p>If you like this topic, make sure to follow me. I\u2019ll be explaining more about Spring annotations! Stay tuned!<\/p>\n\n\n\n<p><a href=\"https:\/\/twitter.com\/WillianFMoya\">Willian Moya (@WillianFMoya) \/ X (twitter.com)<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/www.linkedin.com\/in\/willianmoya\/\">Willian Ferreira Moya | LinkedIn<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is @Component? @Component is a stereotype annotation from the Spring framework that allows you to mark your classes as Spring components. These classes will be managed by Spring and will be available in the Spring IoC container, so they can be injected into other classes (also managed by Spring) through the @Autowired annotation. Use [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3,6],"tags":[],"class_list":["post-283","post","type-post","status-publish","format-standard","hentry","category-java","category-spring"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/springmasteryhub.com\/index.php?rest_route=\/wp\/v2\/posts\/283","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/springmasteryhub.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/springmasteryhub.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/springmasteryhub.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/springmasteryhub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=283"}],"version-history":[{"count":1,"href":"https:\/\/springmasteryhub.com\/index.php?rest_route=\/wp\/v2\/posts\/283\/revisions"}],"predecessor-version":[{"id":285,"href":"https:\/\/springmasteryhub.com\/index.php?rest_route=\/wp\/v2\/posts\/283\/revisions\/285"}],"wp:attachment":[{"href":"https:\/\/springmasteryhub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=283"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/springmasteryhub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=283"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/springmasteryhub.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=283"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}