use of org.springframework.beans.factory.annotation.AnnotatedBeanDefinition in project leopard by tanhaichao.
the class OptionScanner method getValue.
protected String getValue(BeanDefinition beanDefinition) {
AnnotationMetadata metadata = ((AnnotatedBeanDefinition) beanDefinition).getMetadata();
AnnotationAttributes attributes = AnnotationAttributes.fromMap(metadata.getAnnotationAttributes(Option.class.getName(), false));
return attributes.getString("value");
}
use of org.springframework.beans.factory.annotation.AnnotatedBeanDefinition in project kork by spinnaker.
the class SpringLoaderBeanPostProcessor method postProcessAfterInitialization.
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
try {
final BeanDefinition def = pluginContext.getBeanDefinition(beanName);
// look for annotations that indicate a bean should be elevated to the service's app context
boolean exposeToApp = false;
// look annotations on bean class
if (bean.getClass().isAnnotationPresent(ExposeToApp.class) || bean.getClass().isAnnotationPresent(RestController.class)) {
exposeToApp = true;
} else {
if (def instanceof AnnotatedBeanDefinition) {
final AnnotationMetadata metadata = ((AnnotatedBeanDefinition) def).getMetadata();
// look for annotation on an enclosing configuration
if (metadata.hasAnnotation(ExposeToApp.class.getName())) {
exposeToApp = true;
} else {
// look for annotation on the method that instantiates the bean in the enclosing
// configuration
final Set<MethodMetadata> methods = metadata.getAnnotatedMethods(ExposeToApp.class.getName());
if (methods.stream().anyMatch(method -> method.getMethodName().equals(beanName))) {
exposeToApp = true;
}
}
}
}
if (exposeToApp) {
Class klass = bean.getClass();
if (def.getBeanClassName() != null) {
klass = pluginContext.getClassLoader().loadClass(def.getBeanClassName());
}
log.debug("Adding bean {} to application context", beanName);
beanPromoter.promote(beanName, bean, klass, def.isPrimary());
}
} catch (ClassNotFoundException e) {
log.error("Error loading class for bean {}", beanName, e);
}
return bean;
}
use of org.springframework.beans.factory.annotation.AnnotatedBeanDefinition in project spring-framework by spring-projects.
the class ConfigurationClassParser method parse.
public void parse(Set<BeanDefinitionHolder> configCandidates) {
this.deferredImportSelectors = new LinkedList<>();
for (BeanDefinitionHolder holder : configCandidates) {
BeanDefinition bd = holder.getBeanDefinition();
try {
if (bd instanceof AnnotatedBeanDefinition) {
parse(((AnnotatedBeanDefinition) bd).getMetadata(), holder.getBeanName());
} else if (bd instanceof AbstractBeanDefinition && ((AbstractBeanDefinition) bd).hasBeanClass()) {
parse(((AbstractBeanDefinition) bd).getBeanClass(), holder.getBeanName());
} else {
parse(bd.getBeanClassName(), holder.getBeanName());
}
} catch (BeanDefinitionStoreException ex) {
throw ex;
} catch (Throwable ex) {
throw new BeanDefinitionStoreException("Failed to parse configuration class [" + bd.getBeanClassName() + "]", ex);
}
}
processDeferredImportSelectors();
}
use of org.springframework.beans.factory.annotation.AnnotatedBeanDefinition in project spring-boot by spring-projects.
the class BeanTypeRegistry method getFactoryMethod.
private Method getFactoryMethod(ConfigurableListableBeanFactory beanFactory, BeanDefinition definition) throws Exception {
if (definition instanceof AnnotatedBeanDefinition) {
MethodMetadata factoryMethodMetadata = ((AnnotatedBeanDefinition) definition).getFactoryMethodMetadata();
if (factoryMethodMetadata instanceof StandardMethodMetadata) {
return ((StandardMethodMetadata) factoryMethodMetadata).getIntrospectedMethod();
}
}
BeanDefinition factoryDefinition = beanFactory.getBeanDefinition(definition.getFactoryBeanName());
Class<?> factoryClass = ClassUtils.forName(factoryDefinition.getBeanClassName(), beanFactory.getBeanClassLoader());
return getFactoryMethod(definition, factoryClass);
}
use of org.springframework.beans.factory.annotation.AnnotatedBeanDefinition in project spring-integration by spring-projects.
the class IdempotentReceiverAutoProxyCreatorInitializer method initialize.
@Override
public void initialize(ConfigurableListableBeanFactory beanFactory) throws BeansException {
BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
List<Map<String, String>> idempotentEndpointsMapping = new ManagedList<Map<String, String>>();
for (String beanName : registry.getBeanDefinitionNames()) {
BeanDefinition beanDefinition = registry.getBeanDefinition(beanName);
if (IdempotentReceiverInterceptor.class.getName().equals(beanDefinition.getBeanClassName())) {
Object value = beanDefinition.removeAttribute(IDEMPOTENT_ENDPOINTS_MAPPING);
Assert.isInstanceOf(String.class, value, "The 'mapping' of BeanDefinition 'IDEMPOTENT_ENDPOINTS_MAPPING' must be String.");
String mapping = (String) value;
String[] endpoints = StringUtils.tokenizeToStringArray(mapping, ",");
for (String endpoint : endpoints) {
Map<String, String> idempotentEndpoint = new ManagedMap<String, String>();
idempotentEndpoint.put(beanName, beanFactory.resolveEmbeddedValue(endpoint) + IntegrationConfigUtils.HANDLER_ALIAS_SUFFIX);
idempotentEndpointsMapping.add(idempotentEndpoint);
}
} else if (beanDefinition instanceof AnnotatedBeanDefinition) {
if (beanDefinition.getSource() instanceof MethodMetadata) {
MethodMetadata beanMethod = (MethodMetadata) beanDefinition.getSource();
String annotationType = IdempotentReceiver.class.getName();
if (beanMethod.isAnnotated(annotationType)) {
Object value = beanMethod.getAnnotationAttributes(annotationType).get("value");
if (value != null) {
Class<?> returnType;
if (beanMethod instanceof StandardMethodMetadata) {
returnType = ((StandardMethodMetadata) beanMethod).getIntrospectedMethod().getReturnType();
} else {
try {
returnType = ClassUtils.forName(beanMethod.getReturnTypeName(), beanFactory.getBeanClassLoader());
} catch (ClassNotFoundException e) {
throw new CannotLoadBeanClassException(beanDefinition.getDescription(), beanName, beanMethod.getReturnTypeName(), e);
}
}
String endpoint = beanName;
if (!MessageHandler.class.isAssignableFrom(returnType)) {
/*
MessageHandler beans, populated from @Bean methods, have a complex id,
including @Configuration bean name, method name and the Messaging annotation name.
The following pattern matches the bean name, regardless of the annotation name.
*/
endpoint = beanDefinition.getFactoryBeanName() + "." + beanName + ".*" + IntegrationConfigUtils.HANDLER_ALIAS_SUFFIX;
}
String[] interceptors = (String[]) value;
for (String interceptor : interceptors) {
Map<String, String> idempotentEndpoint = new ManagedMap<String, String>();
idempotentEndpoint.put(interceptor, endpoint);
idempotentEndpointsMapping.add(idempotentEndpoint);
}
}
}
}
}
}
if (!idempotentEndpointsMapping.isEmpty()) {
BeanDefinition bd = BeanDefinitionBuilder.rootBeanDefinition(IdempotentReceiverAutoProxyCreator.class).addPropertyValue("idempotentEndpointsMapping", idempotentEndpointsMapping).getBeanDefinition();
registry.registerBeanDefinition(IDEMPOTENT_RECEIVER_AUTO_PROXY_CREATOR_BEAN_NAME, bd);
}
}
Aggregations