@Override
  public void registerBeanDefinitions(AnnotationMetadata meta, BeanDefinitionRegistry registry) {
    Map<String, Object> attrs = meta.getAnnotationAttributes(EnableReactor.class.getName());

    // Create a root Enivronment
    if (!registry.containsBeanDefinition(Environment.class.getName())) {
      BeanDefinitionBuilder envBeanDef =
          BeanDefinitionBuilder.rootBeanDefinition(Environment.class);

      String configReaderBean = (String) attrs.get("configurationReader");
      if (StringUtils.hasText(configReaderBean)) {
        envBeanDef.addConstructorArgReference(configReaderBean);
      } else {
        String profileName = (String) attrs.get("value");
        if (StringUtils.hasText(profileName)) {
          envBeanDef.addConstructorArgValue(new PropertiesConfigurationReader(profileName));
        }
      }
      registry.registerBeanDefinition(Environment.class.getName(), envBeanDef.getBeanDefinition());
    }

    // Create a ConsumerBeanPostProcessor
    if (!registry.containsBeanDefinition(ConsumerBeanPostProcessor.class.getName())) {
      BeanDefinitionBuilder envBeanDef =
          BeanDefinitionBuilder.rootBeanDefinition(ConsumerBeanPostProcessor.class);
      registry.registerBeanDefinition(
          ConsumerBeanPostProcessor.class.getName(), envBeanDef.getBeanDefinition());
    }
  }
  @Override
  public String[] selectImports(AnnotationMetadata metadata) {
    try {
      AnnotationAttributes attributes =
          AnnotationAttributes.fromMap(
              metadata.getAnnotationAttributes(EnableAutoConfiguration.class.getName(), true));

      Assert.notNull(
          attributes,
          "No auto-configuration attributes found. Is "
              + metadata.getClassName()
              + " annotated with @EnableAutoConfiguration?");

      // Find all possible auto configuration classes, filtering duplicates
      List<String> factories =
          new ArrayList<String>(
              new LinkedHashSet<String>(
                  SpringFactoriesLoader.loadFactoryNames(
                      EnableAutoConfiguration.class, this.beanClassLoader)));

      // Remove those specifically excluded
      Set<String> excluded = new LinkedHashSet<String>();
      excluded.addAll(Arrays.asList(attributes.getStringArray("exclude")));
      excluded.addAll(Arrays.asList(attributes.getStringArray("excludeName")));
      factories.removeAll(excluded);
      ConditionEvaluationReport.get(this.beanFactory).recordExclusions(excluded);

      // Sort
      factories = new AutoConfigurationSorter(this.resourceLoader).getInPriorityOrder(factories);

      return factories.toArray(new String[factories.size()]);
    } catch (IOException ex) {
      throw new IllegalStateException(ex);
    }
  }
