private <T> Stream<String> getBeanNamesByTypeWithAnnotation(Class<? extends Annotation> annotationType, Class<T> beanType) throws Exception { return Stream.of(applicationContext.getBeanNamesForType(beanType)) .filter(name -> { final BeanDefinition beanDefinition = applicationContext.getBeanFactory().getBeanDefinition(name); final Map<String, Object> beansWithAnnotation = applicationContext.getBeansWithAnnotation(annotationType); if (beansWithAnnotation.containsKey(name)) { return true; } else if (beanDefinition.getSource() instanceof AnnotatedTypeMetadata) { return AnnotatedTypeMetadata.class.cast(beanDefinition.getSource()).isAnnotated(annotationType.getName()); } return false; }); }
private ServerServiceDefinition bindInterceptors(ServerServiceDefinition serviceDefinition, GRpcService gRpcService, Collection<ServerInterceptor> globalInterceptors) { Stream<? extends ServerInterceptor> privateInterceptors = Stream.of(gRpcService.interceptors()) .map(interceptorClass -> { try { return 0 < applicationContext.getBeanNamesForType(interceptorClass).length ? applicationContext.getBean(interceptorClass) : interceptorClass.newInstance(); } catch (Exception e) { throw new BeanCreationException("Failed to create interceptor instance.", e); } }); List<ServerInterceptor> interceptors = Stream.concat( gRpcService.applyGlobalInterceptors() ? globalInterceptors.stream() : Stream.empty(), privateInterceptors) .distinct() .sorted(serverInterceptorOrderComparator()) .collect(Collectors.toList()); return ServerInterceptors.intercept(serviceDefinition, interceptors); }
/** * Add beans that implement ApplicationListener as listeners. * Doesn't affect other listeners, which can be added without being beans. */ protected void registerListeners() { // Register statically specified listeners first. for (ApplicationListener<?> listener : getApplicationListeners()) { getApplicationEventMulticaster().addApplicationListener(listener); } // Do not initialize FactoryBeans here: We need to leave all regular beans // uninitialized to let post-processors apply to them! String[] listenerBeanNames = getBeanNamesForType(ApplicationListener.class, true, false); for (String listenerBeanName : listenerBeanNames) { getApplicationEventMulticaster().addApplicationListenerBean(listenerBeanName); } // Publish early application events now that we finally have a multicaster... Set<ApplicationEvent> earlyEventsToProcess = this.earlyApplicationEvents; this.earlyApplicationEvents = null; if (earlyEventsToProcess != null) { for (ApplicationEvent earlyEvent : earlyEventsToProcess) { getApplicationEventMulticaster().multicastEvent(earlyEvent); } } }
/** * Add beans that implement ApplicationListener as listeners. * Doesn't affect other listeners, which can be added without being beans. */ protected void registerListeners() { // Register statically specified listeners first. for (ApplicationListener<?> listener : getApplicationListeners()) { getApplicationEventMulticaster().addApplicationListener(listener); } // Do not initialize FactoryBeans here: We need to leave all regular beans // uninitialized to let post-processors apply to them! String[] listenerBeanNames = getBeanNamesForType(ApplicationListener.class, true, false); for (String listenerBeanName : listenerBeanNames) { getApplicationEventMulticaster().addApplicationListenerBean(listenerBeanName); } // Publish early application events now that we finally have a multicaster... Set<ApplicationEvent> earlyEventsToProcess = this.earlyApplicationEvents; this.earlyApplicationEvents = null; if (earlyEventsToProcess != null) { for (ApplicationEvent earlyEvent : earlyEventsToProcess) { getApplicationEventMulticaster().multicastEvent(earlyEvent); } } }
protected ProducerTemplate findOrCreateCamelTemplate() { String[] names = getApplicationContext().getBeanNamesForType(ProducerTemplate.class); if (names != null && names.length > 0) { return getApplicationContext().getBean(names[0], ProducerTemplate.class); } if (getCamelContexts().isEmpty()) { throw new IllegalArgumentException("No CamelContexts are available so cannot create a ProducerTemplate!"); } return getCamelContexts().get(0).createProducerTemplate(); }
public static List<Object> getBeansInScope(Class<?> type, AbstractApplicationContext ctx, int scope) { List<Object> beans = new ArrayList<Object>(); String[] names = ctx.getBeanNamesForType(type); RequestAttributes attributes = RequestContextHolder.currentRequestAttributes(); for (String name : names) { Object bean = attributes.getAttribute(name, scope); if (bean != null) beans.add(bean); } return beans; }
private <T> Stream<String> getBeanNamesByTypeWithAnnotation(Class<? extends Annotation> annotationType, Class<T> beanType) throws Exception { return Stream.of(applicationContext.getBeanNamesForType(beanType)) .filter(name -> { final BeanDefinition beanDefinition = applicationContext.getBeanFactory().getBeanDefinition(name); final Map<String, Object> beansWithAnnotation = applicationContext.getBeansWithAnnotation(annotationType); if (beansWithAnnotation.containsKey(name)) { return true; } else if (beanDefinition.getSource() instanceof AnnotatedTypeMetadata) { return AnnotatedTypeMetadata.class.cast(beanDefinition.getSource()).isAnnotated(annotationType.getName()); } return false; }); }
private ServerServiceDefinition bindInterceptors( ServerServiceDefinition serviceDefinition, GrpcService grpcService, Collection<ServerInterceptor> globalInterceptors) { Stream<? extends ServerInterceptor> privateInterceptors = Stream.of(grpcService.interceptors()).map(interceptorClass -> { try { return 0 < applicationContext.getBeanNamesForType(interceptorClass).length ? applicationContext.getBean(interceptorClass) : interceptorClass.newInstance(); } catch (InstantiationException | IllegalAccessException e) { throw new BeanCreationException("Failed to create interceptor instance.", e); } }); List<ServerInterceptor> interceptors = Stream.concat( grpcService.applyGlobalInterceptors() ? globalInterceptors.stream() : Stream.empty(), privateInterceptors) .distinct() .collect(Collectors.toList()); return ServerInterceptors.intercept(serviceDefinition, interceptors); }
private ServerServiceDefinition bindInterceptors(ServerServiceDefinition serviceDefinition, GRpcService gRpcService, Collection<ServerInterceptor> globalInterceptors) { Stream<? extends ServerInterceptor> privateInterceptors = Stream.of(gRpcService.interceptors()) .map(interceptorClass -> { try { return 0 < applicationContext.getBeanNamesForType(interceptorClass).length ? applicationContext.getBean(interceptorClass) : interceptorClass.newInstance(); } catch (Exception e) { throw new BeanCreationException("Failed to create interceptor instance.", e); } }); List<ServerInterceptor> interceptors = Stream.concat( gRpcService.applyGlobalInterceptors() ? globalInterceptors.stream() : Stream.empty(), privateInterceptors) .distinct() .sorted(serverInterceptorOrderComparator()) .collect(Collectors.toList()); return ServerInterceptors.intercept(serviceDefinition, interceptors); }
private <T> Stream<String> getBeanNamesByTypeWithAnnotation( Class<? extends Annotation> annotationType, Class<T> beanType) { return Stream.of(applicationContext.getBeanNamesForType(beanType)) .filter(name -> { final BeanDefinition beanDefinition = applicationContext.getBeanFactory().getBeanDefinition(name); final Map<String, Object> beansWithAnnotation = applicationContext.getBeansWithAnnotation(annotationType); if (beansWithAnnotation.containsKey(name)) { return true; } else if (beanDefinition.getSource() instanceof AnnotatedTypeMetadata) { // TODO need analysis return ((AnnotatedTypeMetadata) beanDefinition.getSource()) .isAnnotated(annotationType.getName()); } return false; }); }
List<FutureTask<T>> handlerInitTasks = new LinkedList<FutureTask<T>>(); for (HandlerConfigInfo handlerConfigInfo : configInfos) { String[] handlerBeanIds = handlerConfigInfo.getProxyHandlerContext().getBeanNamesForType(this.getHandlerType()); for (String taskHandlerBeanId : handlerBeanIds) { T handler = (T) handlerConfigInfo.getProxyHandlerContext().getBean(taskHandlerBeanId);
/** * Add beans that implement ApplicationListener as listeners. * Doesn't affect other listeners, which can be added without being beans. */ protected void registerListeners() { // Register statically specified listeners first. for (ApplicationListener<?> listener : getApplicationListeners()) { getApplicationEventMulticaster().addApplicationListener(listener); } // Do not initialize FactoryBeans here: We need to leave all regular beans // uninitialized to let post-processors apply to them! String[] listenerBeanNames = getBeanNamesForType(ApplicationListener.class, true, false); for (String listenerBeanName : listenerBeanNames) { getApplicationEventMulticaster().addApplicationListenerBean(listenerBeanName); } // Publish early application events now that we finally have a multicaster... Set<ApplicationEvent> earlyEventsToProcess = this.earlyApplicationEvents; this.earlyApplicationEvents = null; if (earlyEventsToProcess != null) { for (ApplicationEvent earlyEvent : earlyEventsToProcess) { getApplicationEventMulticaster().multicastEvent(earlyEvent); } } }