{"id":224,"date":"2024-06-26T19:00:00","date_gmt":"2024-06-26T22:00:00","guid":{"rendered":"https:\/\/springmasteryhub.com\/?p=224"},"modified":"2024-06-26T19:00:00","modified_gmt":"2024-06-26T22:00:00","slug":"resource-the-versatile-bean-injection-annotation-for-jakarta-ee-and-spring","status":"publish","type":"post","link":"https:\/\/springmasteryhub.com\/?p=224","title":{"rendered":"@Resource: The Versatile Bean Injection Annotation for Jakarta EE and Spring"},"content":{"rendered":"\n<p>This annotation does bean injection, like the <code>@Autowired<\/code> and <code>@Inject<\/code> annotations.<\/p>\n\n\n\n<p>This annotation is packaged with Jakarta EE and will work on your Spring projects.<\/p>\n\n\n\n<p>You can use this annotation almost in the same way you use the other annotations used to inject dependencies.<\/p>\n\n\n\n<p>Using in-field injection and set methods is possible, but constructors are not supported.<\/p>\n\n\n\n<p>It can match the type and the bean name definition. Also, you can combine it with the @Qualifier annotation.<\/p>\n\n\n\n<p>It can simplify migrating projects between Jakarta and Spring.<\/p>\n\n\n\n<p>Let\u2019s take a look at some examples of how to use it.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Field Injection with @Resource<\/h3>\n\n\n\n<p>This is the standard way, using above the field that will be injected.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\n@Repository\npublic class MyRepository {\n\n}\n\n@Component\npublic class MyService {\n    @Resource\n    private MyRepository myRepository;\n}\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Setter Injection<\/h3>\n\n\n\n<p>Another alternative to injecting beans with <code>@Resource<\/code> is to use the setter method.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\n@Repository\npublic class MySetterRepository {\n\n}\n\n@Component\npublic class MySetterService {\n    private static final Logger log = LoggerFactory.getLogger(MySetterService.class);\n    private MySetterRepository mySetterRepository;\n\n    @Resource\n    public void setMySetterRepository(MySetterRepository mySetterRepository) {\n        this.mySetterRepository = mySetterRepository;\n    }\n}\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Combining with @Qualifier<\/h3>\n\n\n\n<p>You can use the <code>@Resource<\/code> annotation combined with the <code>@Qualifier<\/code> in the same way you would do when using the <code>@Autowired<\/code>.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\n@Repository(&quot;anotherRepository&quot;)\npublic class AnotherRepository {\n\n}\n\n@Repository(&quot;specificRepository&quot;)\npublic class SpecificRepository {\n\n}\n\n@Component\npublic class MyQualifierService {\n    @Resource\n    @Qualifier(&quot;specificRepository&quot;)\n    private SpecificRepository specificRepository;\n\n    public void performService() {\n        specificRepository.doSomething();\n    }\n}\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Matching by Name: The &#8216;name&#8217; Attribute<\/h3>\n\n\n\n<p>If you do not want to use the <code>@Qualifier<\/code> annotation, you can use the <code>name<\/code> attribute from the <code>@Resource<\/code> annotation, to define the bean which will be injected.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\n@Repository(&quot;anotherRepository&quot;)\npublic class AnotherRepository {\n\n}\n\n@Repository(&quot;specificRepository&quot;)\npublic class SpecificRepository {\n\n}\n\n@Component\npublic class MyNameService {\n    @Resource(name = &quot;specificRepository&quot;)\n    private SpecificRepository specificNameRepository;\n\n}\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Conclusion<\/h3>\n\n\n\n<p>In this blog post, you learned about the <code>@Resouce<\/code> an alternative to the <code>@Autowired<\/code> annotation to inject beans, that can work both with Jakarta and Spring.<\/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","protected":false},"excerpt":{"rendered":"<p>This annotation does bean injection, like the @Autowired and @Inject annotations. This annotation is packaged with Jakarta EE and will work on your Spring projects. You can use this annotation almost in the same way you use the other annotations used to inject dependencies. Using in-field injection and set methods is possible, but constructors are [&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,20,22,23,24,26],"class_list":["post-224","post","type-post","status-publish","format-standard","hentry","category-spring","tag-java","tag-programming","tag-software-development","tag-spring-boot","tag-spring-framework","tag-springboot","tag-technology"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/springmasteryhub.com\/index.php?rest_route=\/wp\/v2\/posts\/224","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=224"}],"version-history":[{"count":0,"href":"https:\/\/springmasteryhub.com\/index.php?rest_route=\/wp\/v2\/posts\/224\/revisions"}],"wp:attachment":[{"href":"https:\/\/springmasteryhub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=224"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/springmasteryhub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=224"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/springmasteryhub.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=224"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}