{"id":233,"date":"2024-07-05T16:00:00","date_gmt":"2024-07-05T19:00:00","guid":{"rendered":"https:\/\/springmasteryhub.com\/?p=233"},"modified":"2024-07-05T16:00:00","modified_gmt":"2024-07-05T19:00:00","slug":"using-the-lookup-annotation-in-spring","status":"publish","type":"post","link":"https:\/\/springmasteryhub.com\/?p=233","title":{"rendered":"Using the @Lookup Annotation in Spring"},"content":{"rendered":"\n<p>The <code>@Lookup<\/code> annotation is an injection (like <code>@Inject<\/code>, <code>@Resource<\/code>, <code>@Autowired<\/code>) annotation used at the method level. This annotation tells Spring to overwrite the method, redirecting to the bean factory to return a bean matching the return type of the method. This can be useful for some bean scopes, such as the prototype scope, which will return a new instance of the bean each time it is requested. By combining it with the <code>@Lookup<\/code> annotation, you can make things more dynamic.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Use Cases for <code>@Lookup<\/code><\/h2>\n\n\n\n<p>Imagine that you have a bean that handles user-specific session data. Each request should have its own instance to avoid data conflicts.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Code example:<\/h3>\n\n\n\n<p>Let&#8217;s say you have a prototype bean that gets the exact moment a user of your system has made a request.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\njavaCopy code\n@Component\n@Scope(&quot;prototype&quot;)\npublic class UserSessionBean {\n\n    private String userData;\n\n    public UserSessionBean() {\n        this.userData = &quot;Session data for &quot; + System.currentTimeMillis();\n    }\n\n    public String getUserData() {\n        return userData;\n    }\n\n}\n\n\n<\/pre><\/div>\n\n\n<p>Now you want Spring to get you a new bean to process a request and use this new bean&#8217;s information to do some processing. In this example, it will look like a session object that can hold user information during the request context.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\njavaCopy code\n@Component\npublic class UserService {\n\n    @Lookup\n    public UserSessionBean getUserSessionBean() {\n        \/\/ Spring will override this method to return a new instance of UserSessionBean\n        return null;\n    }\n\n    public String processUserSession() {\n        UserSessionBean userSessionBean = getUserSessionBean();\n        return &quot;Processing user session with data: &quot; + userSessionBean.getUserData();\n    }\n}\n\n@RestController\n@RequestMapping(&quot;\/users&quot;)\npublic class UserController {\n\n    private final UserService userService;\n\n    public UserController(UserService userService) {\n        this.userService = userService;\n    }\n\n    @GetMapping(&quot;\/session&quot;)\n    public String getUserSession() {\n        return userService.processUserSession();\n    }\n}\n\n\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>By using the <code>@Lookup<\/code> annotation, you can make things more dynamic. In this example, it allows you to handle user-specific sessions, but you can be creative and create many more scenarios using this annotation.<\/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<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/twitter.com\/WillianFMoya\">Willian Moya (@WillianFMoya) \/ X (twitter.com)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.linkedin.com\/in\/willianmoya\/\">Willian Ferreira Moya | LinkedIn<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>The @Lookup annotation is an injection (like @Inject, @Resource, @Autowired) annotation used at the method level. This annotation tells Spring to overwrite the method, redirecting to the bean factory to return a bean matching the return type of the method. This can be useful for some bean scopes, such as the prototype scope, which will [&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,27],"class_list":["post-233","post","type-post","status-publish","format-standard","hentry","category-spring","tag-java","tag-programming","tag-software-development","tag-spring-boot","tag-spring-framework","tag-testing"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/springmasteryhub.com\/index.php?rest_route=\/wp\/v2\/posts\/233","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=233"}],"version-history":[{"count":0,"href":"https:\/\/springmasteryhub.com\/index.php?rest_route=\/wp\/v2\/posts\/233\/revisions"}],"wp:attachment":[{"href":"https:\/\/springmasteryhub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=233"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/springmasteryhub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=233"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/springmasteryhub.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=233"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}