示例#1
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();
  }
  @Test
  public void overriddenContextConfigurationLocationsAndInheritLocations() throws Exception {
    Class<?> declaringClass = OverriddenMetaLocationsConfigTestCase.class;
    AnnotationDescriptor<ContextConfiguration> descriptor =
        findAnnotationDescriptor(declaringClass, ContextConfiguration.class);
    assertNotNull(descriptor);
    assertEquals(declaringClass, descriptor.getRootDeclaringClass());
    assertEquals(MetaLocationsConfig.class, descriptor.getComposedAnnotationType());
    assertEquals(ContextConfiguration.class, descriptor.getAnnotationType());
    assertNotNull(descriptor.getComposedAnnotation());
    assertEquals(MetaLocationsConfig.class, descriptor.getComposedAnnotationType());

    // direct access to annotation attributes:
    assertArrayEquals(new String[] {"foo.xml"}, descriptor.getAnnotation().locations());
    assertFalse(descriptor.getAnnotation().inheritLocations());

    // overridden attributes:
    AnnotationAttributes attributes = descriptor.getAnnotationAttributes();
    assertArrayEquals(new String[] {"bar.xml"}, attributes.getStringArray("locations"));
    assertTrue(attributes.getBoolean("inheritLocations"));
  }
 /**
  * Process the given <code>@PropertySource</code> annotation metadata.
  *
  * @param propertySource metadata for the <code>@PropertySource</code> annotation found
  * @throws IOException if loading a property source failed
  */
 private void processPropertySource(AnnotationAttributes propertySource) throws IOException {
   String name = propertySource.getString("name");
   String encoding = propertySource.getString("encoding");
   String[] locations = propertySource.getStringArray("value");
   boolean ignoreResourceNotFound = propertySource.getBoolean("ignoreResourceNotFound");
   Assert.isTrue(locations.length > 0, "At least one @PropertySource(value) location is required");
   for (String location : locations) {
     try {
       String resolvedLocation = this.environment.resolveRequiredPlaceholders(location);
       Resource resource = this.resourceLoader.getResource(resolvedLocation);
       addPropertySource(createPropertySource(name, encoding, resource));
     } catch (IllegalArgumentException ex) {
       // from resolveRequiredPlaceholders
       if (!ignoreResourceNotFound) {
         throw ex;
       }
     } catch (FileNotFoundException ex) {
       // from ResourcePropertySource constructor
       if (!ignoreResourceNotFound) {
         throw ex;
       }
     }
   }
 }