use of org.springframework.beans.factory.config.BeanDefinitionHolder in project spring-integration by spring-projects.
the class WireTapParser method parse.
@Override
public String parse(Element element, ParserContext parserContext) {
Object source = parserContext.extractSource(element);
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(WireTap.class);
String targetRef = element.getAttribute("channel");
if (!StringUtils.hasText(targetRef)) {
parserContext.getReaderContext().error("The 'channel' attribute is required.", element);
}
builder.addConstructorArgReference(targetRef);
String selectorRef = element.getAttribute("selector");
String selectorExpression = element.getAttribute("selector-expression");
if (StringUtils.hasText(selectorRef) && StringUtils.hasText(selectorExpression)) {
parserContext.getReaderContext().error("Only one of 'selector' or 'selector-expression' is allowed", source);
}
if (StringUtils.hasText(selectorRef)) {
builder.addConstructorArgReference(selectorRef);
} else if (StringUtils.hasText(selectorExpression)) {
BeanDefinitionBuilder expressionBuilder = BeanDefinitionBuilder.genericBeanDefinition(ExpressionFactoryBean.class);
expressionBuilder.addConstructorArgValue(selectorExpression);
BeanDefinitionBuilder eemsBuilder = BeanDefinitionBuilder.genericBeanDefinition(ExpressionEvaluatingSelector.class);
eemsBuilder.addConstructorArgValue(expressionBuilder.getBeanDefinition());
builder.addConstructorArgValue(eemsBuilder.getBeanDefinition());
}
String timeout = element.getAttribute("timeout");
if (StringUtils.hasText(timeout)) {
builder.addPropertyValue("timeout", Long.parseLong(timeout));
}
String id = element.getAttribute("id");
if (StringUtils.hasText(id)) {
BeanDefinitionReaderUtils.registerBeanDefinition(new BeanDefinitionHolder(builder.getBeanDefinition(), id), parserContext.getRegistry());
return id;
}
return BeanDefinitionReaderUtils.registerWithGeneratedName(builder.getBeanDefinition(), parserContext.getRegistry());
}
use of org.springframework.beans.factory.config.BeanDefinitionHolder in project HotswapAgent by HotswapProjects.
the class ClassPathBeanDefinitionScannerAgent method defineBean.
/**
* Resolve candidate to a bean definition and (re)load in Spring.
* Synchronize to avoid parallel bean definition - usually on reload the beans are interrelated
* and parallel load will cause concurrent modification exception.
*
* @param candidate the candidate to reload
*/
public void defineBean(BeanDefinition candidate) {
synchronized (getClass()) {
// TODO sychronize on DefaultListableFactory.beanDefinitionMap?
ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(candidate);
candidate.setScope(scopeMetadata.getScopeName());
String beanName = this.beanNameGenerator.generateBeanName(candidate, registry);
if (candidate instanceof AbstractBeanDefinition) {
postProcessBeanDefinition((AbstractBeanDefinition) candidate, beanName);
}
if (candidate instanceof AnnotatedBeanDefinition) {
processCommonDefinitionAnnotations((AnnotatedBeanDefinition) candidate);
}
removeIfExists(beanName);
BeanDefinitionHolder definitionHolder = new BeanDefinitionHolder(candidate, beanName);
definitionHolder = applyScopedProxyMode(scopeMetadata, definitionHolder, registry);
LOGGER.reload("Registering Spring bean '{}'", beanName);
LOGGER.debug("Bean definition '{}'", beanName, candidate);
registerBeanDefinition(definitionHolder, registry);
freezeConfiguration();
}
ProxyReplacer.clearAllProxies();
}
use of org.springframework.beans.factory.config.BeanDefinitionHolder in project fabric8 by fabric8io.
the class AbstractServiceRegistar method registerBeanDefinitions.
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
for (Method method : REFLECTIONS.getMethodsAnnotatedWith(Factory.class)) {
String methodName = method.getName();
Class sourceType = getSourceType(method);
Class targetType = method.getReturnType();
Class beanType = method.getDeclaringClass();
BeanDefinitionHolder holder = createConverterBean(beanType, methodName, sourceType, targetType);
BeanDefinitionReaderUtils.registerBeanDefinition(holder, registry);
}
for (Field field : REFLECTIONS.getFieldsAnnotatedWith(ServiceName.class)) {
Class targetClass = field.getType();
Alias alias = field.getAnnotation(Alias.class);
ServiceName name = field.getAnnotation(ServiceName.class);
PortName port = field.getAnnotation(PortName.class);
Protocol protocol = field.getAnnotation(Protocol.class);
External external = field.getAnnotation(External.class);
String serviceName = name != null ? name.value() : null;
// We copy the service since we are going to add properties to it.
Service serviceInstance = new ServiceBuilder(getService(serviceName)).build();
String servicePort = port != null ? port.value() : null;
String serviceProtocol = protocol != null ? protocol.value() : DEFAULT_PROTOCOL;
Boolean serviceExternal = external != null && external.value();
String serviceAlias = alias != null ? alias.value() : createAlias(serviceName, targetClass, serviceProtocol, servicePort, serviceExternal);
// Add annotation info as additional properties
serviceInstance.getAdditionalProperties().put(ALIAS, serviceAlias);
serviceInstance.getAdditionalProperties().put(PROTOCOL, serviceProtocol);
serviceInstance.getAdditionalProperties().put(EXTERNAL, serviceExternal);
// We don't want to add a fallback value to the attributes.
if (port != null) {
serviceInstance.getAdditionalProperties().put(PORT, servicePort);
}
BeanDefinitionHolder holder = createServiceDefinition(serviceInstance, serviceAlias, serviceProtocol, servicePort, targetClass);
BeanDefinitionReaderUtils.registerBeanDefinition(holder, registry);
}
}
use of org.springframework.beans.factory.config.BeanDefinitionHolder in project fabric8 by fabric8io.
the class AbstractServiceRegistar method createServiceDefinition.
private BeanDefinitionHolder createServiceDefinition(Service service, String alias, String protocol, String port, Class type) {
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(KubernetesServiceFactoryBean.class);
builder.addPropertyValue("name", alias);
builder.addPropertyValue("service", service);
builder.addPropertyValue("port", port);
builder.addPropertyValue("type", type.getCanonicalName());
builder.setAutowireMode(Autowire.BY_TYPE.value());
// Add protocol qualifier
builder.getBeanDefinition().addQualifier(new AutowireCandidateQualifier(ServiceName.class, KubernetesHelper.getName(service)));
builder.getBeanDefinition().addQualifier(new AutowireCandidateQualifier(Protocol.class, protocol));
builder.getBeanDefinition().addQualifier(new AutowireCandidateQualifier(PortName.class, port != null ? port : ""));
return new BeanDefinitionHolder(builder.getBeanDefinition(), alias);
}
use of org.springframework.beans.factory.config.BeanDefinitionHolder in project fabric8 by fabric8io.
the class AbstractServiceRegistar method createConverterBean.
private <S, T> BeanDefinitionHolder createConverterBean(Class type, String methodName, Class<S> sourceType, Class<T> targetType) {
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(FactoryConverter.class);
String beanName = type.getName() + "." + methodName;
builder.addPropertyValue("name", methodName);
builder.addPropertyValue("type", type.getCanonicalName());
builder.addPropertyValue("sourceType", sourceType.getCanonicalName());
builder.addPropertyValue("targetType", targetType.getCanonicalName());
builder.setAutowireMode(Autowire.BY_TYPE.value());
return new BeanDefinitionHolder(builder.getBeanDefinition(), beanName);
}
Aggregations