예제 #3
0
 @Override
 public void setImportMetadata(AnnotationMetadata importMetadata) {
   Map<String, Object> elements =
       importMetadata.getAnnotationAttributes(EnableHello.class.getName());
   String name = (String) elements.get("name");
   hello().setName(name);
 }
  /*
   * (non-Javadoc)
   * @see org.springframework.context.annotation.ImportBeanDefinitionRegistrar#registerBeanDefinitions(org.springframework.core.type.AnnotationMetadata, org.springframework.beans.factory.support.BeanDefinitionRegistry)
   */
  @Override
  public void registerBeanDefinitions(
      AnnotationMetadata metadata, BeanDefinitionRegistry registry) {

    linkBuilderBeanDefinitionRegistrar.registerBeanDefinitions(metadata, registry);

    Map<String, Object> attributes =
        metadata.getAnnotationAttributes(EnableHypermediaSupport.class.getName());
    Collection<HypermediaType> types = Arrays.asList((HypermediaType[]) attributes.get("type"));

    for (HypermediaType type : types) {

      if (JSONPATH_PRESENT) {

        AbstractBeanDefinition linkDiscovererBeanDefinition = getLinkDiscovererBeanDefinition(type);
        registerBeanDefinition(
            new BeanDefinitionHolder(
                linkDiscovererBeanDefinition,
                BeanDefinitionReaderUtils.generateBeanName(linkDiscovererBeanDefinition, registry)),
            registry);
      }
    }

    if (types.contains(HypermediaType.HAL)) {

      if (JACKSON2_PRESENT) {

        BeanDefinitionBuilder halQueryMapperBuilder = rootBeanDefinition(ObjectMapper.class);
        registerSourcedBeanDefinition(
            halQueryMapperBuilder, metadata, registry, HAL_OBJECT_MAPPER_BEAN_NAME);

        BeanDefinitionBuilder customizerBeanDefinition =
            rootBeanDefinition(DefaultObjectMapperCustomizer.class);
        registerSourcedBeanDefinition(customizerBeanDefinition, metadata, registry);

        BeanDefinitionBuilder builder =
            rootBeanDefinition(Jackson2ModuleRegisteringBeanPostProcessor.class);
        registerSourcedBeanDefinition(builder, metadata, registry);
      }
    }

    if (!types.isEmpty()) {

      BeanDefinitionBuilder linkDiscoverersRegistryBuilder =
          BeanDefinitionBuilder.rootBeanDefinition(PluginRegistryFactoryBean.class);
      linkDiscoverersRegistryBuilder.addPropertyValue("type", LinkDiscoverer.class);
      registerSourcedBeanDefinition(
          linkDiscoverersRegistryBuilder, metadata, registry, LINK_DISCOVERER_REGISTRY_BEAN_NAME);

      BeanDefinitionBuilder linkDiscoverersBuilder =
          BeanDefinitionBuilder.rootBeanDefinition(LinkDiscoverers.class);
      linkDiscoverersBuilder.addConstructorArgReference(LINK_DISCOVERER_REGISTRY_BEAN_NAME);
      registerSourcedBeanDefinition(linkDiscoverersBuilder, metadata, registry);
    }

    registerRelProviderPluginRegistryAndDelegate(registry);
  }
