{"id":215,"date":"2022-11-01T19:40:39","date_gmt":"2022-11-01T19:40:39","guid":{"rendered":"https:\/\/valerio.nu\/?p=215"},"modified":"2022-11-01T19:40:40","modified_gmt":"2022-11-01T19:40:40","slug":"primary-vs-qualifier-annotations","status":"publish","type":"post","link":"https:\/\/valerio.nu\/index.php\/2022\/11\/01\/primary-vs-qualifier-annotations\/","title":{"rendered":"Primary vs Qualifier Annotations"},"content":{"rendered":"\n<p>Providing the Dependency Injection feature automatically in <a href=\"https:\/\/spring.io\/projects\/spring-boot\" target=\"_blank\" rel=\"noreferrer noopener\">Spring Boot<\/a> applications provides great convenience to the developers. In case of multiple services deriving from an interface though, which service will the Spring Boot application inject? In this case, it might not be able to decide which service to inject.<\/p>\n\n\n\n<p>Let&#8217;s consider the code below, it provides two different implementations of MessageService: <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public interface MessageService {\n    void printMessage();\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>@Service\npublic class ShortMessageService implements MessageService{\n    @Override\n    public void printMessage() {\n        System.out.println(\"ShortMessageService\");\n    }\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>@Service\npublic class LongMessageService implements MessageService{\n    @Override\n    public void printMessage() {\n        System.out.println(\"LongMessageService\");\n    }\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code><code>@Service\npublic class ExecuteMessageService {\n    @Autowired\n    private MessageService messageService;\n\n    public void getMessage(){\n        messageService.printMessage();\n    }\n}<\/code>\n<\/code><\/pre>\n\n\n\n<p>The above code gives an error at compile time as we perform the scenario exemplified in the video below.<\/p>\n\n\n\n<figure class=\"wp-block-video\"><video height=\"1380\" style=\"aspect-ratio: 2176 \/ 1380;\" width=\"2176\" controls src=\"https:\/\/valerio.nu\/wp-content\/uploads\/2022\/10\/Show-Error.mp4\"><\/video><\/figure>\n\n\n\n<p><strong>Field messageService in com.example.demo.ExecuteMessageService required a uniquely identifiable bean, but two equivalent implementations were found<\/strong>.<\/p>\n\n\n\n<p>This error is an example of the situation mentioned above, there are multiple implementations connected to the MessageService interface. Spring Boot couldn&#8217;t decide which one to inject.<\/p>\n\n\n\n<p>Annotations are used to quickly solve this problem in Spring Boot applications. There are two annotations that will help us solve this problem. These annotations are <em>Primary <\/em>and <em>Qualifier<\/em>. They have some similarities and some differences.\u00a0<\/p>\n\n\n\n<p>First of all, when we want to solve the problem with the Primary annotation, we should write @Primary at the beginning of the service we want to inject when the MessageService interface is called. For example, if we want the ShortMessageService service to be injected as follows, such an approach can be preferred.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Primary\n@Service\npublic class ShortMessageService implements MessageService{\n   @Override\n   public void printMessage(){\n      System.out.println(\"ShortMessageService\");\n   }\n}\n<\/code><\/pre>\n\n\n\n<p>Then, when we want to solve the problem with the other method, Qualifier annotation, our approach will be slightly different. Where we call the MessageService interface, we can specify which service to inject as a static parameter of the Qualifier annotation.<\/p>\n\n\n\n<p>For example, if we want the LongMessageService service to be injected, we can update the ExecuteMessageService service as follows.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Service\npublic class ExecuteMessageService{\n\n   @Autowired\n   @Qualifier(\"longMessageService\")\n   private MessageService messageService;\n\n   public void getMessage(){\n      messageService.printMessage();\n   }\n}<\/code><\/pre>\n\n\n\n<p>In the video below, we see that the Spring Boot application is successfully built with two methods separately.<\/p>\n\n\n\n<figure class=\"wp-block-video\"><video height=\"1380\" style=\"aspect-ratio: 2176 \/ 1380;\" width=\"2176\" controls src=\"https:\/\/valerio.nu\/wp-content\/uploads\/2022\/10\/Show-Success.mp4\"><\/video><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Difference between Qualifier and Primary<\/h2>\n\n\n\n<p>Software developers frequently use these two annotations in Spring Boot applications. There is a fundamental difference between these two annotations. If you use the Primary annotation, the same implementation will always be used, this provides more clarity and a more concise code. The Qualifier annotation is more flexible, you can use an implementation of the Service in one class and another in a different class.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Providing the Dependency Injection feature automatically in Spring Boot applications provides great convenience to the developers. In case of multiple services deriving from an interface [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":220,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"class_list":["post-215","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Primary vs Qualifier Annotations - Andy&#039;s blog \u2935\ufe0f<\/title>\n<meta name=\"description\" content=\"There is a fundamental difference between these two annotations. If you use Primary annotation, the same implementation will always be used.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/valerio.nu\/index.php\/2022\/11\/01\/primary-vs-qualifier-annotations\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Primary vs Qualifier Annotations - Andy&#039;s blog \u2935\ufe0f\" \/>\n<meta property=\"og:description\" content=\"There is a fundamental difference between these two annotations. If you use Primary annotation, the same implementation will always be used.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/valerio.nu\/index.php\/2022\/11\/01\/primary-vs-qualifier-annotations\/\" \/>\n<meta property=\"og:site_name\" content=\"Andy&#039;s blog \u2935\ufe0f\" \/>\n<meta property=\"article:published_time\" content=\"2022-11-01T19:40:39+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-11-01T19:40:40+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/valerio.nu\/wp-content\/uploads\/2022\/10\/Primary-vs-Qualifier-Annotations-in-Spring-Boot-1-scaled.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"2560\" \/>\n\t<meta property=\"og:image:height\" content=\"1707\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"andy\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"andy\" \/>\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:\\\/\\\/valerio.nu\\\/index.php\\\/2022\\\/11\\\/01\\\/primary-vs-qualifier-annotations\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/valerio.nu\\\/index.php\\\/2022\\\/11\\\/01\\\/primary-vs-qualifier-annotations\\\/\"},\"author\":{\"name\":\"andy\",\"@id\":\"https:\\\/\\\/valerio.nu\\\/#\\\/schema\\\/person\\\/da69cc3da309893b0d3bc8fcef75f128\"},\"headline\":\"Primary vs Qualifier Annotations\",\"datePublished\":\"2022-11-01T19:40:39+00:00\",\"dateModified\":\"2022-11-01T19:40:40+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/valerio.nu\\\/index.php\\\/2022\\\/11\\\/01\\\/primary-vs-qualifier-annotations\\\/\"},\"wordCount\":365,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/valerio.nu\\\/index.php\\\/2022\\\/11\\\/01\\\/primary-vs-qualifier-annotations\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/valerio.nu\\\/wp-content\\\/uploads\\\/2022\\\/10\\\/Primary-vs-Qualifier-Annotations-in-Spring-Boot-1-scaled.jpg\",\"articleSection\":[\"Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/valerio.nu\\\/index.php\\\/2022\\\/11\\\/01\\\/primary-vs-qualifier-annotations\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/valerio.nu\\\/index.php\\\/2022\\\/11\\\/01\\\/primary-vs-qualifier-annotations\\\/\",\"url\":\"https:\\\/\\\/valerio.nu\\\/index.php\\\/2022\\\/11\\\/01\\\/primary-vs-qualifier-annotations\\\/\",\"name\":\"Primary vs Qualifier Annotations - Andy&#039;s blog \u2935\ufe0f\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/valerio.nu\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/valerio.nu\\\/index.php\\\/2022\\\/11\\\/01\\\/primary-vs-qualifier-annotations\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/valerio.nu\\\/index.php\\\/2022\\\/11\\\/01\\\/primary-vs-qualifier-annotations\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/valerio.nu\\\/wp-content\\\/uploads\\\/2022\\\/10\\\/Primary-vs-Qualifier-Annotations-in-Spring-Boot-1-scaled.jpg\",\"datePublished\":\"2022-11-01T19:40:39+00:00\",\"dateModified\":\"2022-11-01T19:40:40+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/valerio.nu\\\/#\\\/schema\\\/person\\\/da69cc3da309893b0d3bc8fcef75f128\"},\"description\":\"There is a fundamental difference between these two annotations. If you use Primary annotation, the same implementation will always be used.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/valerio.nu\\\/index.php\\\/2022\\\/11\\\/01\\\/primary-vs-qualifier-annotations\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/valerio.nu\\\/index.php\\\/2022\\\/11\\\/01\\\/primary-vs-qualifier-annotations\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/valerio.nu\\\/index.php\\\/2022\\\/11\\\/01\\\/primary-vs-qualifier-annotations\\\/#primaryimage\",\"url\":\"https:\\\/\\\/valerio.nu\\\/wp-content\\\/uploads\\\/2022\\\/10\\\/Primary-vs-Qualifier-Annotations-in-Spring-Boot-1-scaled.jpg\",\"contentUrl\":\"https:\\\/\\\/valerio.nu\\\/wp-content\\\/uploads\\\/2022\\\/10\\\/Primary-vs-Qualifier-Annotations-in-Spring-Boot-1-scaled.jpg\",\"width\":2560,\"height\":1707,\"caption\":\"Primary vs Qualifier Annotations in Spring Boot\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/valerio.nu\\\/index.php\\\/2022\\\/11\\\/01\\\/primary-vs-qualifier-annotations\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/valerio.nu\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Primary vs Qualifier Annotations\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/valerio.nu\\\/#website\",\"url\":\"https:\\\/\\\/valerio.nu\\\/\",\"name\":\"Andy&#039;s blog \u2935\ufe0f\",\"description\":\"Abandon all hope, ye who enter here\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/valerio.nu\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/valerio.nu\\\/#\\\/schema\\\/person\\\/da69cc3da309893b0d3bc8fcef75f128\",\"name\":\"andy\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4852a025c6250c852814d4017f646677730a23c1ec14eb5ca36b69dcce5135a5?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4852a025c6250c852814d4017f646677730a23c1ec14eb5ca36b69dcce5135a5?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4852a025c6250c852814d4017f646677730a23c1ec14eb5ca36b69dcce5135a5?s=96&d=mm&r=g\",\"caption\":\"andy\"},\"sameAs\":[\"https:\\\/\\\/valerio.nu\"],\"url\":\"https:\\\/\\\/valerio.nu\\\/index.php\\\/author\\\/giuseppe\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Primary vs Qualifier Annotations - Andy&#039;s blog \u2935\ufe0f","description":"There is a fundamental difference between these two annotations. If you use Primary annotation, the same implementation will always be used.","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:\/\/valerio.nu\/index.php\/2022\/11\/01\/primary-vs-qualifier-annotations\/","og_locale":"en_US","og_type":"article","og_title":"Primary vs Qualifier Annotations - Andy&#039;s blog \u2935\ufe0f","og_description":"There is a fundamental difference between these two annotations. If you use Primary annotation, the same implementation will always be used.","og_url":"https:\/\/valerio.nu\/index.php\/2022\/11\/01\/primary-vs-qualifier-annotations\/","og_site_name":"Andy&#039;s blog \u2935\ufe0f","article_published_time":"2022-11-01T19:40:39+00:00","article_modified_time":"2022-11-01T19:40:40+00:00","og_image":[{"width":2560,"height":1707,"url":"https:\/\/valerio.nu\/wp-content\/uploads\/2022\/10\/Primary-vs-Qualifier-Annotations-in-Spring-Boot-1-scaled.jpg","type":"image\/jpeg"}],"author":"andy","twitter_card":"summary_large_image","twitter_misc":{"Written by":"andy","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/valerio.nu\/index.php\/2022\/11\/01\/primary-vs-qualifier-annotations\/#article","isPartOf":{"@id":"https:\/\/valerio.nu\/index.php\/2022\/11\/01\/primary-vs-qualifier-annotations\/"},"author":{"name":"andy","@id":"https:\/\/valerio.nu\/#\/schema\/person\/da69cc3da309893b0d3bc8fcef75f128"},"headline":"Primary vs Qualifier Annotations","datePublished":"2022-11-01T19:40:39+00:00","dateModified":"2022-11-01T19:40:40+00:00","mainEntityOfPage":{"@id":"https:\/\/valerio.nu\/index.php\/2022\/11\/01\/primary-vs-qualifier-annotations\/"},"wordCount":365,"commentCount":0,"image":{"@id":"https:\/\/valerio.nu\/index.php\/2022\/11\/01\/primary-vs-qualifier-annotations\/#primaryimage"},"thumbnailUrl":"https:\/\/valerio.nu\/wp-content\/uploads\/2022\/10\/Primary-vs-Qualifier-Annotations-in-Spring-Boot-1-scaled.jpg","articleSection":["Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/valerio.nu\/index.php\/2022\/11\/01\/primary-vs-qualifier-annotations\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/valerio.nu\/index.php\/2022\/11\/01\/primary-vs-qualifier-annotations\/","url":"https:\/\/valerio.nu\/index.php\/2022\/11\/01\/primary-vs-qualifier-annotations\/","name":"Primary vs Qualifier Annotations - Andy&#039;s blog \u2935\ufe0f","isPartOf":{"@id":"https:\/\/valerio.nu\/#website"},"primaryImageOfPage":{"@id":"https:\/\/valerio.nu\/index.php\/2022\/11\/01\/primary-vs-qualifier-annotations\/#primaryimage"},"image":{"@id":"https:\/\/valerio.nu\/index.php\/2022\/11\/01\/primary-vs-qualifier-annotations\/#primaryimage"},"thumbnailUrl":"https:\/\/valerio.nu\/wp-content\/uploads\/2022\/10\/Primary-vs-Qualifier-Annotations-in-Spring-Boot-1-scaled.jpg","datePublished":"2022-11-01T19:40:39+00:00","dateModified":"2022-11-01T19:40:40+00:00","author":{"@id":"https:\/\/valerio.nu\/#\/schema\/person\/da69cc3da309893b0d3bc8fcef75f128"},"description":"There is a fundamental difference between these two annotations. If you use Primary annotation, the same implementation will always be used.","breadcrumb":{"@id":"https:\/\/valerio.nu\/index.php\/2022\/11\/01\/primary-vs-qualifier-annotations\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/valerio.nu\/index.php\/2022\/11\/01\/primary-vs-qualifier-annotations\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/valerio.nu\/index.php\/2022\/11\/01\/primary-vs-qualifier-annotations\/#primaryimage","url":"https:\/\/valerio.nu\/wp-content\/uploads\/2022\/10\/Primary-vs-Qualifier-Annotations-in-Spring-Boot-1-scaled.jpg","contentUrl":"https:\/\/valerio.nu\/wp-content\/uploads\/2022\/10\/Primary-vs-Qualifier-Annotations-in-Spring-Boot-1-scaled.jpg","width":2560,"height":1707,"caption":"Primary vs Qualifier Annotations in Spring Boot"},{"@type":"BreadcrumbList","@id":"https:\/\/valerio.nu\/index.php\/2022\/11\/01\/primary-vs-qualifier-annotations\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/valerio.nu\/"},{"@type":"ListItem","position":2,"name":"Primary vs Qualifier Annotations"}]},{"@type":"WebSite","@id":"https:\/\/valerio.nu\/#website","url":"https:\/\/valerio.nu\/","name":"Andy&#039;s blog \u2935\ufe0f","description":"Abandon all hope, ye who enter here","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/valerio.nu\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/valerio.nu\/#\/schema\/person\/da69cc3da309893b0d3bc8fcef75f128","name":"andy","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4852a025c6250c852814d4017f646677730a23c1ec14eb5ca36b69dcce5135a5?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4852a025c6250c852814d4017f646677730a23c1ec14eb5ca36b69dcce5135a5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4852a025c6250c852814d4017f646677730a23c1ec14eb5ca36b69dcce5135a5?s=96&d=mm&r=g","caption":"andy"},"sameAs":["https:\/\/valerio.nu"],"url":"https:\/\/valerio.nu\/index.php\/author\/giuseppe\/"}]}},"_links":{"self":[{"href":"https:\/\/valerio.nu\/index.php\/wp-json\/wp\/v2\/posts\/215","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/valerio.nu\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/valerio.nu\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/valerio.nu\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/valerio.nu\/index.php\/wp-json\/wp\/v2\/comments?post=215"}],"version-history":[{"count":9,"href":"https:\/\/valerio.nu\/index.php\/wp-json\/wp\/v2\/posts\/215\/revisions"}],"predecessor-version":[{"id":230,"href":"https:\/\/valerio.nu\/index.php\/wp-json\/wp\/v2\/posts\/215\/revisions\/230"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/valerio.nu\/index.php\/wp-json\/wp\/v2\/media\/220"}],"wp:attachment":[{"href":"https:\/\/valerio.nu\/index.php\/wp-json\/wp\/v2\/media?parent=215"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/valerio.nu\/index.php\/wp-json\/wp\/v2\/categories?post=215"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/valerio.nu\/index.php\/wp-json\/wp\/v2\/tags?post=215"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}