From d3d65af83526943e9be3fd1ab0bf9544f40992de Mon Sep 17 00:00:00 2001 From: Eunsol Kim <61370551+Cyma-s@users.noreply.github.com> Date: Tue, 26 Sep 2023 23:09:29 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20=EC=83=9D=EC=84=B1=EC=9E=90=20?= =?UTF-8?q?=ED=8C=8C=EB=9D=BC=EB=AF=B8=ED=84=B0=20=EA=B0=80=EB=B3=80?= =?UTF-8?q?=EC=9D=B8=EC=9E=90=EB=A1=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/techcourse/DispatcherServletInitializer.java | 11 ++++++----- .../springframework/context/ApplicationContext.java | 8 ++++---- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/com/techcourse/DispatcherServletInitializer.java b/app/src/main/java/com/techcourse/DispatcherServletInitializer.java index beba1adf38..51d9ca26cb 100644 --- a/app/src/main/java/com/techcourse/DispatcherServletInitializer.java +++ b/app/src/main/java/com/techcourse/DispatcherServletInitializer.java @@ -8,25 +8,26 @@ import webmvc.org.springframework.web.servlet.DispatcherServlet; /** - * Base class for {@link WebApplicationInitializer} - * implementations that register a {@link DispatcherServlet} in the servlet context. + * Base class for {@link WebApplicationInitializer} implementations that register a {@link DispatcherServlet} in the + * servlet context. */ public class DispatcherServletInitializer implements WebApplicationInitializer { private static final Logger log = LoggerFactory.getLogger(DispatcherServletInitializer.class); private static final String APPLICATION_BASE_PACKAGE = "com.techcourse"; - + private static final String INTERNAL_BASE_PACKAGE = "webmvc.org.springframework.web.servlet"; private static final String DEFAULT_SERVLET_NAME = "dispatcher"; @Override public void onStartup(final ServletContext servletContext) { - final ApplicationContext applicationContext = new ApplicationContext(APPLICATION_BASE_PACKAGE); + final ApplicationContext applicationContext = new ApplicationContext(APPLICATION_BASE_PACKAGE, + INTERNAL_BASE_PACKAGE); final var dispatcherServlet = new DispatcherServlet(applicationContext); final var registration = servletContext.addServlet(DEFAULT_SERVLET_NAME, dispatcherServlet); if (registration == null) { throw new IllegalStateException("Failed to register servlet with name '" + DEFAULT_SERVLET_NAME + "'. " + - "Check if there is another servlet registered under the same name."); + "Check if there is another servlet registered under the same name."); } registration.setLoadOnStartup(1); diff --git a/mvc/src/main/java/context/org/springframework/context/ApplicationContext.java b/mvc/src/main/java/context/org/springframework/context/ApplicationContext.java index 6eeaa150a4..0458c37653 100644 --- a/mvc/src/main/java/context/org/springframework/context/ApplicationContext.java +++ b/mvc/src/main/java/context/org/springframework/context/ApplicationContext.java @@ -6,6 +6,7 @@ import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; +import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -17,7 +18,6 @@ public class ApplicationContext { - private static final String INTERNAL_BASE_PACKAGE = "webmvc.org.springframework.web.servlet"; private static final List> beanClasses = List.of( HandlerMapping.class, HandlerAdapter.class, @@ -29,10 +29,10 @@ public class ApplicationContext { private final Map, Object> beans; - public ApplicationContext(final String externalPackage) { + public ApplicationContext(final String... basePackages) { this.beans = new HashMap<>(); - registerBeans(INTERNAL_BASE_PACKAGE); - registerBeans(externalPackage); + Arrays.stream(basePackages) + .forEach(this::registerBeans); initialize(); }