use of org.springframework.core.type.MethodMetadata in project AxonFramework by AxonFramework.
the class SpringUtils method isQualifierMatch.
/**
* Match if the given {@code qualifier} can be found on the
* {@link org.springframework.beans.factory.config.BeanDefinition} of the given {@code beanName}.
* Uses a {@link org.springframework.beans.factory.config.ConfigurableListableBeanFactory} to find the required
* information on the bean definition.
* <p/>
* Copied from {@link org.springframework.beans.factory.annotation.BeanFactoryAnnotationUtils}, plus an additional
* function to check for the Qualifier annotation on a factory method and some style adjustments.
*
* @param beanName A {@link java.lang.String} for the bean name to check its qualifier for.
* @param beanFactory A {@link org.springframework.beans.factory.config.ConfigurableListableBeanFactory} used to
* retrieve information about the given {@code beanName}.
* @param qualifier A {@link java.lang.String} for the {@code qualifier} we trying to match with the qualifier of
* the given {@code beanName}.
* @return True if the {@code qualifier} is found on the {@code beanName}; false if it is not found.
*/
public static boolean isQualifierMatch(String beanName, ConfigurableListableBeanFactory beanFactory, String qualifier) {
if (!beanFactory.containsBean(beanName)) {
return false;
}
try {
BeanDefinition bd = beanFactory.getMergedBeanDefinition(beanName);
// Check for Qualifier annotation on the FactoryMethod
if (bd instanceof AnnotatedBeanDefinition) {
MethodMetadata factoryMethodMetadata = ((AnnotatedBeanDefinition) bd).getFactoryMethodMetadata();
Map<String, Object> qualifierAttributes = factoryMethodMetadata.getAnnotationAttributes(Qualifier.class.getName());
if (qualifierAttributes != null && qualifier.equals(qualifierAttributes.get("value"))) {
return true;
}
}
// Explicit qualifier metadata on bean definition? (typically in XML definition)
if (bd instanceof AbstractBeanDefinition) {
AbstractBeanDefinition abd = (AbstractBeanDefinition) bd;
AutowireCandidateQualifier candidate = abd.getQualifier(Qualifier.class.getName());
if ((candidate != null && qualifier.equals(candidate.getAttribute(AutowireCandidateQualifier.VALUE_KEY))) || qualifier.equals(beanName) || ObjectUtils.containsElement(beanFactory.getAliases(beanName), qualifier)) {
return true;
}
}
// Corresponding qualifier on factory method? (typically in configuration class)
if (bd instanceof RootBeanDefinition) {
Method factoryMethod = ((RootBeanDefinition) bd).getResolvedFactoryMethod();
if (factoryMethod != null) {
Qualifier targetAnnotation = getAnnotation(factoryMethod, Qualifier.class);
if (targetAnnotation != null) {
return qualifier.equals(targetAnnotation.value());
}
}
}
// Corresponding qualifier on bean implementation class? (for custom user types)
Class<?> beanType = beanFactory.getType(beanName);
if (beanType != null) {
Qualifier targetAnnotation = getAnnotation(beanType, Qualifier.class);
if (targetAnnotation != null) {
return qualifier.equals(targetAnnotation.value());
}
}
} catch (NoSuchBeanDefinitionException ex) {
// Ignore - can't compare qualifiers for a manually registered singleton object
}
return false;
}
use of org.springframework.core.type.MethodMetadata in project spring-integration by spring-projects.
the class IntegrationFlowBeanPostProcessor method registerComponent.
@SuppressWarnings("unchecked")
private void registerComponent(Object component, String beanName, String parentName, BeanDefinitionCustomizer... customizers) {
AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.genericBeanDefinition((Class<Object>) component.getClass(), () -> component).applyCustomizers(customizers).getRawBeanDefinition();
if (parentName != null && this.beanFactory.containsBeanDefinition(parentName)) {
AbstractBeanDefinition parentBeanDefinition = (AbstractBeanDefinition) this.beanFactory.getBeanDefinition(parentName);
beanDefinition.setResource(parentBeanDefinition.getResource());
Object source = parentBeanDefinition.getSource();
if (source instanceof MethodMetadata) {
source = "bean method " + ((MethodMetadata) source).getMethodName();
}
beanDefinition.setSource(source);
this.beanFactory.registerDependentBean(parentName, beanName);
}
((BeanDefinitionRegistry) this.beanFactory).registerBeanDefinition(beanName, beanDefinition);
this.beanFactory.getBean(beanName);
}
use of org.springframework.core.type.MethodMetadata in project spring-integration by spring-projects.
the class StandardIntegrationFlowContext method registerBean.
@SuppressWarnings("unchecked")
private void registerBean(Object bean, @Nullable String beanNameArg, String parentName) {
String beanName = beanNameArg;
if (beanName == null) {
beanName = generateBeanName(bean, parentName);
}
AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.genericBeanDefinition((Class<Object>) bean.getClass(), () -> bean).getRawBeanDefinition();
if (parentName != null) {
AbstractBeanDefinition parentBeanDefinition = (AbstractBeanDefinition) this.beanFactory.getBeanDefinition(parentName);
Object source = parentBeanDefinition.getSource();
if (source instanceof MethodMetadata) {
source = "bean method " + ((MethodMetadata) source).getMethodName();
}
beanDefinition.setSource(source);
this.beanFactory.registerDependentBean(parentName, beanName);
}
this.beanDefinitionRegistry.registerBeanDefinition(beanName, beanDefinition);
this.beanFactory.getBean(beanName);
}
use of org.springframework.core.type.MethodMetadata in project sofa-boot by alipay.
the class BeanDefinitionUtil method resolveBeanClassType.
/**
* {@link AnnotatedGenericBeanDefinition}
* {@link ScannedGenericBeanDefinition}
* {@link GenericBeanDefinition}
* {@link org.springframework.beans.factory.support.ChildBeanDefinition}
* {@link org.springframework.beans.factory.support.RootBeanDefinition}
*
* @param beanDefinition resolve bean class type from bean definition
* @return
*/
public static Class<?> resolveBeanClassType(BeanDefinition beanDefinition) {
Class<?> clazz = null;
if (beanDefinition instanceof AnnotatedBeanDefinition) {
String className;
if (isFromConfigurationSource(beanDefinition)) {
MethodMetadata methodMetadata = ((AnnotatedBeanDefinition) beanDefinition).getFactoryMethodMetadata();
className = methodMetadata.getReturnTypeName();
} else {
AnnotationMetadata annotationMetadata = ((AnnotatedBeanDefinition) beanDefinition).getMetadata();
className = annotationMetadata.getClassName();
}
try {
clazz = org.springframework.util.StringUtils.isEmpty(className) ? null : ClassUtils.forName(className, null);
} catch (Throwable throwable) {
// ignore
}
}
if (clazz == null) {
try {
clazz = ((AbstractBeanDefinition) beanDefinition).getBeanClass();
} catch (IllegalStateException ex) {
try {
String className = beanDefinition.getBeanClassName();
clazz = StringUtils.isEmpty(className) ? null : ClassUtils.forName(className, null);
} catch (Throwable throwable) {
// ignore
}
}
}
if (ClassUtils.isCglibProxyClass(clazz)) {
return clazz.getSuperclass();
} else {
return clazz;
}
}
use of org.springframework.core.type.MethodMetadata in project apollo by apolloconfig.
the class BeanRegistrationUtil method registerBeanDefinitionIfNotExists.
public static boolean registerBeanDefinitionIfNotExists(BeanDefinitionRegistry registry, String beanName, Class<?> beanClass, Map<String, Object> extraPropertyValues) {
if (registry.containsBeanDefinition(beanName)) {
return false;
}
String[] candidates = registry.getBeanDefinitionNames();
String reservedBeanDefinition = RESERVED_BEAN_DEFINITIONS.get(beanClass.getName());
for (String candidate : candidates) {
BeanDefinition beanDefinition = registry.getBeanDefinition(candidate);
if (Objects.equals(beanDefinition.getBeanClassName(), beanClass.getName())) {
return false;
}
if (reservedBeanDefinition != null && beanDefinition.getSource() != null && beanDefinition.getSource() instanceof MethodMetadata) {
MethodMetadata metadata = (MethodMetadata) beanDefinition.getSource();
if (Objects.equals(reservedBeanDefinition, String.format("%s#%s", metadata.getDeclaringClassName(), metadata.getMethodName()))) {
return false;
}
}
}
BeanDefinition beanDefinition = BeanDefinitionBuilder.genericBeanDefinition(beanClass).getBeanDefinition();
if (extraPropertyValues != null) {
for (Map.Entry<String, Object> entry : extraPropertyValues.entrySet()) {
beanDefinition.getPropertyValues().add(entry.getKey(), entry.getValue());
}
}
registry.registerBeanDefinition(beanName, beanDefinition);
return true;
}
Aggregations