/** * Find the names of beans annotated with * {@linkplain ControllerAdvice @ControllerAdvice} in the given * ApplicationContext and wrap them as {@code ControllerAdviceBean} instances. */ public static List<ControllerAdviceBean> findAnnotatedBeans(ApplicationContext context) { return Arrays.stream(BeanFactoryUtils.beanNamesForTypeIncludingAncestors(context, Object.class)) .filter(name -> context.findAnnotationOnBean(name, ControllerAdvice.class) != null) .map(name -> new ControllerAdviceBean(name, context)) .collect(Collectors.toList()); }
/** * Find the names of beans annotated with * {@linkplain ControllerAdvice @ControllerAdvice} in the given * ApplicationContext and wrap them as {@code ControllerAdviceBean} instances. */ public static List<ControllerAdviceBean> findAnnotatedBeans(ApplicationContext context) { return Arrays.stream(BeanFactoryUtils.beanNamesForTypeIncludingAncestors(context, Object.class)) .filter(name -> context.findAnnotationOnBean(name, ControllerAdvice.class) != null) .map(name -> new ControllerAdviceBean(name, context)) .collect(Collectors.toList()); }
private void initProviderBean(String beanName, Object bean) throws Exception { Service service = this.applicationContext.findAnnotationOnBean(beanName, Service.class); ServiceBean<Object> serviceConfig = new ServiceBean<Object>(service); if ((service.interfaceClass() == null || service.interfaceClass() == void.class)
private Iterable<Class<?>> getBeanTypesWithAnnotation(Class<? extends Annotation> type) { Set<Class<?>> annotatedTypes = new HashSet<>(); for (String beanName : context.getBeanDefinitionNames()) { Annotation annotation = context.findAnnotationOnBean(beanName, type); if (annotation != null) { annotatedTypes.add(context.getType(beanName)); } } return annotatedTypes; } }
@Override public Collection<GrpcCodecDefinition> findGrpcCodecs() { if (this.definitions == null) { log.debug("Searching for codecs..."); final String[] beanNames = this.applicationContext.getBeanNamesForAnnotation(GrpcCodec.class); final ImmutableList.Builder<GrpcCodecDefinition> builder = ImmutableList.builder(); for (final String beanName : beanNames) { final Codec codec = this.applicationContext.getBean(beanName, Codec.class); final GrpcCodec annotation = this.applicationContext.findAnnotationOnBean(beanName, GrpcCodec.class); builder.add(new GrpcCodecDefinition(codec, annotation.advertised(), annotation.codecType())); log.debug("Found gRPC codec: {}, bean: {}, class: {}", codec.getMessageEncoding(), beanName, codec.getClass().getName()); } this.definitions = builder.build(); log.debug("Done"); } return this.definitions; }
@Override public Collection<GrpcServiceDefinition> findGrpcServices() { Collection<String> beanNames = Arrays.asList(this.applicationContext.getBeanNamesForAnnotation(GrpcService.class)); List<GrpcServiceDefinition> definitions = Lists.newArrayListWithCapacity(beanNames.size()); GlobalServerInterceptorRegistry globalServerInterceptorRegistry = applicationContext.getBean(GlobalServerInterceptorRegistry.class); List<ServerInterceptor> globalInterceptorList = globalServerInterceptorRegistry.getServerInterceptors(); for (String beanName : beanNames) { BindableService bindableService = this.applicationContext.getBean(beanName, BindableService.class); ServerServiceDefinition serviceDefinition = bindableService.bindService(); GrpcService grpcServiceAnnotation = applicationContext.findAnnotationOnBean(beanName, GrpcService.class); serviceDefinition = bindInterceptors(serviceDefinition, grpcServiceAnnotation, globalInterceptorList); definitions.add(new GrpcServiceDefinition(beanName, bindableService.getClass(), serviceDefinition)); log.debug("Found gRPC service: " + serviceDefinition.getServiceDescriptor().getName() + ", bean: " + beanName + ", class: " + bindableService.getClass().getName()); } return definitions; }
/** * Extract configuration prefix from {@link ConfigurationProperties} annotation. * @param context the application context * @param beanFactoryMetaData the bean factory meta-data * @param beanName the bean name * @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(); }
public <A extends Annotation> A findAnnotationOnBean(String beanName, Class<A> annotationType) { return applicationContext.findAnnotationOnBean(beanName, annotationType); } public <T> T getBean(String name, Class<T> requiredType)
/** * You should never need to create instances of this class directly. * * @param beanName the name of the bean that implements the view to go to, must not be {@code null}. * @param applicationContext the application context to use when looking up beans, must not be {@code null}. */ public ViewItemDescriptor(String beanName, ApplicationContext applicationContext) { super(beanName, applicationContext); this.vaadinView = applicationContext.findAnnotationOnBean(beanName, SpringView.class); }
public <A extends Annotation> A findAnnotationOnBean(String beanName, Class<A> annotationType) { return wrappedApplicationContext.findAnnotationOnBean(beanName, annotationType); }
/** * Attempts to find and return an annotation of the specified type declared on this side bar item. * * @param annotationType the type of the annotation to look for. * @return the annotation, or {@code null} if not found. */ public <A extends Annotation> A findAnnotationOnBean(Class<A> annotationType) { return applicationContext.findAnnotationOnBean(beanName, annotationType); }
/** * You should never need to create instances of this class directly. * * @param beanName the name of the bean that implements the view to go to, must not be {@code null}. * @param applicationContext the application context to use when looking up beans, must not be {@code null}. */ public ViewItemDescriptor(String beanName, ApplicationContext applicationContext) { super(beanName, applicationContext); this.vaadinView = applicationContext.findAnnotationOnBean(beanName, SpringView.class); }
@Override public boolean isAccessGranted(UI ui, String beanName) { final SpringView annotation = applicationContext.findAnnotationOnBean(beanName, SpringView.class); if (annotation != null) { return allowedViews.contains(annotation.name()); } else { return false; } } }
@Override public <A extends Annotation> A findAnnotationOnBean( String s, Class<A> aClass ) throws NoSuchBeanDefinitionException { return applicationContext.findAnnotationOnBean( s, aClass ); }
public static <T extends Annotation, F> List<F> findAnnotatedBeans(Class<T> annotationType, Class<F> elementType) { List<F> beans = new ArrayList<>(); for (String name : BeanFactoryUtils.beanNamesForTypeIncludingAncestors(applicationContext, Object.class)) { if (applicationContext.findAnnotationOnBean(name, annotationType) != null) { beans.add(applicationContext.getBean(name, elementType)); } } return beans; }
/** * Find the names of beans annotated with * {@linkplain ControllerAdvice @ControllerAdvice} in the given * ApplicationContext and wrap them as {@code ControllerAdviceBean} instances. */ public static List<ControllerAdviceBean> findAnnotatedBeans(ApplicationContext context) { return Arrays.stream(BeanFactoryUtils.beanNamesForTypeIncludingAncestors(context, Object.class)) .filter(name -> context.findAnnotationOnBean(name, ControllerAdvice.class) != null) .map(name -> new ControllerAdviceBean(name, context)) .collect(Collectors.toList()); }
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); }
@Override public void onApplicationEvent(ContextRefreshedEvent event) { String[] beanNamesForAnnotation = applicationContext.getBeanNamesForAnnotation(RequestMapping.class); Arrays.stream(beanNamesForAnnotation) .filter(beanName -> applicationContext.findAnnotationOnBean(beanName, FeignClient.class) != null) .forEach(beanName -> throwAnnotationConfigurationException(beanName)); }
private Iterable<Class<?>> getBeanTypesWithAnnotation(Class<? extends Annotation> type) { Set<Class<?>> annotatedTypes = new HashSet<Class<?>>(); for (String beanName : context.getBeanDefinitionNames()) { Annotation annotation = context.findAnnotationOnBean(beanName, type); if (annotation != null) { annotatedTypes.add(context.getType(beanName)); } } return annotatedTypes; } }
@Override public boolean isAccessGranted(String beanName, UI ui) { Secured viewSecured = applicationContext.findAnnotationOnBean(beanName, Secured.class); if ( viewSecured == null ) { return true; } else if ( security.hasAccessDecisionManager() ) { return true; // Leave decision to the second hook } else { return security.hasAnyAuthority(viewSecured.value()); } }