Java Code Examples for org.springframework.beans.factory.config.BeanDefinition#getSource()

The following examples show how to use org.springframework.beans.factory.config.BeanDefinition#getSource() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
private PollableBean extractPollableAnnotation(StreamFunctionProperties functionProperties, GenericApplicationContext context,
		BindableFunctionProxyFactory proxyFactory) {
	// here we need to ensure that for cases where composition is defined we only look for supplier method to find Pollable annotation.
	String supplierFunctionName = StringUtils
			.delimitedListToStringArray(proxyFactory.getFunctionDefinition().replaceAll(",", "|").trim(), "|")[0];
	BeanDefinition bd = context.getBeanDefinition(supplierFunctionName);
	if (!(bd instanceof RootBeanDefinition)) {
		return null;
	}

	Method factoryMethod = ((RootBeanDefinition) bd).getResolvedFactoryMethod();
	if (factoryMethod == null) {
		Object source = bd.getSource();
		if (source instanceof MethodMetadata) {
			Class<?> factory = ClassUtils.resolveClassName(((MethodMetadata) source).getDeclaringClassName(), null);
			Class<?>[] params = FunctionContextUtils.getParamTypesFromBeanDefinitionFactory(factory, (RootBeanDefinition) bd);
			factoryMethod = ReflectionUtils.findMethod(factory, ((MethodMetadata) source).getMethodName(), params);
		}
	}
	Assert.notNull(factoryMethod, "Failed to introspect factory method since it was not discovered for function '"
			+ functionProperties.getDefinition() + "'");
	return factoryMethod.getReturnType().isAssignableFrom(Supplier.class)
			? AnnotationUtils.findAnnotation(factoryMethod, PollableBean.class)
					: null;
}
 
Example 2
private boolean isMatch(BeanDefinition beanDefinition, String beanName) {
    String className = beanDefinition.getBeanClassName();
    if (className == null) {
        if (beanDefinition.getFactoryMethodName() != null &&
                beanDefinition.getFactoryMethodName().contentEquals(beanName)) {
            Object source = beanDefinition.getSource();
            if (source instanceof MethodMetadata) {
                String returnType = ((MethodMetadata) source).getReturnTypeName();
                if (returnType.contentEquals("javax.sql.DataSource")) {
                    return true;
                }
                if (returnType.startsWith("org.springframework") || returnType.startsWith("io.micrometer")
                        || returnType.startsWith("com.fasterxml.") || returnType.startsWith("org.hibernate.")) {
                    return false;
                }
                className = returnType;
            }
        }
    }

    if (className != null) {
        try {
            final Class<?> beanClass = Class.forName(className);
            if (DataSource.class.isAssignableFrom(beanClass)
                    || BaseConnectionFactory.class.isAssignableFrom(beanClass)) {
                return true;
            }
        } catch (ClassNotFoundException e) {
        }
    }
    return false;
}
 
Example 3
private String getQualifier(String key) {
	if (this.applicationContext != null && this.applicationContext.getBeanFactory().containsBeanDefinition(key)) {
		BeanDefinition beanDefinition = this.applicationContext.getBeanFactory().getBeanDefinition(key);
		Object source = beanDefinition.getSource();
		if (source instanceof StandardMethodMetadata) {
			StandardMethodMetadata metadata = (StandardMethodMetadata) source;
			Qualifier qualifier = AnnotatedElementUtils.findMergedAnnotation(metadata.getIntrospectedMethod(),
				Qualifier.class);
			if (qualifier != null && qualifier.value().length() > 0) {
				return qualifier.value();
			}
		}
	}
	return key;
}
 
Example 4
private <T> T findQualifiedBeanByType(Class<? extends T> type, String qualifierValue, Class<? extends Annotation> qualifierType) {
    final NoSuchBeanDefinitionException noSuchBeanDefinitionException = new NoSuchBeanDefinitionException(qualifierValue, "No matching " + type.getSimpleName() +
            " bean found for qualifier " + qualifierValue + " of type " + qualifierType.getSimpleName() + " !");
    try {

        if (StringUtils.isEmpty(qualifierValue)) {
            if (qualifierType.equals(Qualifier.class)) {
                return Optional.of(
                        context.getBean(type))
                        .orElseThrow(() -> noSuchBeanDefinitionException);
            }
            return context.getBean(
                    Arrays.stream(context.getBeanNamesForAnnotation(qualifierType))
                            .filter(beanName -> type.isInstance(context.getBean(beanName)))
                            .findFirst()
                            .orElseThrow(() -> noSuchBeanDefinitionException),
                    type);
        }

        return BeanFactoryAnnotationUtils.qualifiedBeanOfType(context.getBeanFactory(), type, qualifierValue);
    } catch (NoSuchBeanDefinitionException noBeanException) {
        ConfigurableListableBeanFactory factory = context.getBeanFactory();

        for (String name : factory.getBeanDefinitionNames()) {
            BeanDefinition bd = factory.getBeanDefinition(name);

            if (bd.getSource() instanceof StandardMethodMetadata) {
                StandardMethodMetadata metadata = (StandardMethodMetadata) bd.getSource();

                if (metadata.getReturnTypeName().equals(type.getName())) {
                    Map<String, Object> attributes = metadata.getAnnotationAttributes(qualifierType.getName());
                    if (null != attributes) {
                        if (qualifierType.equals(Qualifier.class)) {
                            if (qualifierValue.equals(attributes.get("value"))) {
                                return context.getBean(name, type);
                            }
                        }
                        return context.getBean(name, type);
                    }
                }
            }
        }

        throw noSuchBeanDefinitionException;
    }
}
 
