From a9f35eb743f7d87babb8a267734fffc74d9b112c Mon Sep 17 00:00:00 2001 From: Pekka Helenius Date: Wed, 7 Oct 2020 11:32:49 +0300 Subject: [PATCH] Register classes as Spring services, deprecate instance attribute autowiring Signed-off-by: Pekka Helenius --- .../bookstore/service/BookAuthorHelper.java | 4 ++-- .../bookstore/service/HttpServerLogger.java | 2 ++ .../web/BookBasePathAwareController.java | 17 ++++------------- .../fjordtek/bookstore/web/BookController.java | 15 +++------------ .../bookstore/web/BookRestController.java | 3 ++- 5 files changed, 13 insertions(+), 28 deletions(-) diff --git a/bookstore/src/main/java/com/fjordtek/bookstore/service/BookAuthorHelper.java b/bookstore/src/main/java/com/fjordtek/bookstore/service/BookAuthorHelper.java index 7025249..46a8d7c 100644 --- a/bookstore/src/main/java/com/fjordtek/bookstore/service/BookAuthorHelper.java +++ b/bookstore/src/main/java/com/fjordtek/bookstore/service/BookAuthorHelper.java @@ -3,7 +3,7 @@ package com.fjordtek.bookstore.service; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; +import org.springframework.stereotype.Service; import com.fjordtek.bookstore.model.book.Author; import com.fjordtek.bookstore.model.book.AuthorRepository; @@ -21,7 +21,7 @@ import com.fjordtek.bookstore.model.book.Book; * @author Pekka Helenius */ -@Component +@Service public class BookAuthorHelper { @Autowired diff --git a/bookstore/src/main/java/com/fjordtek/bookstore/service/HttpServerLogger.java b/bookstore/src/main/java/com/fjordtek/bookstore/service/HttpServerLogger.java index f3e5d0c..0257701 100644 --- a/bookstore/src/main/java/com/fjordtek/bookstore/service/HttpServerLogger.java +++ b/bookstore/src/main/java/com/fjordtek/bookstore/service/HttpServerLogger.java @@ -11,6 +11,7 @@ import javax.servlet.http.HttpServletResponse; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Service; /** * @@ -19,6 +20,7 @@ import org.slf4j.LoggerFactory; * @author Pekka Helenius */ +@Service public class HttpServerLogger { private static final Logger serverLogger = LoggerFactory.getLogger(HttpServerLogger.class); diff --git a/bookstore/src/main/java/com/fjordtek/bookstore/web/BookBasePathAwareController.java b/bookstore/src/main/java/com/fjordtek/bookstore/web/BookBasePathAwareController.java index fbb5fd3..a036cfc 100644 --- a/bookstore/src/main/java/com/fjordtek/bookstore/web/BookBasePathAwareController.java +++ b/bookstore/src/main/java/com/fjordtek/bookstore/web/BookBasePathAwareController.java @@ -47,23 +47,14 @@ public class BookBasePathAwareController { @Autowired private CategoryRepository categoryRepository; + @Autowired private BookAuthorHelper bookAuthorHelper; - private BookEventHandler bookEventHandler; - private HttpServerLogger httpServerLogger = new HttpServerLogger(); + @Autowired + private BookEventHandler bookEventHandler; - /* - * This method MUST exist with Autowired annotation. Handles autowiring of external classes. - * If this method is not defined, they are not found by this controller class (are null). - */ @Autowired - private void instanceAttributeController( - BookAuthorHelper bookAuthorHelper, - BookEventHandler bookEventHandler - ) { - this.bookAuthorHelper = bookAuthorHelper; - this.bookEventHandler = bookEventHandler; - } + private HttpServerLogger httpServerLogger; ////////////////////////////// private void bookGetAndSetNestedJSON(Book book, JsonNode bookNode) { diff --git a/bookstore/src/main/java/com/fjordtek/bookstore/web/BookController.java b/bookstore/src/main/java/com/fjordtek/bookstore/web/BookController.java index 5f36969..a8c5ce1 100644 --- a/bookstore/src/main/java/com/fjordtek/bookstore/web/BookController.java +++ b/bookstore/src/main/java/com/fjordtek/bookstore/web/BookController.java @@ -75,20 +75,11 @@ public class BookController { @Autowired private BookHashRepository bookHashRepository; - private BookAuthorHelper bookAuthorHelper; - - /* - * This method MUST exist with Autowired annotation. Handles autowiring of external classes. - * If this method is not defined, they are not found by this controller class (are null). - */ @Autowired - private void instanceAttributeController( - BookAuthorHelper bookAuthorHelper - ) { - this.bookAuthorHelper = bookAuthorHelper; - } + private HttpServerLogger httpServerLogger; - private HttpServerLogger httpServerLogger = new HttpServerLogger(); + @Autowired + private BookAuthorHelper bookAuthorHelper; /* private Map globalModelMap = new HashMap() { diff --git a/bookstore/src/main/java/com/fjordtek/bookstore/web/BookRestController.java b/bookstore/src/main/java/com/fjordtek/bookstore/web/BookRestController.java index e587916..b43262a 100644 --- a/bookstore/src/main/java/com/fjordtek/bookstore/web/BookRestController.java +++ b/bookstore/src/main/java/com/fjordtek/bookstore/web/BookRestController.java @@ -49,7 +49,8 @@ public class BookRestController { private CategoryRepository categoryRepository; */ - private HttpServerLogger httpServerLogger = new HttpServerLogger(); + @Autowired + private HttpServerLogger httpServerLogger; @RequestMapping( value = "${page.url.json.list}",