예제 #5
0
  @Override
  protected BeanDefinition buildBeanDefinition(
      AnnotationMetadata importingClassMetadata, Class<? extends Annotation> namedAnnotation)
      throws Exception {

    String enableStateMachineEnclosingClassName = importingClassMetadata.getClassName();
    // for below classloader, see gh122
    Class<?> enableStateMachineEnclosingClass =
        ClassUtils.forName(
            enableStateMachineEnclosingClassName, ClassUtils.getDefaultClassLoader());
    // return null if it looks like @EnableStateMachine was annotated with class
    // not extending StateMachineConfigurer.
    if (!ClassUtils.isAssignable(StateMachineConfigurer.class, enableStateMachineEnclosingClass)) {
      return null;
    }

    BeanDefinitionBuilder beanDefinitionBuilder =
        BeanDefinitionBuilder.rootBeanDefinition(StateMachineDelegatingFactoryBean.class);
    AnnotationAttributes esmAttributes =
        AnnotationAttributes.fromMap(
            importingClassMetadata.getAnnotationAttributes(
                EnableStateMachine.class.getName(), false));
    Boolean contextEvents = esmAttributes.getBoolean("contextEvents");

    // check if Scope annotation is defined and set scope from it
    AnnotationAttributes scopeAttributes =
        AnnotationAttributes.fromMap(
            importingClassMetadata.getAnnotationAttributes(Scope.class.getName(), false));
    if (scopeAttributes != null) {
      String scope =
          scopeAttributes.getAliasedString("value", Scope.class, enableStateMachineEnclosingClass);
      if (StringUtils.hasText(scope)) {
        beanDefinitionBuilder.setScope(scope);
      }
    }

    beanDefinitionBuilder.addConstructorArgValue(builder);
    beanDefinitionBuilder.addConstructorArgValue(StateMachine.class);
    beanDefinitionBuilder.addConstructorArgValue(importingClassMetadata.getClassName());
    beanDefinitionBuilder.addConstructorArgValue(contextEvents);
    return beanDefinitionBuilder.getBeanDefinition();
  }
 @Override
 public void setImportMetadata(AnnotationMetadata importMetadata) {
   AnnotationAttributes annotationAttributes =
       AnnotationAttributes.fromMap(
           importMetadata.getAnnotationAttributes(EnableRoleChecking.class.getName()));
   Assert.notNull(
       annotationAttributes,
       "No "
           + EnableRoleChecking.class.getSimpleName()
           + " annotation found on  '"
           + importMetadata.getClassName()
           + "'.");
 }
 private Set<String> getPackagesToScan(AnnotationMetadata metadata) {
   AnnotationAttributes attributes =
       AnnotationAttributes.fromMap(
           metadata.getAnnotationAttributes(EntityScan.class.getName()));
   String[] basePackages =
       attributes.getAliasedStringArray(
           "basePackages", EntityScan.class, metadata.getClassName());
   Class<?>[] basePackageClasses = attributes.getClassArray("basePackageClasses");
   Set<String> packagesToScan = new LinkedHashSet<String>();
   packagesToScan.addAll(Arrays.asList(basePackages));
   for (Class<?> basePackageClass : basePackageClasses) {
     packagesToScan.add(ClassUtils.getPackageName(basePackageClass));
   }
   if (packagesToScan.isEmpty()) {
     String packageName = ClassUtils.getPackageName(metadata.getClassName());
     Assert.state(
         !StringUtils.isEmpty(packageName),
         "@EntityScan cannot be used with the default package");
     return Collections.singleton(packageName);
   }
   return packagesToScan;
 }
 public void registerBeanDefinitions(
     AnnotationMetadata annotationMetadata, BeanDefinitionRegistry registry) {
   Map<String, Object> annotationAttributes =
       annotationMetadata.getAnnotationAttributes(EnableJdbcConnectionRepository.class.getName());
   if (annotationAttributes == null) {
     return;
   }
   AnnotationAttributes attributes = new AnnotationAttributes(annotationAttributes);
   String connectionRepositoryId = attributes.getString("connectionRepositoryId");
   String usersConnectionRepositoryId = attributes.getString("usersConnectionRepositoryId");
   String connectionFactoryLocatorRef = attributes.getString("connectionFactoryLocatorRef");
   String dataSourceRef = attributes.getString("dataSourceRef");
   String encryptorRef = attributes.getString("encryptorRef");
   String userIdSourceRef = attributes.getString("userIdSourceRef");
   registerJdbcConnectionRepositoryBeans(
       registry,
       connectionRepositoryId,
       usersConnectionRepositoryId,
       connectionFactoryLocatorRef,
       dataSourceRef,
       encryptorRef,
       userIdSourceRef);
 }
 /**
  * Derive a bean name from one of the annotations on the class.
  *
  * @param annotatedDef the annotation-aware bean definition
  * @return the bean name, or <code>null</code> if none is found
  */
 protected String determineBeanNameFromAnnotation(AnnotatedBeanDefinition annotatedDef) {
   AnnotationMetadata amd = annotatedDef.getMetadata();
   Set<String> types = amd.getAnnotationTypes();
   String beanName = null;
   for (String type : types) {
     Map<String, Object> attributes = amd.getAnnotationAttributes(type);
     if (isStereotypeWithNameValue(type, amd.getMetaAnnotationTypes(type), attributes)) {
       String value = (String) attributes.get("value");
       if (StringUtils.hasLength(value)) {
         if (beanName != null && !value.equals(beanName)) {
           throw new IllegalStateException(
               "Stereotype annotations suggest inconsistent "
                   + "component names: '"
                   + beanName
                   + "' versus '"
                   + value
                   + "'");
         }
         beanName = value;
       }
     }
   }
   return beanName;
 }
  public void setImportMetadata(AnnotationMetadata importMetadata) {
    Map<String, Object> enableAttrMap =
        importMetadata.getAnnotationAttributes(EnableHazelcastHttpSession.class.getName());
    AnnotationAttributes enableAttrs = AnnotationAttributes.fromMap(enableAttrMap);
    if (enableAttrs == null) {
      // search parent classes
      Class<?> currentClass =
          ClassUtils.resolveClassName(importMetadata.getClassName(), beanClassLoader);
      for (Class<?> classToInspect = currentClass;
          classToInspect != null;
          classToInspect = classToInspect.getSuperclass()) {
        EnableHazelcastHttpSession enableHazelcastHttpSessionAnnotation =
            AnnotationUtils.findAnnotation(classToInspect, EnableHazelcastHttpSession.class);
        if (enableHazelcastHttpSessionAnnotation == null) {
          continue;
        }
        enableAttrMap =
            AnnotationUtils.getAnnotationAttributes(enableHazelcastHttpSessionAnnotation);
        enableAttrs = AnnotationAttributes.fromMap(enableAttrMap);
      }
    }

    transferAnnotationAttributes(enableAttrs);
  }
  protected void doProcessConfigurationClass(
      ConfigurationClass configClass, AnnotationMetadata metadata) throws IOException {

    // recursively process any member (nested) classes first
    for (String memberClassName : metadata.getMemberClassNames()) {
      MetadataReader reader = this.metadataReaderFactory.getMetadataReader(memberClassName);
      AnnotationMetadata memberClassMetadata = reader.getAnnotationMetadata();
      if (isConfigurationCandidate(memberClassMetadata)) {
        processConfigurationClass(new ConfigurationClass(reader, null));
      }
    }

    // process any @PropertySource annotations
    Map<String, Object> propertySourceAttributes =
        metadata.getAnnotationAttributes(
            org.springframework.context.annotation.PropertySource.class.getName());
    if (propertySourceAttributes != null) {
      String name = (String) propertySourceAttributes.get("name");
      String[] locations = (String[]) propertySourceAttributes.get("value");
      ClassLoader classLoader = this.resourceLoader.getClassLoader();
      for (String location : locations) {
        location = this.environment.resolveRequiredPlaceholders(location);
        ResourcePropertySource ps =
            StringUtils.hasText(name)
                ? new ResourcePropertySource(name, location, classLoader)
                : new ResourcePropertySource(location, classLoader);
        this.propertySources.push(ps);
      }
    }

    // process any @ComponentScan annotions
    Map<String, Object> componentScanAttributes =
        metadata.getAnnotationAttributes(ComponentScan.class.getName());
    if (componentScanAttributes != null) {
      // the config class is annotated with @ComponentScan -> perform the scan immediately
      Set<BeanDefinitionHolder> scannedBeanDefinitions =
          this.componentScanParser.parse(componentScanAttributes);

      // check the set of scanned definitions for any further config classes and parse recursively
      // if necessary
      for (BeanDefinitionHolder holder : scannedBeanDefinitions) {
        if (ConfigurationClassUtils.checkConfigurationClassCandidate(
            holder.getBeanDefinition(), metadataReaderFactory)) {
          try {
            this.parse(holder.getBeanDefinition().getBeanClassName(), holder.getBeanName());
          } catch (ConflictingBeanDefinitionException ex) {
            throw new CircularComponentScanException(
                "A conflicting bean definition was detected while processing @ComponentScan annotations. "
                    + "This usually indicates a circle between scanned packages.",
                ex);
          }
        }
      }
    }

    // process any @Import annotations
    List<Map<String, Object>> allImportAttribs =
        AnnotationUtils.findAllAnnotationAttributes(Import.class, metadata.getClassName(), true);
    for (Map<String, Object> importAttribs : allImportAttribs) {
      processImport(configClass, (String[]) importAttribs.get("value"), true);
    }

    // process any @ImportResource annotations
    if (metadata.isAnnotated(ImportResource.class.getName())) {
      String[] resources =
          (String[]) metadata.getAnnotationAttributes(ImportResource.class.getName()).get("value");
      Class<?> readerClass =
          (Class<?>) metadata.getAnnotationAttributes(ImportResource.class.getName()).get("reader");
      if (readerClass == null) {
        throw new IllegalStateException(
            "No reader class associated with imported resources: "
                + StringUtils.arrayToCommaDelimitedString(resources));
      }
      for (String resource : resources) {
        configClass.addImportedResource(resource, readerClass);
      }
    }

    // process individual @Bean methods
    Set<MethodMetadata> beanMethods = metadata.getAnnotatedMethods(Bean.class.getName());
    for (MethodMetadata methodMetadata : beanMethods) {
      configClass.addBeanMethod(new BeanMethod(methodMetadata, configClass));
    }
  }
예제 #12
0
 public static AnnotationAttributes attributesFor(
     AnnotationMetadata metadata, String annoClassName) {
   return AnnotationAttributes.fromMap(metadata.getAnnotationAttributes(annoClassName, false));
 }