Skip to content

Commit 4ba43b4

Browse files
committed
Support meta @component with non-string value
Update AnnotationBeanNameGenerator to only use the value attribute of a @component meta-annotated annotation when it is a String. Issue: SPR-10580
1 parent 2655657 commit 4ba43b4

File tree

2 files changed

+44
-5
lines changed

2 files changed

+44
-5
lines changed

spring-context/src/main/java/org/springframework/context/annotation/AnnotationBeanNameGenerator.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2013 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -116,7 +116,10 @@ protected boolean isStereotypeWithNameValue(String annotationType,
116116
(metaAnnotationTypes != null && metaAnnotationTypes.contains(COMPONENT_ANNOTATION_CLASSNAME)) ||
117117
annotationType.equals("javax.annotation.ManagedBean") ||
118118
annotationType.equals("javax.inject.Named");
119-
return (isStereotype && attributes != null && attributes.containsKey("value"));
119+
120+
return (isStereotype && attributes != null &&
121+
attributes.containsKey("value") &&
122+
attributes.get("value") instanceof String);
120123
}
121124

122125
/**

spring-context/src/test/java/org/springframework/context/annotation/AnnotationBeanNameGeneratorTests.java

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2013 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,16 +16,21 @@
1616

1717
package org.springframework.context.annotation;
1818

19-
import example.scannable.DefaultNamedComponent;
20-
import org.junit.Test;
19+
import java.lang.annotation.ElementType;
20+
import java.lang.annotation.Retention;
21+
import java.lang.annotation.RetentionPolicy;
22+
import java.lang.annotation.Target;
2123

24+
import org.junit.Test;
2225
import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
2326
import org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition;
2427
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
2528
import org.springframework.beans.factory.support.SimpleBeanDefinitionRegistry;
2629
import org.springframework.stereotype.Component;
30+
import org.springframework.stereotype.Service;
2731
import org.springframework.util.StringUtils;
2832

33+
import example.scannable.DefaultNamedComponent;
2934
import static org.junit.Assert.*;
3035

3136
/**
@@ -81,6 +86,22 @@ public void testGenerateBeanNameWithAnonymousComponentYieldsGeneratedBeanName()
8186
assertEquals(expectedGeneratedBeanName, beanName);
8287
}
8388

89+
@Test
90+
public void testGenerateBeanNameFromMetaComponentWithStringValue() {
91+
BeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry();
92+
AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition(ComponentFromStringMeta.class);
93+
String beanName = this.beanNameGenerator.generateBeanName(bd, registry);
94+
assertEquals("henry", beanName);
95+
}
96+
97+
@Test
98+
public void testGenerateBeanNameFromMetaComponentWithNonStringValue() {
99+
BeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry();
100+
AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition(ComponentFromNonStringMeta.class);
101+
String beanName = this.beanNameGenerator.generateBeanName(bd, registry);
102+
assertEquals("annotationBeanNameGeneratorTests.ComponentFromNonStringMeta", beanName);
103+
}
104+
84105

85106
@Component("walden")
86107
private static class ComponentWithName {
@@ -96,4 +117,19 @@ private static class ComponentWithBlankName {
96117
private static class AnonymousComponent {
97118
}
98119

120+
@Service("henry")
121+
private static class ComponentFromStringMeta {
122+
}
123+
124+
@Retention(RetentionPolicy.RUNTIME)
125+
@Target(ElementType.TYPE)
126+
@Component
127+
public @interface NonStringMetaComponent {
128+
long value();
129+
}
130+
131+
@NonStringMetaComponent(123)
132+
private static class ComponentFromNonStringMeta {
133+
}
134+
99135
}

0 commit comments

Comments
 (0)