org.springframework.beans.factory.config.ConfigurableListableBeanFactory#getParentBeanFactory ( )源码实例Demo

下面列出了org.springframework.beans.factory.config.ConfigurableListableBeanFactory#getParentBeanFactory ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

private boolean containsBeanDefinition(
        ConfigurableListableBeanFactory beanFactory, String name) {
    if (beanFactory.containsBeanDefinition(name)) {
        return true;
    }
    BeanFactory parent = beanFactory.getParentBeanFactory();
    if (parent instanceof ConfigurableListableBeanFactory) {
        return containsBeanDefinition((ConfigurableListableBeanFactory) parent,
                name);
    }
    return false;
}
 
源代码2 项目: bulbasaur   文件: Bulbasaur.java
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {

    if (springContextShare) {
        if (beanFactory.getParentBeanFactory() == null) {
            beanFactory.setParentBeanFactory(innerBeanFactory);
        } else {
            logger.warn("Had set Parent applicationContext!");
        }
    }
}
 
protected void modifyFeignBeanFacotries(ConfigurableListableBeanFactory beanFactory){
	ListableBeanFactory bf = (ListableBeanFactory)beanFactory.getParentBeanFactory();
	if(bf==null){
		return ;
	}
	String[] feignBeanNames = bf.getBeanNamesForAnnotation(EnhanceFeignClient.class);
	
	BeanDefinitionRegistry bdr = (BeanDefinitionRegistry)bf;
	Stream.of(feignBeanNames).forEach(beanName->{
		BeanDefinition feignBeanDefinition = bdr.getBeanDefinition(feignBeanNames[0]);
		String typeName = (String)feignBeanDefinition.getPropertyValues().getPropertyValue("type").getValue();
		Class<?> fallbackType = (Class<?>)feignBeanDefinition.getPropertyValues().getPropertyValue("fallback").getValue();
		Class<?> clientInterface = ReflectUtils.loadClass(typeName);
		Class<?> apiInterface = clientInterface.getInterfaces()[0];
		String[] typeBeanNames = bf.getBeanNamesForType(apiInterface);//maybe: feignclient, fallback, controller
		if(typeBeanNames.length<=1){
			return ;
		}
		String[] localBeanNames = ArrayUtils.removeElement(typeBeanNames, beanName);//remove
		Optional<String> localBeanNameOpt = Stream.of(localBeanNames).filter(lbn->{
			BeanDefinition bd = bdr.getBeanDefinition(lbn);
			return !bd.getBeanClassName().equals(fallbackType.getName());
		})
		.findFirst();
		
		if(!localBeanNameOpt.isPresent()){
			return ;
		}

		MutablePropertyValues mpvs = feignBeanDefinition.getPropertyValues();
		this.clearMutablePropertyValues(mpvs);
		feignBeanDefinition.setBeanClassName(LocalFeignInvokerFactoryBean.class.getName());
		mpvs.addPropertyValue("remoteInterface", clientInterface);
		mpvs.addPropertyValue("localBeanName", localBeanNameOpt.get());
	});
}
 
private static BeanDefinition getBeanDefinition(String beanName,
                                                ConfigurableListableBeanFactory beanFactory) {
    try {
        return beanFactory.getBeanDefinition(beanName);
    } catch (NoSuchBeanDefinitionException ex) {
        BeanFactory parentBeanFactory = beanFactory.getParentBeanFactory();
        if (parentBeanFactory instanceof ConfigurableListableBeanFactory) {
            return getBeanDefinition(beanName,
                    (ConfigurableListableBeanFactory) parentBeanFactory);
        }
        throw ex;
    }
}
 
@SuppressWarnings("Convert2streamapi")
private static void collectFromParentBeans(ConfigurableListableBeanFactory beanFactory, Map<String, String> serviceBeanNames) {
	BeanFactory parentBeanFactory = beanFactory.getParentBeanFactory();
	if (parentBeanFactory != null && ConfigurableListableBeanFactory.class.isInstance(parentBeanFactory)) {
		for (Entry<String, String> entry : findServiceBeanDefinitions((ConfigurableListableBeanFactory) parentBeanFactory).entrySet()) {
			if (isNotDuplicateService(serviceBeanNames, entry.getKey(), entry.getValue())) {
                   serviceBeanNames.put(entry.getKey(), entry.getValue());
               }
		}
	}
}
 
