@Primary/@Lazy/@DependsOn supported as meta-annotations; @Bean supported as meta-annotation on factory methods as well

This commit is contained in:
Juergen Hoeller
2009-07-22 15:23:22 +00:00
parent a4bbd9abda
commit f519406c37
17 changed files with 156 additions and 123 deletions

View File

@@ -205,14 +205,14 @@ public class ClassPathBeanDefinitionScanner extends ClassPathScanningCandidateCo
}
if (candidate instanceof AnnotatedBeanDefinition) {
AnnotatedBeanDefinition abd = (AnnotatedBeanDefinition) candidate;
if (abd.getMetadata().hasAnnotation(Primary.class.getName())) {
if (abd.getMetadata().isAnnotated(Primary.class.getName())) {
abd.setPrimary(true);
}
if (abd.getMetadata().hasAnnotation(Lazy.class.getName())) {
if (abd.getMetadata().isAnnotated(Lazy.class.getName())) {
Boolean value = (Boolean) abd.getMetadata().getAnnotationAttributes(Lazy.class.getName()).get("value");
abd.setLazyInit(value);
}
if (abd.getMetadata().hasAnnotation(DependsOn.class.getName())) {
if (abd.getMetadata().isAnnotated(DependsOn.class.getName())) {
String[] value = (String[]) abd.getMetadata().getAnnotationAttributes(DependsOn.class.getName()).get("value");
abd.setDependsOn(value);
}

View File

@@ -116,7 +116,7 @@ final class ConfigurationClass {
}
// A configuration class may not be final (CGLIB limitation)
if (getMetadata().hasAnnotation(Configuration.class.getName())) {
if (getMetadata().isAnnotated(Configuration.class.getName())) {
if (getMetadata().isFinal()) {
problemReporter.error(new FinalConfigurationProblem());
}

View File

@@ -36,6 +36,7 @@ import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.core.io.Resource;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.MethodMetadata;
@@ -128,11 +129,11 @@ class ConfigurationClassBeanDefinitionReader {
List<String> names = new ArrayList<String>(Arrays.asList((String[]) beanAttributes.get("name")));
String beanName = (names.size() > 0 ? names.remove(0) : method.getMetadata().getMethodName());
for (String alias : names) {
registry.registerAlias(beanName, alias);
this.registry.registerAlias(beanName, alias);
}
// has this already been overriden (i.e.: via XML)?
if (registry.containsBeanDefinition(beanName)) {
if (this.registry.containsBeanDefinition(beanName)) {
BeanDefinition existingBeanDef = registry.getBeanDefinition(beanName);
// is the existing bean definition one that was created from a configuration class?
if (!(existingBeanDef instanceof ConfigurationClassBeanDefinition)) {
@@ -146,19 +147,19 @@ class ConfigurationClassBeanDefinitionReader {
}
}
if (metadata.hasAnnotation(Primary.class.getName())) {
if (metadata.isAnnotated(Primary.class.getName())) {
beanDef.setPrimary(true);
}
// is this bean to be instantiated lazily?
if (metadata.hasAnnotation(Lazy.class.getName())) {
if (metadata.isAnnotated(Lazy.class.getName())) {
beanDef.setLazyInit((Boolean) metadata.getAnnotationAttributes(Lazy.class.getName()).get("value"));
}
else if (configClass.getMetadata().hasAnnotation(Lazy.class.getName())){
else if (configClass.getMetadata().isAnnotated(Lazy.class.getName())){
beanDef.setLazyInit((Boolean) configClass.getMetadata().getAnnotationAttributes(Lazy.class.getName()).get("value"));
}
if (metadata.hasAnnotation(DependsOn.class.getName())) {
if (metadata.isAnnotated(DependsOn.class.getName())) {
String[] dependsOn = (String[]) metadata.getAnnotationAttributes(DependsOn.class.getName()).get("value");
if (dependsOn.length > 0) {
beanDef.setDependsOn(dependsOn);
@@ -232,7 +233,7 @@ class ConfigurationClassBeanDefinitionReader {
@Override
public boolean isFactoryMethod(Method candidate) {
return (super.isFactoryMethod(candidate) && candidate.isAnnotationPresent(Bean.class));
return (super.isFactoryMethod(candidate) && AnnotationUtils.findAnnotation(candidate, Bean.class) != null);
}
@Override

View File

@@ -57,7 +57,7 @@ final class ConfigurationClassMethod {
}
public void validate(ProblemReporter problemReporter) {
if (this.declaringClass.getMetadata().hasAnnotation(Configuration.class.getName()) && !getMetadata().isOverridable()) {
if (this.declaringClass.getMetadata().isAnnotated(Configuration.class.getName()) && !getMetadata().isOverridable()) {
problemReporter.error(new NonOverridableMethodError());
}
}

View File

@@ -121,7 +121,7 @@ class ConfigurationClassParser {
}
protected void doProcessConfigurationClass(ConfigurationClass configClass, AnnotationMetadata metadata) throws IOException {
if (metadata.hasAnnotation(Import.class.getName())) {
if (metadata.isAnnotated(Import.class.getName())) {
processImport(configClass, (String[]) metadata.getAnnotationAttributes(Import.class.getName()).get("value"));
}
Set<MethodMetadata> methods = metadata.getAnnotatedMethods(Bean.class.getName());
@@ -146,7 +146,7 @@ class ConfigurationClassParser {
private void processClassToImport(String classToImport) throws IOException {
MetadataReader reader = this.metadataReaderFactory.getMetadataReader(classToImport);
AnnotationMetadata metadata = reader.getAnnotationMetadata();
if (!metadata.hasAnnotation(Configuration.class.getName())) {
if (!metadata.isAnnotated(Configuration.class.getName())) {
this.problemReporter.error(
new NonAnnotatedConfigurationProblem(metadata.getClassName(), reader.getResource(), metadata));
}

View File

@@ -218,11 +218,11 @@ public class ConfigurationClassPostProcessor implements BeanFactoryPostProcessor
}
if (metadata != null) {
if (metadata.hasAnnotation(Configuration.class.getName())) {
if (metadata.isAnnotated(Configuration.class.getName())) {
beanDef.setAttribute(CONFIGURATION_CLASS_ATTRIBUTE, CONFIGURATION_CLASS_FULL);
return true;
}
else if (metadata.hasAnnotation(Component.class.getName())) {
else if (metadata.isAnnotated(Component.class.getName())) {
beanDef.setAttribute(CONFIGURATION_CLASS_ATTRIBUTE, CONFIGURATION_CLASS_LITE);
return true;
}