How to detect unused spring beans configured in XML?

Viewed 540

I have a java project in which many .xml files are present.

All these xml files contain many beans.

The test for unused is:

  • A bean that is defined but never injected.
  • A bean that is injected but is never called in the code.
  • A bean that is defined but never loaded into the Spring context.

Questions

  • How do I identify which bean is used or not?
  • Is there a utility to do that?
2 Answers

There is no tools for detecting the unused springbeans in xml file. You can use the Spring Tools Suite for detecting. But it is taking too much time for checking.

I think that you can use spring actuator to check the opposite problem - which beans have been loaded in your context.

https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-endpoints.html

Actuator endpoints allow you to monitor and interact with your application. Spring Boot includes a number of built-in endpoints and you can also add your own. For example the health endpoint provides basic application health information.

The way that endpoints are exposed will depend on the type of technology that you choose. Most applications choose HTTP monitoring, where the ID of the endpoint is mapped to a URL. For example, by default, the health endpoint will be mapped to /health.

beans Displays a complete list of all the Spring beans in your application.

Looking for all the beans which are unused requires scanning XML files within application, then you can compare it with the list of beans produced by actuator.

Related