/**
 * Find a {@link BeanDefinition} in the {@link BeanFactory} or it's parents.
 */
private BeanDefinition findBeanDefinition(ConfigurableListableBeanFactory beanFactory, String serviceBeanName) {
	if (beanFactory.containsLocalBean(serviceBeanName)) {
		return beanFactory.getBeanDefinition(serviceBeanName);
	}
	BeanFactory parentBeanFactory = beanFactory.getParentBeanFactory();
	if (parentBeanFactory != null && ConfigurableListableBeanFactory.class.isInstance(parentBeanFactory)) {
		return findBeanDefinition((ConfigurableListableBeanFactory) parentBeanFactory, serviceBeanName);
	}
	throw new NoSuchBeanDefinitionException(serviceBeanName);
}
 
源代码7 项目: jsonrpc4j   文件: AutoJsonRpcServiceExporter.java
@SuppressWarnings("Convert2streamapi")
private static void collectFromParentBeans(ConfigurableListableBeanFactory beanFactory, Map<String, String> serviceBeanNames) {
	BeanFactory parentBeanFactory = beanFactory.getParentBeanFactory();
	if (parentBeanFactory != null && ConfigurableListableBeanFactory.class.isInstance(parentBeanFactory)) {
		for (Entry<String, String> entry : findServiceBeanDefinitions((ConfigurableListableBeanFactory) parentBeanFactory).entrySet()) {
			if (isNotDuplicateService(serviceBeanNames, entry.getKey(), entry.getValue()))
				serviceBeanNames.put(entry.getKey(), entry.getValue());
		}
	}
}
 
源代码8 项目: jsonrpc4j   文件: AutoJsonRpcServiceExporter.java
/**
 * Find a {@link BeanDefinition} in the {@link BeanFactory} or it's parents.
 */
private BeanDefinition findBeanDefinition(ConfigurableListableBeanFactory beanFactory, String serviceBeanName) {
	if (beanFactory.containsLocalBean(serviceBeanName)) return beanFactory.getBeanDefinition(serviceBeanName);
	BeanFactory parentBeanFactory = beanFactory.getParentBeanFactory();
	if (parentBeanFactory != null && ConfigurableListableBeanFactory.class.isInstance(parentBeanFactory))
		return findBeanDefinition((ConfigurableListableBeanFactory) parentBeanFactory, serviceBeanName);
	throw new RuntimeException(format("Bean with name '%s' can no longer be found.", serviceBeanName));
}
 
源代码9 项目: spring-context-support   文件: BeanUtils.java
/**
 * Get Bean Names from {@link ConfigurableListableBeanFactory} by type.
 *
 * @param beanFactory        {@link ConfigurableListableBeanFactory}
 * @param beanClass          The  {@link Class} of Bean
 * @param includingAncestors including ancestors or not
 * @return If found , return the array of Bean Names , or empty array.
 */
public static String[] getBeanNames(ConfigurableListableBeanFactory beanFactory, Class<?> beanClass,
                                    boolean includingAncestors) {

    Set<String> beanNames = new LinkedHashSet<String>();

    beanNames.addAll(doGetBeanNames(beanFactory, beanClass));

    if (includingAncestors) {

        BeanFactory parentBeanFactory = beanFactory.getParentBeanFactory();

        if (parentBeanFactory instanceof ConfigurableListableBeanFactory) {

            ConfigurableListableBeanFactory configurableListableBeanFactory =
                    (ConfigurableListableBeanFactory) parentBeanFactory;

            String[] parentBeanNames = getBeanNames(configurableListableBeanFactory, beanClass, includingAncestors);

            beanNames.addAll(Arrays.asList(parentBeanNames));

        }

    }

    return StringUtils.toStringArray(beanNames);
}