use of org.springframework.beans.factory.config.ConfigurableBeanFactory in project spring-framework by spring-projects.
the class AbstractApplicationEventMulticaster method setBeanFactory.
@Override
public void setBeanFactory(BeanFactory beanFactory) {
this.beanFactory = beanFactory;
if (beanFactory instanceof ConfigurableBeanFactory) {
ConfigurableBeanFactory cbf = (ConfigurableBeanFactory) beanFactory;
if (this.beanClassLoader == null) {
this.beanClassLoader = cbf.getBeanClassLoader();
}
this.retrievalMutex = cbf.getSingletonMutex();
}
}
use of org.springframework.beans.factory.config.ConfigurableBeanFactory in project spring-framework by spring-projects.
the class JmsListenerAnnotationBeanPostProcessor method setBeanFactory.
/**
* Making a {@link BeanFactory} available is optional; if not set,
* {@link JmsListenerConfigurer} beans won't get autodetected and an
* {@link #setEndpointRegistry endpoint registry} has to be explicitly configured.
*/
@Override
public void setBeanFactory(BeanFactory beanFactory) {
this.beanFactory = beanFactory;
if (beanFactory instanceof ConfigurableBeanFactory) {
this.embeddedValueResolver = new EmbeddedValueResolver((ConfigurableBeanFactory) beanFactory);
}
this.registrar.setBeanFactory(beanFactory);
}
use of org.springframework.beans.factory.config.ConfigurableBeanFactory in project spring-framework by spring-projects.
the class AbstractExpressionEvaluatingCondition method evaluateExpression.
private <A extends Annotation> boolean evaluateExpression(String expression, boolean loadContext, Class<A> annotationType, ExtensionContext extensionContext) {
AnnotatedElement element = extensionContext.getElement().get();
GenericApplicationContext gac = null;
ApplicationContext applicationContext;
if (loadContext) {
applicationContext = SpringExtension.getApplicationContext(extensionContext);
} else {
gac = new GenericApplicationContext();
gac.refresh();
applicationContext = gac;
}
if (!(applicationContext instanceof ConfigurableApplicationContext)) {
if (logger.isWarnEnabled()) {
String contextType = (applicationContext != null ? applicationContext.getClass().getName() : "null");
logger.warn(String.format("@%s(\"%s\") could not be evaluated on [%s] since the test " + "ApplicationContext [%s] is not a ConfigurableApplicationContext", annotationType.getSimpleName(), expression, element, contextType));
}
return false;
}
ConfigurableBeanFactory configurableBeanFactory = ((ConfigurableApplicationContext) applicationContext).getBeanFactory();
BeanExpressionResolver expressionResolver = configurableBeanFactory.getBeanExpressionResolver();
BeanExpressionContext beanExpressionContext = new BeanExpressionContext(configurableBeanFactory, null);
Object result = expressionResolver.evaluate(configurableBeanFactory.resolveEmbeddedValue(expression), beanExpressionContext);
if (gac != null) {
gac.close();
}
if (result instanceof Boolean) {
return ((Boolean) result).booleanValue();
} else if (result instanceof String) {
String str = ((String) result).trim().toLowerCase();
if ("true".equals(str)) {
return true;
}
Assert.state("false".equals(str), () -> String.format("@%s(\"%s\") on %s must evaluate to \"true\" or \"false\", not \"%s\"", annotationType.getSimpleName(), expression, element, result));
return false;
} else {
String message = String.format("@%s(\"%s\") on %s must evaluate to a String or a Boolean, not %s", annotationType.getSimpleName(), expression, element, (result != null ? result.getClass().getName() : "null"));
throw new IllegalStateException(message);
}
}
use of org.springframework.beans.factory.config.ConfigurableBeanFactory in project spring-framework by spring-projects.
the class AbstractJaxWsServiceExporter method publishEndpoints.
/**
* Publish all {@link javax.jws.WebService} annotated beans in the
* containing BeanFactory.
* @see #publishEndpoint
*/
public void publishEndpoints() {
Set<String> beanNames = new LinkedHashSet<>(this.beanFactory.getBeanDefinitionCount());
beanNames.addAll(Arrays.asList(this.beanFactory.getBeanDefinitionNames()));
if (this.beanFactory instanceof ConfigurableBeanFactory) {
beanNames.addAll(Arrays.asList(((ConfigurableBeanFactory) this.beanFactory).getSingletonNames()));
}
for (String beanName : beanNames) {
try {
Class<?> type = this.beanFactory.getType(beanName);
if (type != null && !type.isInterface()) {
WebService wsAnnotation = type.getAnnotation(WebService.class);
WebServiceProvider wsProviderAnnotation = type.getAnnotation(WebServiceProvider.class);
if (wsAnnotation != null || wsProviderAnnotation != null) {
Endpoint endpoint = createEndpoint(this.beanFactory.getBean(beanName));
if (this.endpointProperties != null) {
endpoint.setProperties(this.endpointProperties);
}
if (this.executor != null) {
endpoint.setExecutor(this.executor);
}
if (wsAnnotation != null) {
publishEndpoint(endpoint, wsAnnotation);
} else {
publishEndpoint(endpoint, wsProviderAnnotation);
}
this.publishedEndpoints.add(endpoint);
}
}
} catch (CannotLoadBeanClassException ex) {
// ignore beans where the class is not resolvable
}
}
}
use of org.springframework.beans.factory.config.ConfigurableBeanFactory in project grails-core by grails.
the class GenericBeanFactoryAccessor method findAnnotationOnBean.
/**
* Find a {@link Annotation} of <code>annotationType</code> on the specified
* bean, traversing its interfaces and super classes if no annotation can be
* found on the given class itself, as well as checking its raw bean class
* if not found on the exposed bean reference (e.g. in case of a proxy).
* @param beanName the name of the bean to look for annotations on
* @param annotationType the annotation class to look for
* @return the annotation of the given type found, or <code>null</code>
* @see org.springframework.core.annotation.AnnotationUtils#findAnnotation(Class, Class)
*/
public <A extends Annotation> A findAnnotationOnBean(String beanName, Class<A> annotationType) {
Class<?> handlerType = beanFactory.getType(beanName);
A ann = AnnotationUtils.findAnnotation(handlerType, annotationType);
if (ann == null && beanFactory instanceof ConfigurableBeanFactory && beanFactory.containsBeanDefinition(beanName)) {
ConfigurableBeanFactory cbf = (ConfigurableBeanFactory) beanFactory;
BeanDefinition bd = cbf.getMergedBeanDefinition(beanName);
if (bd instanceof AbstractBeanDefinition) {
AbstractBeanDefinition abd = (AbstractBeanDefinition) bd;
if (abd.hasBeanClass()) {
Class<?> beanClass = abd.getBeanClass();
ann = AnnotationUtils.findAnnotation(beanClass, annotationType);
}
}
}
return ann;
}
Aggregations