Example usage for org.springframework.context ApplicationContext findAnnotationOnBean

List of usage examples for org.springframework.context ApplicationContext findAnnotationOnBean

Introduction

In this page you can find the example usage for org.springframework.context ApplicationContext findAnnotationOnBean.

Prototype

@Nullable
<A extends Annotation> A findAnnotationOnBean(String beanName, Class<A> annotationType)
        throws NoSuchBeanDefinitionException;

Source Link

Document

Find an Annotation of annotationType 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 the bean's factory method (if any).

Usage

From source file:org.vaadin.spring.stuff.sidebar.SideBarItemDescriptor.java

protected SideBarItemDescriptor(String beanName, ApplicationContext applicationContext) {
    this.item = applicationContext.findAnnotationOnBean(beanName, SideBarItem.class);
    this.i18n = applicationContext.getBean(I18N.class);
    this.applicationContext = applicationContext;
    this.beanName = beanName;
    this.iconAnnotation = findIconAnnotation();
    this.iconProvider = findIconProvider();
}

From source file:org.vaadin.spring.sidebar.SideBarItemDescriptor.java

protected SideBarItemDescriptor(String beanName, ApplicationContext applicationContext) {
    this.item = applicationContext.findAnnotationOnBean(beanName, SideBarItem.class);
    LOGGER.debug("Item annotation of bean [{}] is [{}]", beanName, item);
    this.i18n = applicationContext.getBean(I18N.class);
    this.applicationContext = applicationContext;
    this.beanName = beanName;
    this.iconAnnotation = findIconAnnotation();
    LOGGER.debug("Icon annotation of bean [{}] is [{}]", beanName, iconAnnotation);
    this.iconProvider = findIconProvider();
    LOGGER.debug("Icon provider of bean [{}] is [{}]", beanName, iconProvider);
}

From source file:org.fenixedu.bennu.spring.BennuSpringConfiguration.java

private Set<String> getBaseNames(ApplicationContext context) {
    final Set<String> baseNames = new HashSet<>();
    baseNames.add(getBundleBasename("BennuSpringResources"));
    final String[] beanNames = context.getBeanNamesForAnnotation(BennuSpringModule.class);
    for (String beanName : beanNames) {
        BennuSpringModule bennuSpringModuleAnnotation = context.findAnnotationOnBean(beanName,
                BennuSpringModule.class);
        if (bennuSpringModuleAnnotation != null) {
            baseNames.addAll(Arrays.stream(bennuSpringModuleAnnotation.bundles()).map(this::getBundleBasename)
                    .collect(Collectors.toSet()));
        }//from  w  w  w . java  2  s .  c  om
    }
    return baseNames;
}

From source file:org.springframework.boot.actuate.context.properties.ConfigurationPropertiesReportEndpoint.java

/**
 * Extract configuration prefix from {@link ConfigurationProperties} annotation.
 * @param context the application context
 * @param beanFactoryMetaData the bean factory meta-data
 * @param beanName the bean name//from  w  w w .j a v a 2  s .  c o m
 * @return the prefix
 */
private String extractPrefix(ApplicationContext context, ConfigurationBeanFactoryMetadata beanFactoryMetaData,
        String beanName) {
    ConfigurationProperties annotation = context.findAnnotationOnBean(beanName, ConfigurationProperties.class);
    if (beanFactoryMetaData != null) {
        ConfigurationProperties override = beanFactoryMetaData.findFactoryAnnotation(beanName,
                ConfigurationProperties.class);
        if (override != null) {
            // The @Bean-level @ConfigurationProperties overrides the one at type
            // level when binding. Arguably we should render them both, but this one
            // might be the most relevant for a starting point.
            annotation = override;
        }
    }
    return annotation.prefix();
}

From source file:org.springframework.boot.actuate.endpoint.ConfigurationPropertiesReportEndpoint.java

/**
 * Extract configuration prefix from {@link ConfigurationProperties} annotation.
 * @param beanFactoryMetaData/* w ww . j  ava 2 s . c  o m*/
 */
private String extractPrefix(ApplicationContext context, ConfigurationBeanFactoryMetaData beanFactoryMetaData,
        String beanName, Object bean) {
    ConfigurationProperties annotation = context.findAnnotationOnBean(beanName, ConfigurationProperties.class);
    if (beanFactoryMetaData != null) {
        ConfigurationProperties override = beanFactoryMetaData.findFactoryAnnotation(beanName,
                ConfigurationProperties.class);
        if (override != null) {
            // The @Bean-level @ConfigurationProperties overrides the one at type
            // level when binding. Arguably we should render them both, but this one
            // might be the most relevant for a starting point.
            annotation = override;
        }
    }
    return (StringUtils.hasLength(annotation.value()) ? annotation.value() : annotation.prefix());
}

From source file:org.springframework.web.method.ControllerAdviceBean.java

/**
 * Find the names of beans annotated with
 * {@linkplain ControllerAdvice @ControllerAdvice} in the given
 * ApplicationContext and wrap them as {@code ControllerAdviceBean} instances.
 *///from w  ww .  j  a va  2  s.com
public static List<ControllerAdviceBean> findAnnotatedBeans(ApplicationContext applicationContext) {
    List<ControllerAdviceBean> beans = new ArrayList<ControllerAdviceBean>();
    for (String name : BeanFactoryUtils.beanNamesForTypeIncludingAncestors(applicationContext, Object.class)) {
        if (applicationContext.findAnnotationOnBean(name, ControllerAdvice.class) != null) {
            beans.add(new ControllerAdviceBean(name, applicationContext));
        }
    }
    return beans;
}