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(); }