Example 5
@SuppressWarnings({"unchecked"})
private List<SpqrBean> findGraphQLApiBeans() {
    ConfigurableListableBeanFactory factory = context.getBeanFactory();

    List<SpqrBean> spqrBeans = new ArrayList<>();

    for (String beanName : factory.getBeanDefinitionNames()) {
        BeanDefinition bd = factory.getBeanDefinition(beanName);

        if (bd.getSource() instanceof StandardMethodMetadata) {
            StandardMethodMetadata metadata = (StandardMethodMetadata) bd.getSource();

            Map<String, Object> attributes = metadata.getAnnotationAttributes(GraphQLApi.class.getName());
            if (null == attributes) {
                continue;
            }

            SpqrBean spqrBean = new SpqrBean(context, beanName, metadata.getIntrospectedMethod().getAnnotatedReturnType());

            Map<String, Object> withResolverBuildersAttributes = metadata.getAnnotationAttributes(WithResolverBuilders.class.getTypeName());
            if (withResolverBuildersAttributes != null) {
                AnnotationAttributes[] annotationAttributesArray = (AnnotationAttributes[]) withResolverBuildersAttributes.get("value");
                Arrays.stream(annotationAttributesArray)
                        .forEach(annotationAttributes ->
                                spqrBean.getResolverBuilders().add(
                                        new ResolverBuilderBeanIdentity(
                                                (Class<? extends ResolverBuilder>) annotationAttributes.get("value"),
                                                (String) annotationAttributes.get("qualifierValue"),
                                                (Class<? extends Annotation>) annotationAttributes.get("qualifierType"))
                                )
                        );
            } else {
                Map<String, Object> withResolverBuilderAttributes = metadata.getAnnotationAttributes(WithResolverBuilder.class.getTypeName());
                if (withResolverBuilderAttributes != null) {
                    spqrBean.getResolverBuilders().add(
                            new ResolverBuilderBeanIdentity(
                                    (Class<? extends ResolverBuilder>) withResolverBuilderAttributes.get("value"),
                                    (String) withResolverBuilderAttributes.get("qualifierValue"),
                                    (Class<? extends Annotation>) withResolverBuilderAttributes.get("qualifierType"))
                    );
                }
            }

            spqrBeans.add(spqrBean);
        }
    }

    return spqrBeans;
}
 
Example 6
private static void testRefreshScoped(AssertableApplicationContext context, String beanName) {
    BeanDefinition beanDefinition = context.getBeanFactory().getBeanDefinition(beanName);
    MethodMetadata beanMethod = (MethodMetadata) beanDefinition.getSource();

    assertTrue(beanMethod.isAnnotated(RefreshScope.class.getName()));
}
 
Example 7
/**
 * Determine whether the given new bean definition is compatible with
 * the given existing bean definition.
 * <p>The default implementation considers them as compatible when the existing
 * bean definition comes from the same source or from a non-scanning source.
 * @param newDefinition the new bean definition, originated from scanning
 * @param existingDefinition the existing bean definition, potentially an
 * explicitly defined one or a previously generated one from scanning
 * @return whether the definitions are considered as compatible, with the
 * new definition to be skipped in favor of the existing definition
 */
protected boolean isCompatible(BeanDefinition newDefinition, BeanDefinition existingDefinition) {
	return (!(existingDefinition instanceof ScannedGenericBeanDefinition) ||  // explicitly registered overriding bean
			(newDefinition.getSource() != null && newDefinition.getSource().equals(existingDefinition.getSource())) ||  // scanned same file twice
			newDefinition.equals(existingDefinition));  // scanned equivalent class twice
}
 
Example 8
/**
 * Determine whether the given new bean definition is compatible with
 * the given existing bean definition.
 * <p>The default implementation considers them as compatible when the existing
 * bean definition comes from the same source or from a non-scanning source.
 * @param newDefinition the new bean definition, originated from scanning
 * @param existingDefinition the existing bean definition, potentially an
 * explicitly defined one or a previously generated one from scanning
 * @return whether the definitions are considered as compatible, with the
 * new definition to be skipped in favor of the existing definition
 */
protected boolean isCompatible(BeanDefinition newDefinition, BeanDefinition existingDefinition) {
	return (!(existingDefinition instanceof ScannedGenericBeanDefinition) ||  // explicitly registered overriding bean
			(newDefinition.getSource() != null && newDefinition.getSource().equals(existingDefinition.getSource())) ||  // scanned same file twice
			newDefinition.equals(existingDefinition));  // scanned equivalent class twice
}