{"id":105,"date":"2009-02-11T13:19:52","date_gmt":"2009-02-11T20:19:52","guid":{"rendered":"http:\/\/www.connorgarvey.com\/blog\/?p=105"},"modified":"2010-07-23T05:54:34","modified_gmt":"2010-07-23T12:54:34","slug":"spring-annotations-static-injection-tutorial","status":"publish","type":"post","link":"https:\/\/connorgarvey.com\/blog\/?p=105","title":{"rendered":"Spring annotations static injection tutorial"},"content":{"rendered":"<p>Spring&#8217;s architecture isn&#8217;t very friendly to static classes and methods.\u00a0 It doesn&#8217;t have any way of injecting static properties of classes because it doesn&#8217;t have any way to discover them.\u00a0 Spring&#8217;s designers <a href=\"http:\/\/jira.springframework.org\/browse\/SPR-3845\" target=\"_blank\">have acknowledged that it&#8217;s a shortcoming of the framework<\/a> and suggest the use of this solution.<\/p>\n<ol>\n<li>Create the static property of the class without any annotations<\/li>\n<li>Mark the class to have static properties injected with @Component so that the properties will be injected on Spring startup<\/li>\n<li>Create a <strong>non-static<\/strong> setter method that sets the <strong>static<\/strong> property<\/li>\n<li>Mark the setter method with @Autowired(required = true)<\/li>\n<\/ol>\n<div id=\"wpshdo_1\" class=\"wp-synhighlighter-outer\"><div id=\"wpshdt_1\" class=\"wp-synhighlighter-expanded\"><table border=\"0\" width=\"100%\"><tr><td align=\"left\" width=\"80%\"><a name=\"#codesyntax_1\"><\/a><a id=\"wpshat_1\" class=\"wp-synhighlighter-title\" href=\"#codesyntax_1\"  onClick=\"javascript:wpsh_toggleBlock(1)\" title=\"Click to show\/hide code block\">Code block<\/a><\/td><td align=\"right\"><a href=\"#codesyntax_1\" onClick=\"javascript:wpsh_code(1)\" title=\"Show code only\"><img decoding=\"async\" border=\"0\" style=\"border: 0 none\" src=\"https:\/\/connorgarvey.com\/blog\/wp-content\/plugins\/wp-synhighlight\/themes\/default\/images\/code.png\" \/><\/a>&nbsp;<a href=\"#codesyntax_1\" onClick=\"javascript:wpsh_print(1)\" title=\"Print code\"><img decoding=\"async\" border=\"0\" style=\"border: 0 none\" src=\"https:\/\/connorgarvey.com\/blog\/wp-content\/plugins\/wp-synhighlight\/themes\/default\/images\/printer.png\" \/><\/a>&nbsp;<a href=\"https:\/\/connorgarvey.com\/blog\/wp-content\/plugins\/wp-synhighlight\/About.html\" target=\"_blank\" title=\"Show plugin information\"><img decoding=\"async\" border=\"0\" style=\"border: 0 none\" src=\"https:\/\/connorgarvey.com\/blog\/wp-content\/plugins\/wp-synhighlight\/themes\/default\/images\/info.gif\" \/><\/a>&nbsp;<\/td><\/tr><\/table><\/div><div id=\"wpshdi_1\" class=\"wp-synhighlighter-inner\" style=\"display: block;\"><pre class=\"java\" style=\"font-family:monospace;\">@<a href=\"http:\/\/www.google.com\/search?hl=en&amp;q=allinurl%3Acomponent+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky\"><span class=\"kw3\">Component<\/span><\/a>\n<span class=\"kw1\">public<\/span> <span class=\"kw1\">class<\/span> UserUtils\n<span class=\"br0\">&#123;<\/span>\n  <span class=\"kw1\">private<\/span> <span class=\"kw1\">static<\/span> UserAccessor userAccessor<span class=\"sy0\">;<\/span>\n&nbsp;\n  <span class=\"co3\">\/**\n   * Sets the user DAO. This method should never be called except by Spring\n   * @param userAccessor The user accessor to set\n   *\/<\/span>\n  @Autowired<span class=\"br0\">&#40;<\/span>required <span class=\"sy0\">=<\/span> <span class=\"kw2\">true<\/span><span class=\"br0\">&#41;<\/span>\n  <span class=\"kw1\">public<\/span> <span class=\"kw4\">void<\/span> setUserAccessor<span class=\"br0\">&#40;<\/span>userAccessor UserAccessor<span class=\"br0\">&#41;<\/span> <span class=\"br0\">&#123;<\/span>\n    UserUtils.<span class=\"me1\">userAccessor<\/span> <span class=\"sy0\">=<\/span> userAccessor<span class=\"sy0\">;<\/span>\n  <span class=\"br0\">&#125;<\/span>\n<span class=\"br0\">&#125;<\/span><\/pre><\/div><\/div>\n<p>Using this technique, you can have all of the advantages of Spring injection without the headaches of Spring injection!\u00a0 Avoid using this technique whenever possible.\u00a0 It should only be used to support legacy applications.\u00a0 With a lot of statically stored values, your application will not scale well.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Spring&#8217;s architecture isn&#8217;t very friendly to static classes and methods.\u00a0 It doesn&#8217;t have any way of injecting static properties of classes because it doesn&#8217;t have any way to discover them.\u00a0 Spring&#8217;s designers have acknowledged that it&#8217;s a shortcoming of the framework and suggest the use of this solution. Create the static property of the class [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"jetpack_post_was_ever_published":false,"_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":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[23],"tags":[24,4,7,60],"class_list":["post-105","post","type-post","status-publish","format-standard","hentry","category-spring","tag-injection","tag-java","tag-redpoint","tag-spring"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1ql2n-1H","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/connorgarvey.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/105","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/connorgarvey.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/connorgarvey.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/connorgarvey.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/connorgarvey.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=105"}],"version-history":[{"count":10,"href":"https:\/\/connorgarvey.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/105\/revisions"}],"predecessor-version":[{"id":108,"href":"https:\/\/connorgarvey.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/105\/revisions\/108"}],"wp:attachment":[{"href":"https:\/\/connorgarvey.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=105"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/connorgarvey.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=105"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/connorgarvey.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=105"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}