Java Spring候选Bean类型策略不应该在检查限定符之前实例化给定类型的所有Bean

Java Spring候选Bean类型策略不应该在检查限定符之前实例化给定类型的所有Bean,java,spring,Java,Spring,拥有一个具有多个数据库和自定义EntityManager作用域的应用程序 我认为Spring为某个类型创建所有bean条件,然后过滤合格的类型 因此,在我的情况下,它打开所有EMF,即使我只需要打开一个 调试将我带到导致问题的代码部分: org.springframework.beans.factory.annotation.BeanFactoryAnnotationUtils /** * Obtain a bean of type {@code T} from the given {@co

拥有一个具有多个数据库和自定义EntityManager作用域的应用程序

我认为Spring为某个类型创建所有bean条件,然后过滤合格的类型

因此,在我的情况下,它打开所有EMF,即使我只需要打开一个

调试将我带到导致问题的代码部分: org.springframework.beans.factory.annotation.BeanFactoryAnnotationUtils

/**
  * Obtain a bean of type {@code T} from the given {@code BeanFactory} declaring a qualifier
  * (e.g. {@code <qualifier>} or {@code @Qualifier}) matching the given qualifier).
 */
private static <T> T qualifiedBeanOfType(ConfigurableListableBeanFactory bf, Class<T> beanType, String qualifier) {    
    Map<String, T> candidateBeans = BeanFactoryUtils.beansOfTypeIncludingAncestors(bf, beanType);
 -> Spring is looking for all bean condidate with the given Type before testing the qualifier
        T matchingBean = null;
        for (String beanName : candidateBeans.keySet()) {
            if (isQualifierMatch(qualifier, beanName, bf)) {
            ........
            }
        }
    }
/**
*从声明限定符的给定{@code BeanFactory}获取类型为{@code T}的bean
*(例如{@code}或{@code@Qualifier})匹配给定的限定符)。
*/
私有静态T限定BeanOfType(ConfigurableListableBeanFactory bf,类beanType,字符串限定符){
Map candidateBeans=BeanFactoryUtils.beansoftypeincludingFounders(bf,beanType);
->在测试限定符之前,Spring将查找具有给定类型的所有bean condidate
T matchingBean=null;
for(字符串beanName:candidateBeans.keySet()){
if(isQualifierMatch(qualifier,beanName,bf)){
........
}
}
}
然后是创作部分

    public <T> Map<String, T> getBeansOfType(Class<T> type, boolean includeNonSingletons, boolean allowEagerInit)
                throws BeansException {

        String[] beanNames = getBeanNamesForType(type, includeNonSingletons, allowEagerInit);
        Map<String, T> result = new LinkedHashMap<String, T>(beanNames.length);
        for (String beanName : beanNames) {
            try {
               result.put(beanName, getBean(beanName, type));
-> the bean is created even if its not the qualified one
            }
    ...........
            }
        }
publicmap getBeansOfType(类类型,布尔includensingleton,布尔allowEagerInit)
抛出BeansException{
字符串[]beanNames=getBeanNamesForType(类型,包括单例,allowEagerInit);
映射结果=新的LinkedHashMap(beanNames.length);
for(字符串beanName:beanName){
试一试{
put(beanName,getBean(beanName,type));
->即使不是限定的bean,也会创建该bean
}
...........
}
}

在创建所有Bean条件之前,我可以修改Spring代码以找到合格的Bean,但我想知道是否还有其他方法可以做到?

context.xml中有一个惰性Bean实例化:

lazy-init="true" 

改进已被接受,并在版本4.1.9和4.2.4中推出。

有关详细信息,请参阅错误报告:

您是否提交了罚单或其他文件?找不到这样创建的罚单