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.

GenericServlet::init(ServletConfig)->GenericServlet::init()-‌​‌​>GenericServlet::s‌​er‌​vice()->HTTPServ‌​let:‌​:service()->My‌​Servle‌​t::doGet()


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.

  • 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 acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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