@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;
}

View File

@@ -66,7 +66,9 @@ public class ScopingTests {
@After
public void tearDown() throws Exception {
ctx.close();
if (ctx != null) {
ctx.close();
}
ctx = null;
customScope = null;
}
@@ -328,7 +330,6 @@ public class ScopingTests {
return tb;
}
@Bean
@MyProxiedScope
public TestBean scopedProxyClass() {
TestBean tb = new TestBean();
@@ -361,6 +362,7 @@ public class ScopingTests {
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Bean
@Scope(value=SCOPE, proxyMode=ScopedProxyMode.TARGET_CLASS)
@interface MyProxiedScope {
}

View File

@@ -0,0 +1,37 @@
/*
* Copyright 2002-2009 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.context.annotation5;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Repository;
/**
* @author Juergen Hoeller
*/
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Repository
@Primary
@Lazy
public @interface MyRepository {
}

View File

@@ -25,8 +25,7 @@ import org.springframework.stereotype.Repository;
/**
* @author Juergen Hoeller
*/
@Repository
@Primary @Lazy
@MyRepository
public class OtherFooDao implements FooDao {
public String findFoo(int id) {