Towards a Catalog of Java Dependency Injection Anti-Patterns

  • Rodrigo Laigner PUC-Rio
  • Marcos Kalinowski PUC-Rio
  • Luiz Carvalho PUC-Rio
  • Diogo Mendonça CEFET/RJ
  • Alessandro Garcia PUC-Rio


[Context] Dependency Injection (DI) is a commonly applied mechanism to decouple classes from their dependencies in order to provide better modularization of software. In the context of Java, the availability of a DI specification and popular frameworks, such as Spring, facilitate DI usage in software projects. However, bad DI implementation practices can have negative consequences, such as increasing coupling. Even though the literature suggests the existence of DI anti-patterns, there is no detailed catalog of such bad practices. Moreover, there is no evidence on their occurrence and perceived usefulness from the developer's point of view. [Goal] Our goal is to review the reported DI anti-patterns in order to analyze their completeness and to propose and evaluate a novel catalog of DI anti-patterns in the context of Java. [Method] We propose an initial catalog containing twelve Java DI anti-patterns. We selected four open source software projects that adopt a DI framework and developed a tool to statically analyze the occurrence of the DI anti-patterns within their source code. Also, we conducted a survey through face to face interviews with three experienced developers that regularly apply DI. [Results] At least nine different DI anti-patterns appeared in each analyzed project. In addition, the feedback received from the developers confirmed their relevance and the importance of investing further effort towards a catalog. [Conclusion] The results indicate that the initial catalog contains Java DI anti-patterns that occur in practice and are useful. Sharing it with practitioners may help them to avoid such anti-patterns. Sharing it with the research community will enable further improving the catalog.

Palavras-chave: dependency injection, dependency inversion, inversion of control, anti-pattern, java, catalog, modularization, coupling


Venera Arnaoudova, Massimiliano Di Penta, Giuliano Antoniol, and Yann-Gael Gueheneuc. 2013. A New Family of Software Anti-Patterns: Linguistic Anti-Patterns. In European Conference on Software Maintenance and Reengineering.

William J. Brown, Raphael C. Malveau, Hays W. McCormick III, and Thomas J. Mowbray. 1998. AntiPatterns Refactoring Software, Architectures, and Projects in Crisis. John Wiley & Sons.

Boyuan Chen and Zhen Ming (Jack) Jiang. 2017. Characterizing and Detecting Anti-patterns in the Logging Code. In International Conference on Software Engineering.

M. Crasso, C. Mateos, A. Zunino, and M. Campo. 2010. Empirically Assessing the Impact of DI on the Development of Web Service Applications. Journal of Web Engineering 9 (2010), 66--94.

F. Davis. 1989. Perceived usefulness, perceived ease of use, and user acceptance of information technology. MIS Quarterly 13 (3) (1989), 319--340.

Martin Fowler. 2004. Inversion of Control Containers and the Dependency Injection pattern.

Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides. 1995. Design patterns: elements of reusable object-oriented software. Addison-Wesley Longman Publishing Co.

Ralph E. Johnson and Brian Foote. 1988. Designing Reusable Classes. Journal of Object-Oriented Programming 1, 2 (1988), 22--35.

M. Lanza and R. Marinescu. 2006. Object-Oriented Metrics in Practice. Springer.

Craig Larman. 2004. Applying UML and Patterns: An Introduction to Object-Oriented Analysis and Design and Iterative Development (3rd Edition). Prentice Hall PTR, Upper Saddle River, NJ, USA.

J. Linaker, S. M. Sulaman, R. Maiani de Mello, and M. Höst. 2015. Guidelines for Conducting Surveys in Software Engineering. Technical Report. Lund University. [Publisher Information Missing].

R. C. Martin. 1996. The Dependency Inversion Principle. Report. 8(6) (1996), 61--66.

Dhanji R. Prasanna. 2009. Dependency Injection. Manning Publications Co., Greenwich, CT, USA.

E. Razina and D. Janzen. 2007. Effects of Dependency Injection on Maintainability. In International Conference Software Engineering and Applications.

JSRs: Java Specification Requests. 2015. JSR 330: Dependency Injection for Java.

Serguei Roubtsov, Alexander Serebrenik, and Mark van den Brand. 2010. Detecting Modularity Smells in Dependencies Injected with Java Annotations. In Software Maintenance and Reengineering European Conference. 244--247.

Mark Seemann. 2012. Dependency Injection in .NET. Manning Publications Co., Shelter Island, NY.

Nicholas Smith, Danny van Bruggen, and Federico Tomassetti. 2018. JavaParser: Visited. Leanpub.

Marco Torchiano, Daniel Méndez Fernández, Guilherme Horta Travassos, and Rafael Maiani de Mello. 2017. Lessons learnt in conducting survey research. In Proceedings of the 5th International Workshop on Conducting Empirical Studies in Industry. IEEE Press, 33--39.

Mark Turner, Barbara Kitchenham, Pearl Brereton, Stuart Charters, and David Budgen. 2010. Does the technology acceptance model predict actual use? A systematic literature review. Information and Software Technology 52 (2010), 463--479.

Steven van Deursen and Mark Seemann. 2018. Dependency Injection Principles, Practices, Patterns. Manning Publications Co., Shelter Island, NY.

C. Wohlin, P. Runeson, M. Höst, M.C. Ohlsson, B. Regnell, and A. Wesslén. 2012. Experimentation in software engineering. Springer.

Hong Yul Yang, Ewan Tempero, and Hayden Melton. 2008. An Empirical Study into Use of Dependency Injection in Java. In Australian Conference on Software Engineering.
LAIGNER, Rodrigo; KALINOWSKI, Marcos; CARVALHO, Luiz; MENDONÇA, Diogo; GARCIA, Alessandro. Towards a Catalog of Java Dependency Injection Anti-Patterns. In: SIMPÓSIO BRASILEIRO DE ENGENHARIA DE SOFTWARE (SBES), 33. , 2019, Salvador. Anais [...]. Porto Alegre: Sociedade Brasileira de Computação, 2019 .