IOC principle can be implemented, using either:

  • Dependency Injection

  • Service locator pattern

This article also supports these two approaches for implementing IOC principle.

Spring IOC container is named after IOC principle that supports IOC implementation using Dependency injection, where implementations(<bean/>) can be injected at runtime as constructor-based or setter-based.

My understanding is, Servlet container(Ex: Apache tomcat) in it's implementation follow IOC principle because in runtime, servlet configuration information(from web.xml) is injected(more in next para) to pick corresponding servlet class implementation based on the configured data given in web.xml.

Following URL pattern matching rules, Servlet container takes configuration information of MySeervlet from web.xml and injects into GenericServlet::init(ServletConfig) method. Injects servlet configuration but not servlet implementation.


The purpose of both containers differ. But,

Does servlet container use service locator pattern to implement IOC principle?


Servlet containers provide a ServiceLoader that can be interpreted as an implementation of the service locator pattern.

In this case your application is loosely coupled to services locked up in the container. The implementation of the services returned by the container can be changed by reconfiguring the container without touching the application.

| improve this answer | |
  • you mean reconfiguring using web.xml? – user1787812 Oct 1 '17 at 14:27
  • web.xml is part of your application and is packaged with app.war. If we change it we change the application. Service implementations are either supplied through platform extensions (<JRE_HOME>/lib/ext) or are supplied to the ext path given by system property java.ext.dirs. See the e.g. Oracle Docs for details. – blafasel Oct 1 '17 at 14:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.