Skip to content

Commit

Permalink
Support meta @component with non-string value
Browse files Browse the repository at this point in the history
Update AnnotationBeanNameGenerator to only use the value attribute of
a @component meta-annotated annotation when it is a String.

Issue: SPR-10580
  • Loading branch information
philwebb committed Jun 4, 2013
1 parent 2655657 commit 4ba43b4
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -116,7 +116,10 @@ protected boolean isStereotypeWithNameValue(String annotationType,
(metaAnnotationTypes != null && metaAnnotationTypes.contains(COMPONENT_ANNOTATION_CLASSNAME)) ||
annotationType.equals("javax.annotation.ManagedBean") ||
annotationType.equals("javax.inject.Named");
return (isStereotype && attributes != null && attributes.containsKey("value"));

return (isStereotype && attributes != null &&
attributes.containsKey("value") &&
attributes.get("value") instanceof String);
}

/**
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,16 +16,21 @@

package org.springframework.context.annotation;

import example.scannable.DefaultNamedComponent;
import org.junit.Test;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.junit.Test;
import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
import org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.SimpleBeanDefinitionRegistry;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import example.scannable.DefaultNamedComponent;
import static org.junit.Assert.*;

/**
Expand Down Expand Up @@ -81,6 +86,22 @@ public void testGenerateBeanNameWithAnonymousComponentYieldsGeneratedBeanName()
assertEquals(expectedGeneratedBeanName, beanName);
}

@Test
public void testGenerateBeanNameFromMetaComponentWithStringValue() {
BeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry();
AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition(ComponentFromStringMeta.class);
String beanName = this.beanNameGenerator.generateBeanName(bd, registry);
assertEquals("henry", beanName);
}

@Test
public void testGenerateBeanNameFromMetaComponentWithNonStringValue() {
BeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry();
AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition(ComponentFromNonStringMeta.class);
String beanName = this.beanNameGenerator.generateBeanName(bd, registry);
assertEquals("annotationBeanNameGeneratorTests.ComponentFromNonStringMeta", beanName);
}


@Component("walden")
private static class ComponentWithName {
Expand All @@ -96,4 +117,19 @@ private static class ComponentWithBlankName {
private static class AnonymousComponent {
}

@Service("henry")
private static class ComponentFromStringMeta {
}

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Component
public @interface NonStringMetaComponent {
long value();
}

@NonStringMetaComponent(123)
private static class ComponentFromNonStringMeta {
}

}

0 comments on commit 4ba43b4

Please sign in to comment.