有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何检测在XML中配置的未使用的Springbean?

我有一个java项目,其中有很多。存在xml文件

所有这些xml文件都包含许多bean

未使用的测试为:

  • 已定义但从未注入的bean
  • 被注入但从未在代码中调用的bean
  • 已定义但从未加载到Spring上下文中的bean

问题

  • 我如何确定使用了还是没有使用哪个bean
  • 有没有一个实用程序可以做到这一点

共 (2) 个答案

  1. # 1 楼答案

    没有工具可以检测xml文件中未使用的springbeans。您可以使用Spring工具套件进行检测。但这需要花太多时间来检查

  2. # 2 楼答案

    我认为您可以使用spring执行器来检查相反的问题——在您的上下文中加载了哪些bean

    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.

    寻找所有未使用的bean需要扫描应用程序中的XML文件,然后可以将其与执行器生成的bean列表进行比较