{"id":211,"date":"2024-06-14T20:32:36","date_gmt":"2024-06-14T23:32:36","guid":{"rendered":"https:\/\/springmasteryhub.com\/?p=211"},"modified":"2024-06-14T20:32:36","modified_gmt":"2024-06-14T23:32:36","slug":"how-aliasfor-simplifies-annotations-usage-in-spring","status":"publish","type":"post","link":"https:\/\/springmasteryhub.com\/?p=211","title":{"rendered":"How @AliasFor Simplifies Annotations usage in Spring"},"content":{"rendered":"\n<p>This annotation is an annotation that allows you to create an alias for an attribute in an annotation. These attributes have the same function. The idea is to avoid confusion when multiple attributes can be used for the same purpose<\/p>\n\n\n\n<p>Maybe this is the first time you have seen it.<\/p>\n\n\n\n<p>Spring uses this annotation in places like <code>@Bean<\/code>, <code>@ComponentScan<\/code>, <code>@Scope<\/code>, <code>@RequestMapping<\/code>, etc.<\/p>\n\n\n\n<p>So it\u2019s more used internally. And maybe you are already using its functionality and don\u2019t even know.<\/p>\n\n\n\n<p>Let\u2019s take a look at how <code>@RequestMapping<\/code> is using this annotation:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\n@Target({ElementType.TYPE, ElementType.METHOD})\n@Retention(RetentionPolicy.RUNTIME)\n@Documented\n@Mapping\n@Reflective({ControllerMappingReflectiveProcessor.class})\npublic @interface RequestMapping {\n    String name() default &quot;&quot;;\n\n    @AliasFor(&quot;path&quot;)\n    String&#x5B;] value() default {};\n\n    @AliasFor(&quot;value&quot;)\n    String&#x5B;] path() default {};\n\n    RequestMethod&#x5B;] method() default {};\n\n    String&#x5B;] params() default {};\n\n    String&#x5B;] headers() default {};\n\n    String&#x5B;] consumes() default {};\n\n    String&#x5B;] produces() default {};\n}\n\n\n<\/pre><\/div>\n\n\n<p>The attributes <code>path<\/code> and <code>value<\/code> have the same purpose, they define the URL pattern for your request mappings.<\/p>\n\n\n\n<p>The scenario below shows how the effect is the same and you can choose the alias you want.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\n@Controller\npublic class AlisForController {\n\n    @RequestMapping(value = &quot;\/home&quot;, method = RequestMethod.GET)\n    public String homePage() {\n        return &quot;home&quot;;\n    }\n\n    \/\/ Equivalent to the above using 'path'\n    @RequestMapping(path = &quot;\/home&quot;, method = RequestMethod.GET)\n    public String homePage2() {\n        return &quot;home&quot;;\n    }\n}\n\n\n<\/pre><\/div>\n\n\n<p>Have you ever seen this before?<\/p>\n\n\n\n<p>Have some dev defined the <code>path<\/code> and in another controller, you found that it\u2019s using a <code>value<\/code> for the same purpose, and somehow everything works, now you know why it works.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>So, pay attention to your project, have you found any places where this happened? Open the annotation\u2019s code and see the <code>@AliasFor<\/code> there.<\/p>\n\n\n\n<p>If you like this topic, make sure to follow me. In the following days, 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\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This annotation is an annotation that allows you to create an alias for an attribute in an annotation. These attributes have the same function. The idea is to avoid confusion when multiple attributes can be used for the same purpose Maybe this is the first time you have seen it. Spring uses this annotation in [&hellip;]<\/p>\n","protected":false},"author":2,"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":[6],"tags":[13,18,22,23,26,27],"class_list":["post-211","post","type-post","status-publish","format-standard","hentry","category-spring","tag-java","tag-programming","tag-spring-boot","tag-spring-framework","tag-technology","tag-testing"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/springmasteryhub.com\/index.php?rest_route=\/wp\/v2\/posts\/211","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\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/springmasteryhub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=211"}],"version-history":[{"count":0,"href":"https:\/\/springmasteryhub.com\/index.php?rest_route=\/wp\/v2\/posts\/211\/revisions"}],"wp:attachment":[{"href":"https:\/\/springmasteryhub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=211"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/springmasteryhub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=211"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/springmasteryhub.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=211"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}