美文网首页
如何从容器中获取懒加载的类

如何从容器中获取懒加载的类

作者: guessguess | 来源:发表于2021-02-04 16:52 被阅读0次
    @Component
    public class CustomBeanFactoryPostProcessor implements BeanFactoryPostProcessor{
        
        private static final List<String> LAZY_BEANNAMES = new ArrayList<String>();
        
        private static ConfigurableListableBeanFactory STATIC_FAC;
        
        @Override
        public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
            STATIC_FAC = beanFactory;
            String[] beanDefinitionNames = beanFactory.getBeanDefinitionNames();
            if(beanDefinitionNames == null) {
                return;
            }
            Arrays.asList(beanDefinitionNames).forEach(beanDefinitionName -> {
                BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanDefinitionName);
                if(beanDefinition.isLazyInit()) {
                    LAZY_BEANNAMES.add(beanDefinitionName);
                }
            });
        }
    
        public static List<String> getLazyBeannames() {
            return LAZY_BEANNAMES;
        }
        
        public static BeanDefinition getBeanDefition(String beanName) {
            return STATIC_FAC.getBeanDefinition(beanName);
        }
    }
    
    

    相关文章

      网友评论

          本文标题:如何从容器中获取懒加载的类

          本文链接:https://www.haomeiwen.com/subject/rhwbtltx.html