{"id":1422,"date":"2024-01-30T19:53:23","date_gmt":"2024-01-30T19:53:23","guid":{"rendered":"https:\/\/tiemensfamily.com\/timoncs\/?p=1422"},"modified":"2024-01-30T19:54:13","modified_gmt":"2024-01-30T19:54:13","slug":"better-pattern-for-autowired","status":"publish","type":"post","link":"https:\/\/tiemensfamily.com\/timoncs\/2024\/01\/30\/better-pattern-for-autowired\/","title":{"rendered":"Better pattern for @Autowired"},"content":{"rendered":"\n<p>Just ran across this in a video about Spring Boot by Frank Moley and I thought it deserved a callout.<\/p>\n\n\n\n<p>When creating your Beans (e.g. Controller), you can either:<\/p>\n\n\n\n<div style=\"background-color: #ffffff; padding: 0px 0px 0px 0px;\">\n<div style=\"color: #000000; background-color: #ffffff; font-family: 'Courier New'; font-size: 14pt; white-space: pre;\">\n<code>\n@RestController()\npublic class ItemController {\n    @Autowired;\n    private ItemService itemService;\n<\/code>\n<\/div>\n<\/div>\n\n\n\n<p>Or you can do this:<\/p>\n\n\n\n<div style=\"background-color: #ffffff; padding: 0px 0px 0px 0px;\">\n<div style=\"color: #000000; background-color: #ffffff; font-family: 'Courier New'; font-size: 14pt; white-space: pre;\">\n<code>\n@RestController()\npublic class ItemController {\n    private final ItemService itemService;\n\n    @Autowired;\n    public ItemController(ItemService itemService) {\n        this.itemService = itemService\n    }\n<\/code>\n<\/div>\n<\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>Both work, but the second one makes JUnit testing way easier.  The first one @Autowires the actual field, and is really hard to isolate for test.  The second one lets the field stay POJO, and @Autowires the constructor.  Both are Spring friendly, but only the second one is JUnit friendly.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Just ran across this in a video about Spring Boot by Frank Moley and I thought it deserved a callout. When creating your Beans (e.g. Controller), you can either: @RestController() public class ItemController { @Autowired; private ItemService itemService; Or you &hellip; <a href=\"https:\/\/tiemensfamily.com\/timoncs\/2024\/01\/30\/better-pattern-for-autowired\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[6,1],"tags":[],"_links":{"self":[{"href":"https:\/\/tiemensfamily.com\/timoncs\/wp-json\/wp\/v2\/posts\/1422"}],"collection":[{"href":"https:\/\/tiemensfamily.com\/timoncs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tiemensfamily.com\/timoncs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tiemensfamily.com\/timoncs\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/tiemensfamily.com\/timoncs\/wp-json\/wp\/v2\/comments?post=1422"}],"version-history":[{"count":7,"href":"https:\/\/tiemensfamily.com\/timoncs\/wp-json\/wp\/v2\/posts\/1422\/revisions"}],"predecessor-version":[{"id":1429,"href":"https:\/\/tiemensfamily.com\/timoncs\/wp-json\/wp\/v2\/posts\/1422\/revisions\/1429"}],"wp:attachment":[{"href":"https:\/\/tiemensfamily.com\/timoncs\/wp-json\/wp\/v2\/media?parent=1422"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tiemensfamily.com\/timoncs\/wp-json\/wp\/v2\/categories?post=1422"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tiemensfamily.com\/timoncs\/wp-json\/wp\/v2\/tags?post=1422"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}