private Method getFactoryMethod(ConfigurableListableBeanFactory beanFactory, BeanDefinition definition) throws Exception { if (definition instanceof AnnotatedBeanDefinition) { MethodMetadata factoryMethodMetadata = ((AnnotatedBeanDefinition) definition) .getFactoryMethodMetadata(); if (factoryMethodMetadata instanceof StandardMethodMetadata) { return ((StandardMethodMetadata) factoryMethodMetadata) .getIntrospectedMethod(); } } BeanDefinition factoryDefinition = beanFactory .getBeanDefinition(definition.getFactoryBeanName()); Class<?> factoryClass = ClassUtils.forName(factoryDefinition.getBeanClassName(), beanFactory.getBeanClassLoader()); return getFactoryMethod(definition, factoryClass); }
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.isEmpty()) { return beansWithAnnotation.containsKey(name); } else if (beanDefinition.getSource() instanceof StandardMethodMetadata) { StandardMethodMetadata metadata = (StandardMethodMetadata) beanDefinition.getSource(); return metadata.isAnnotated(annotationType.getName()); } return false; }); }
private Method getFactoryMethod(ConfigurableListableBeanFactory beanFactory, BeanDefinition definition) throws Exception { if (definition instanceof AnnotatedBeanDefinition) { MethodMetadata factoryMethodMetadata = ((AnnotatedBeanDefinition) definition) .getFactoryMethodMetadata(); if (factoryMethodMetadata instanceof StandardMethodMetadata) { return ((StandardMethodMetadata) factoryMethodMetadata) .getIntrospectedMethod(); } } BeanDefinition factoryDefinition = beanFactory .getBeanDefinition(definition.getFactoryBeanName()); Class<?> factoryClass = ClassUtils.forName(factoryDefinition.getBeanClassName(), beanFactory.getBeanClassLoader()); return ReflectionUtils.findMethod(factoryClass, definition.getFactoryMethodName()); }
public Map<String, Map<String, Object>> getBeansWithAnnotation(Class<? extends Annotation> type, Predicate<Map<String, Object>> attributeFilter) { Map<String, Map<String, Object>> result = Maps.newConcurrentMap(); ConfigurableListableBeanFactory factory = (ConfigurableListableBeanFactory) applicationContext.getAutowireCapableBeanFactory(); for (String name : factory.getBeanDefinitionNames()) { BeanDefinition bd = factory.getBeanDefinition(name); if (bd.getSource() instanceof StandardMethodMetadata) { StandardMethodMetadata metadata = (StandardMethodMetadata) bd.getSource(); Map<String, Object> attributes = metadata.getAnnotationAttributes(type.getName()); if (null == attributes) { continue; } if (attributeFilter == null || attributeFilter.apply(attributes)) { result.put(name, attributes); } } } return result; }
static Map<Class<? extends Annotation>,AnnotationAttributes> qualifierAttributesFor(final MethodMetadata metadata) { if (metadata instanceof StandardMethodMetadata) { return qualifierFor(metadata, ((StandardMethodMetadata)metadata).getIntrospectedMethod()); } else { if (logger.isDebugEnabled()) { logger.debug(String .format("Found unsupported method meta data %s for method %s.%s", metadata.getClass(), metadata.getDeclaringClassName(),metadata.getMethodName())); } try { // TODO find better way to load the specified @Bean method (ignore parameter etc.) return qualifierFor(metadata, ReflectionUtils.findMethod(Class.forName(metadata.getDeclaringClassName()), metadata.getMethodName(), null)); } catch (final ClassNotFoundException e) { logger.warn(String .format("Cant scan method meta data %s for method %s.%s", metadata.getClass(), metadata.getDeclaringClassName(),metadata.getMethodName()), e); } } return new HashMap<Class<? extends Annotation>, AnnotationAttributes>(); }
private Method getFactoryMethod(ConfigurableListableBeanFactory beanFactory, BeanDefinition definition) throws Exception { if (definition instanceof AnnotatedBeanDefinition) { MethodMetadata factoryMethodMetadata = ((AnnotatedBeanDefinition) definition).getFactoryMethodMetadata(); if (factoryMethodMetadata instanceof StandardMethodMetadata) { return ((StandardMethodMetadata) factoryMethodMetadata).getIntrospectedMethod(); } } BeanDefinition factoryDefinition = beanFactory.getBeanDefinition(definition.getFactoryBeanName()); Class<?> factoryClass = ClassUtils.forName(factoryDefinition.getBeanClassName(), beanFactory.getBeanClassLoader()); return getFactoryMethod(definition, factoryClass); }
private Collection<Object> getTypedBeansWithAnnotation(Class<? extends Annotation> annotationType) throws Exception { return Stream.of(applicationContext.getBeanNamesForAnnotation(annotationType)).filter(name -> { BeanDefinition beanDefinition = applicationContext.getBeanFactory().getBeanDefinition(name); if (beanDefinition.getSource() instanceof StandardMethodMetadata) { StandardMethodMetadata metadata = (StandardMethodMetadata) beanDefinition.getSource(); return metadata.isAnnotated(annotationType.getName()); } return null != applicationContext.getBeanFactory().findAnnotationOnBean(name, annotationType); }).map(name -> applicationContext.getBeanFactory().getBean(name)).collect(Collectors.toList()); }
public void startServer() { preHandle(); ServerBuilder serverBuilder = ServerBuilder.forPort(grpcServerProperties.getPort()); Stream.of(applicationContext.getBeanNamesForType(BindableService.class)).filter(name->{ final BeanDefinition beanDefinition = applicationContext.getBeanFactory().getBeanDefinition(name); final Map<String, Object> beansWithAnnotation = applicationContext.getBeansWithAnnotation(GrpcService.class); if ( !beansWithAnnotation.isEmpty() ) { return beansWithAnnotation.containsKey(name); } else if( beanDefinition.getSource() instanceof StandardMethodMetadata) { StandardMethodMetadata metadata = (StandardMethodMetadata) beanDefinition.getSource(); return metadata.isAnnotated(GrpcService.class.getName()); } return false; }).forEach(name->{ BindableService srv = applicationContext.getBeanFactory().getBean(name, BindableService.class); ServerServiceDefinition serviceDefinition = srv.bindService(); serverBuilder.addService(ServerInterceptors.intercept(serviceDefinition,getInterceptors())); logger.info("GRPC Service add {}",name); }); try { server = serverBuilder.build().start(); logger.info("GRPC Server started at port{}",grpcServerProperties.getPort()); startAwait(); } catch (IOException e) { logger.info(e.getMessage(),e); throw new MSGrpcException("grpc start error:" + e.getMessage()); } }
private void processAnnotationOnMethod(Method method, MethodMetadata.Builder requestTemplateBuilder) { AnnotatedTypeMetadata methodMetadata = new StandardMethodMetadata(method); Parameter[] parameters = method.getParameters(); processRequestMappingAnnotation(methodMetadata, requestTemplateBuilder); for (int i = 0; i < parameters.length; i++) { Parameter parameter = parameters[i]; processAnnotationsOnParameter(requestTemplateBuilder, parameter, i); if (parameter.getAnnotations().length == 0) { requestTemplateBuilder.body(i, parameter.getParameterizedType()); } } }
private <T> Stream<String> getBeanNamesByTypeWithAnnotation(Class<? extends Annotation> annotationType, Class<T> beanType) throws Exception { return Stream.of(applicationContext.getBeanNamesForType(beanType)) .filter(name -> { BeanDefinition beanDefinition = applicationContext.getBeanFactory().getBeanDefinition(name); if (beanDefinition.getSource() instanceof StandardMethodMetadata) { StandardMethodMetadata metadata = (StandardMethodMetadata) beanDefinition.getSource(); return metadata.isAnnotated(annotationType.getName()); } return null != applicationContext.getBeanFactory().findAnnotationOnBean(name, annotationType); }); }
private <T> Stream<String> getBeanNamesByTypeWithAnnotation(Class<? extends Annotation> annotationType, Class<T> beanType) { return Stream.of(SpringContainer.getContext().getBeanNamesForType(beanType)) .filter(name->{ BeanDefinition beanDefinition = SpringContainer.getContext().getBeanFactory().getBeanDefinition(name); if( beanDefinition.getSource() instanceof StandardMethodMetadata) { StandardMethodMetadata metadata = (StandardMethodMetadata) beanDefinition.getSource(); return metadata.isAnnotated(annotationType.getName()); } return null!= SpringContainer.getContext().getBeanFactory().findAnnotationOnBean(name, annotationType); }); }
private String getQualifier(String key) { if (registry != null && registry.containsBeanDefinition(key)) { BeanDefinition beanDefinition = registry.getBeanDefinition(key); Object source = beanDefinition.getSource(); if (source instanceof StandardMethodMetadata) { StandardMethodMetadata metadata = (StandardMethodMetadata) source; Qualifier qualifier = AnnotatedElementUtils.findMergedAnnotation( metadata.getIntrospectedMethod(), Qualifier.class); if (qualifier != null && qualifier.value().length() > 0) { return qualifier.value(); } } } return key; }
/** * Case when the Bean is being declared in a @Configuration, and we cannot * get the BeanClassName directly from the BeanDefinition. * <p> * In this case we need to get the class from the IntrospectedMethod in the * beanDefinition "source" * * @param beanDefinition * @return */ private Class<?> getClassFromMethodMetadata(final BeanDefinition beanDefinition) { final Object source = beanDefinition.getSource(); if (source != null && StandardMethodMetadata.class.isInstance(source)) { final StandardMethodMetadata methodMetadata = StandardMethodMetadata.class.cast(source); final Method introspectedMethod = methodMetadata.getIntrospectedMethod(); if (introspectedMethod != null) { return introspectedMethod.getReturnType(); } } return null; }
/** * Try to obtain the actual bean class of the bean with given name. * @param beanName Bean name * @param beanDefinition Optional bean definition * @param beanFactory Bean factory (not null) * @param classLoader ClassLoader to use * @return The bean class, or <code>null</code> if cannot be detected */ public static Class<?> getBeanClass(String beanName, BeanDefinition beanDefinition, ConfigurableListableBeanFactory beanFactory, ClassLoader classLoader) { ObjectUtils.argumentNotNull(beanFactory, "Bean factory must be not null"); BeanDefinition definition = beanDefinition; if (definition == null && beanName != null) { definition = beanFactory.getBeanDefinition(beanName); } if (definition == null) { throw new BeanDefinitionValidationException("Missing bean definition for bean name [" + beanName + "]"); } // check Root definition if (definition instanceof RootBeanDefinition) { RootBeanDefinition rootBeanDef = (RootBeanDefinition) definition; try { if (rootBeanDef.getBeanClass() != null) { return rootBeanDef.getBeanClass(); } } catch (@SuppressWarnings("unused") IllegalStateException e) { // factory bean: ignore } } // Check factory bean if (definition.getFactoryMethodName() == null) { // factory class if (definition.getBeanClassName() != null) { return getBeanClass(beanName, definition.getBeanClassName(), classLoader); } } else { // factory method String factoryClassName = getBeanFactoryClassName(definition, beanFactory); if (factoryClassName != null) { Class<?> factoryClass = getBeanClass(beanName, factoryClassName, classLoader); if (factoryClass != null) { for (Method method : factoryClass.getMethods()) { if (method.getName().equals(definition.getFactoryMethodName())) { return method.getReturnType(); } } } } } // check beans defined using @Configuration Object source = definition.getSource(); if (source instanceof StandardMethodMetadata) { StandardMethodMetadata metadata = (StandardMethodMetadata) source; return metadata.getIntrospectedMethod().getReturnType(); } return null; }
@Override public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition) { try { Class<?> beanClazz = null; String beanClassName = beanDefinition.getBeanClassName(); // Fix for @Bean annotations in @Configuration will return a "null" beanClassName. This // finds the class anyway: if (beanClassName == null && beanDefinition instanceof AnnotatedBeanDefinition) { MethodMetadata factoryMethodMetadata = ((AnnotatedBeanDefinition) beanDefinition).getFactoryMethodMetadata(); if (factoryMethodMetadata instanceof StandardMethodMetadata) { StandardMethodMetadata methodMetaData = (StandardMethodMetadata) factoryMethodMetadata; beanClazz = methodMetaData.getIntrospectedMethod().getReturnType(); } else { // Try to find the method directly from the defining class/method String declaringClassName = factoryMethodMetadata.getDeclaringClassName(); Class<?> factoryClazz = Class.forName(declaringClassName); // Note that MethodMetadata does not give us the exact method, we have to loop through to find // one which matches the name -- this is not optimal. But it should not matter much, the only thing // which may trip this up is having two classes with same name but different return values which is // not something we would expect in (sane) @Configuration classes. for (Method m : factoryClazz.getDeclaredMethods()) { if (factoryMethodMetadata.getMethodName().equals(m.getName())) { beanClazz = m.getReturnType(); } } } } if (beanClassName != null) { beanClazz = Class.forName(beanClassName); } if (beanClazz != null && isBeanRejected(beanName, beanClazz)) { log.info("Rejected bean [{}] with definiton [{}]", beanName, beanDefinition); } else { if (beanClazz == null) { log.warn("Unable to find beanclass for bean [{}] with definition [{}]", beanName, beanDefinition); } if (remockConfig.getEagerBeanClasses().contains(beanClazz) || remockConfig.getEagerBeanNames().contains(beanName)) { beanDefinition.setLazyInit(false); } else if (!remockConfig.disableLazyInit()) { beanDefinition.setLazyInit(true); } super.registerBeanDefinition(beanName, beanDefinition); } } catch (ClassNotFoundException e) { throw new RuntimeException(e); } }