Get all the beans in the Spring container

A testing colleague asked a question before, asking for help in counting how many interfaces there are in the project. Although swagger is used in the project, not all interfaces have the annotations required by swagger. At this time, I imagined implementing it through reflection. , but the premise is that you need to obtain all the beans in the Spring container, so how to obtain it, let's look at the analysis as follows:
 

We know that the Spring container will initialize a default BeanFactory when it is started. This BeanFactory is org.springframework.beans.factory.support.DefaultListableBeanFactory, which is when obtaining the BeanFactory in the famous AbstractApplicationContext refresh method:

 According to the inheritance diagram of the class, we will find that DefaultListableBeanFactory implements the ListableBeanFactory interface, which has a method to get all the Bean names in the Srping container:

And we know that we usually get the bean either through the bean name or the bean type, which is the two methods we usually use applicationContext.getBean(beanName) and applicationContext.getBean(Class type), then we need to get the Spring container All the beans inside and the statistics of how many interfaces there are are the following codes:
 

    public void getBean() {
        //获取所有的beanName
        String[] beans = applicationContext
                .getBeanDefinitionNames();
        int i = 0;
        for (String beanName : beans) {
            //根据名称找到所有的bean
            System.err.println(applicationContext.getBean(beanName));
            //根据Bean名称找到对应的Class,然后通过反射来统计RestController、Controller、RequestMapping的数量
            Class<?> beanType = applicationContext
                    .getType(beanName);
            RestController restController = beanType.getAnnotation(RestController.class);
            Controller controller = beanType.getAnnotation(Controller.class);
            if (restController != null || controller != null) {
                Method[] declaredMethods = beanType.getDeclaredMethods();
                for (Method declaredMethod : declaredMethods) {
                    if (declaredMethod.getAnnotation(RequestMapping.class) != null) {
                        i++;
                    }
                }
            }
        }
        System.err.println("接口数量:" + i);
    }

The second method: spring has provided all the beans and methods to easily obtain the specified annotations: For example, the following code can obtain all methods with @RequestMapping annotations.

public void getRequestMappingMethods() {
        String[] beanDefinitionNames = applicationContext.getBeanNamesForType(Object.class, false, true);
        for (String beanDefinitionName : beanDefinitionNames) {
            Object bean = null;
            Lazy onBean = applicationContext.findAnnotationOnBean(beanDefinitionName, Lazy.class);
            if (onBean != null) {
                continue;
            } else {
                bean = applicationContext.getBean(beanDefinitionName);
            }
            Map<Method, RequestMapping> annotatedMethods = null;   // referred to :org.springframework.context.event.EventListenerMethodProcessor.processBean
            try {
                annotatedMethods = MethodIntrospector.selectMethods(bean.getClass(),
                        new MethodIntrospector.MetadataLookup<RequestMapping>() {
                            @Override
                            public RequestMapping inspect(Method method) {
                                return AnnotatedElementUtils.findMergedAnnotation(method, RequestMapping.class);
                            }
                        });
            } catch (Throwable ex) {
                ex.printStackTrace();
            }
            if (annotatedMethods == null || annotatedMethods.isEmpty()) {
                continue;
            }
            for (Map.Entry<Method, RequestMapping> methodXxlJobEntry : annotatedMethods.entrySet()) {
                Method executeMethod = methodXxlJobEntry.getKey();
                RequestMapping xxlJob = methodXxlJobEntry.getValue();
                // regist
                System.err.println("executeMethod:"+executeMethod);
            }
        }
    }

Guess you like

Origin blog.csdn.net/qq_17805707/article/details/132102352