Browse Source

Complete set of registerBean methods on AnnotatedBeanDefinitionReader

Includes bringing registerBean constructor-vararg variants up to GenericApplicationContext, making AnnotationConfigApplicationContext a straightforward subclass with a single template method to override.

See gh-22457
pull/22752/head
Juergen Hoeller 7 years ago
parent
commit
9de83674cd
  1. 101
      spring-context/src/main/java/org/springframework/context/annotation/AnnotatedBeanDefinitionReader.java
  2. 47
      spring-context/src/main/java/org/springframework/context/annotation/AnnotationConfigApplicationContext.java
  3. 49
      spring-context/src/main/java/org/springframework/context/support/GenericApplicationContext.java

101
spring-context/src/main/java/org/springframework/context/annotation/AnnotatedBeanDefinitionReader.java

@ -143,34 +143,19 @@ public class AnnotatedBeanDefinitionReader { @@ -143,34 +143,19 @@ public class AnnotatedBeanDefinitionReader {
* @param annotatedClass the class of the bean
*/
public void registerBean(Class<?> annotatedClass) {
doRegisterBean(annotatedClass, null, null, null);
doRegisterBean(annotatedClass, null, null, null, null);
}
/**
* Register a bean from the given bean class, deriving its metadata from
* class-declared annotations, using the given supplier for obtaining a new
* instance (possibly declared as a lambda expression or method reference).
* @param annotatedClass the class of the bean
* @param instanceSupplier a callback for creating an instance of the bean
* (may be {@code null})
* @since 5.0
*/
public <T> void registerBean(Class<T> annotatedClass, @Nullable Supplier<T> instanceSupplier) {
doRegisterBean(annotatedClass, instanceSupplier, null, null);
}
/**
* Register a bean from the given bean class, deriving its metadata from
* class-declared annotations, using the given supplier for obtaining a new
* instance (possibly declared as a lambda expression or method reference).
* class-declared annotations.
* @param annotatedClass the class of the bean
* @param name an explicit name for the bean
* @param instanceSupplier a callback for creating an instance of the bean
* (may be {@code null})
* @since 5.0
* (or {@code null} for generating a default bean name)
* @since 5.2
*/
public <T> void registerBean(Class<T> annotatedClass, String name, @Nullable Supplier<T> instanceSupplier) {
doRegisterBean(annotatedClass, instanceSupplier, name, null);
public void registerBean(Class<?> annotatedClass, @Nullable String name) {
doRegisterBean(annotatedClass, name, null, null, null);
}
/**
@ -182,7 +167,7 @@ public class AnnotatedBeanDefinitionReader { @@ -182,7 +167,7 @@ public class AnnotatedBeanDefinitionReader {
*/
@SuppressWarnings("unchecked")
public void registerBean(Class<?> annotatedClass, Class<? extends Annotation>... qualifiers) {
doRegisterBean(annotatedClass, null, null, qualifiers);
doRegisterBean(annotatedClass, null, qualifiers, null, null);
}
/**
@ -190,36 +175,86 @@ public class AnnotatedBeanDefinitionReader { @@ -190,36 +175,86 @@ public class AnnotatedBeanDefinitionReader {
* class-declared annotations.
* @param annotatedClass the class of the bean
* @param name an explicit name for the bean
* (or {@code null} for generating a default bean name)
* @param qualifiers specific qualifier annotations to consider,
* in addition to qualifiers at the bean class level
*/
@SuppressWarnings("unchecked")
public void registerBean(Class<?> annotatedClass, String name, Class<? extends Annotation>... qualifiers) {
doRegisterBean(annotatedClass, null, name, qualifiers);
public void registerBean(Class<?> annotatedClass, @Nullable String name,
Class<? extends Annotation>... qualifiers) {
doRegisterBean(annotatedClass, name, qualifiers, null, null);
}
/**
* Register a bean from the given bean class, deriving its metadata from
* class-declared annotations, using the given supplier for obtaining a new
* instance (possibly declared as a lambda expression or method reference).
* @param annotatedClass the class of the bean
* @param supplier a callback for creating an instance of the bean
* (may be {@code null})
* @since 5.0
*/
public <T> void registerBean(Class<T> annotatedClass, @Nullable Supplier<T> supplier) {
doRegisterBean(annotatedClass, null, null, supplier, null);
}
/**
* Register a bean from the given bean class, deriving its metadata from
* class-declared annotations, using the given supplier for obtaining a new
* instance (possibly declared as a lambda expression or method reference).
* @param annotatedClass the class of the bean
* @param name an explicit name for the bean
* (or {@code null} for generating a default bean name)
* @param supplier a callback for creating an instance of the bean
* (may be {@code null})
* @since 5.0
*/
public <T> void registerBean(Class<T> annotatedClass, @Nullable String name, @Nullable Supplier<T> supplier) {
doRegisterBean(annotatedClass, name, null, supplier, null);
}
/**
* Register a bean from the given bean class, deriving its metadata from
* class-declared annotations.
* @param annotatedClass the class of the bean
* @param instanceSupplier a callback for creating an instance of the bean
* @param name an explicit name for the bean
* (or {@code null} for generating a default bean name)
* @param supplier a callback for creating an instance of the bean
* (may be {@code null})
* @param customizers one or more callbacks for customizing the factory's
* {@link BeanDefinition}, e.g. setting a lazy-init or primary flag
* @since 5.2
*/
public <T> void registerBean(Class<T> annotatedClass, @Nullable String name, @Nullable Supplier<T> supplier,
BeanDefinitionCustomizer... customizers) {
doRegisterBean(annotatedClass, name, null, supplier, customizers);
}
/**
* Register a bean from the given bean class, deriving its metadata from
* class-declared annotations.
* @param annotatedClass the class of the bean
* @param name an explicit name for the bean
* @param supplier a callback for creating an instance of the bean
* (may be {@code null})
* @param qualifiers specific qualifier annotations to consider, if any,
* in addition to qualifiers at the bean class level
* @param definitionCustomizers one or more callbacks for customizing the
* factory's {@link BeanDefinition}, e.g. setting a lazy-init or primary flag
* @param customizers one or more callbacks for customizing the factory's
* {@link BeanDefinition}, e.g. setting a lazy-init or primary flag
* @since 5.0
*/
<T> void doRegisterBean(Class<T> annotatedClass, @Nullable Supplier<T> instanceSupplier, @Nullable String name,
@Nullable Class<? extends Annotation>[] qualifiers, BeanDefinitionCustomizer... definitionCustomizers) {
private <T> void doRegisterBean(Class<T> annotatedClass, @Nullable String name,
@Nullable Class<? extends Annotation>[] qualifiers, @Nullable Supplier<T> supplier,
@Nullable BeanDefinitionCustomizer[] customizers) {
AnnotatedGenericBeanDefinition abd = new AnnotatedGenericBeanDefinition(annotatedClass);
if (this.conditionEvaluator.shouldSkip(abd.getMetadata())) {
return;
}
abd.setInstanceSupplier(instanceSupplier);
abd.setInstanceSupplier(supplier);
ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(abd);
abd.setScope(scopeMetadata.getScopeName());
String beanName = (name != null ? name : this.beanNameGenerator.generateBeanName(abd, this.registry));
@ -238,8 +273,10 @@ public class AnnotatedBeanDefinitionReader { @@ -238,8 +273,10 @@ public class AnnotatedBeanDefinitionReader {
}
}
}
for (BeanDefinitionCustomizer customizer : definitionCustomizers) {
customizer.customize(abd);
if (customizers != null) {
for (BeanDefinitionCustomizer customizer : customizers) {
customizer.customize(abd);
}
}
BeanDefinitionHolder definitionHolder = new BeanDefinitionHolder(abd, beanName);

47
spring-context/src/main/java/org/springframework/context/annotation/AnnotationConfigApplicationContext.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 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.
@ -172,51 +172,14 @@ public class AnnotationConfigApplicationContext extends GenericApplicationContex @@ -172,51 +172,14 @@ public class AnnotationConfigApplicationContext extends GenericApplicationContex
//---------------------------------------------------------------------
// Convenient methods for registering individual beans
// Adapt superclass registerBean calls to AnnotatedBeanDefinitionReader
//---------------------------------------------------------------------
/**
* Register a bean from the given bean class, deriving its metadata from
* class-declared annotations, and optionally providing explicit constructor
* arguments for consideration in the autowiring process.
* <p>The bean name will be generated according to annotated component rules.
* @param annotatedClass the class of the bean
* @param constructorArguments argument values to be fed into Spring's
* constructor resolution algorithm, resolving either all arguments or just
* specific ones, with the rest to be resolved through regular autowiring
* (may be {@code null} or empty)
* @since 5.0
*/
public <T> void registerBean(Class<T> annotatedClass, Object... constructorArguments) {
registerBean(null, annotatedClass, constructorArguments);
}
/**
* Register a bean from the given bean class, deriving its metadata from
* class-declared annotations, and optionally providing explicit constructor
* arguments for consideration in the autowiring process.
* @param beanName the name of the bean (may be {@code null})
* @param annotatedClass the class of the bean
* @param constructorArguments argument values to be fed into Spring's
* constructor resolution algorithm, resolving either all arguments or just
* specific ones, with the rest to be resolved through regular autowiring
* (may be {@code null} or empty)
* @since 5.0
*/
public <T> void registerBean(@Nullable String beanName, Class<T> annotatedClass, Object... constructorArguments) {
this.reader.doRegisterBean(annotatedClass, null, beanName, null,
bd -> {
for (Object arg : constructorArguments) {
bd.getConstructorArgumentValues().addGenericArgumentValue(arg);
}
});
}
@Override
public <T> void registerBean(@Nullable String beanName, Class<T> beanClass, @Nullable Supplier<T> supplier,
BeanDefinitionCustomizer... customizers) {
public <T> void registerBean(@Nullable String beanName, Class<T> beanClass,
@Nullable Supplier<T> supplier, BeanDefinitionCustomizer... customizers) {
this.reader.doRegisterBean(beanClass, supplier, beanName, null, customizers);
this.reader.registerBean(beanClass, beanName, supplier, customizers);
}
}

49
spring-context/src/main/java/org/springframework/context/support/GenericApplicationContext.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 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.
@ -358,10 +358,43 @@ public class GenericApplicationContext extends AbstractApplicationContext implem @@ -358,10 +358,43 @@ public class GenericApplicationContext extends AbstractApplicationContext implem
// Convenient methods for registering individual beans
//---------------------------------------------------------------------
/**
* Register a bean from the given bean class, optionally providing explicit
* constructor arguments for consideration in the autowiring process.
* @param beanClass the class of the bean
* @param constructorArgs custom argument values to be fed into Spring's
* constructor resolution algorithm, resolving either all arguments or just
* specific ones, with the rest to be resolved through regular autowiring
* (may be {@code null} or empty)
* @since 5.2 (since 5.0 on the AnnotationConfigApplicationContext subclass)
*/
public <T> void registerBean(Class<T> beanClass, Object... constructorArgs) {
registerBean(null, beanClass, constructorArgs);
}
/**
* Register a bean from the given bean class, optionally providing explicit
* constructor arguments for consideration in the autowiring process.
* @param beanName the name of the bean (may be {@code null})
* @param beanClass the class of the bean
* @param constructorArgs custom argument values to be fed into Spring's
* constructor resolution algorithm, resolving either all arguments or just
* specific ones, with the rest to be resolved through regular autowiring
* (may be {@code null} or empty)
* @since 5.2 (since 5.0 on the AnnotationConfigApplicationContext subclass)
*/
public <T> void registerBean(@Nullable String beanName, Class<T> beanClass, Object... constructorArgs) {
registerBean(beanName, beanClass, (Supplier<T>) null,
bd -> {
for (Object arg : constructorArgs) {
bd.getConstructorArgumentValues().addGenericArgumentValue(arg);
}
});
}
/**
* Register a bean from the given bean class, optionally customizing its
* bean definition metadata (typically declared as a lambda expression
* or method reference).
* bean definition metadata (typically declared as a lambda expression).
* @param beanClass the class of the bean (resolving a public constructor
* to be autowired, possibly simply the default constructor)
* @param customizers one or more callbacks for customizing the factory's
@ -374,10 +407,8 @@ public class GenericApplicationContext extends AbstractApplicationContext implem @@ -374,10 +407,8 @@ public class GenericApplicationContext extends AbstractApplicationContext implem
}
/**
* Register a bean from the given bean class, using the given supplier for
* obtaining a new instance (typically declared as a lambda expression or
* method reference), optionally customizing its bean definition metadata
* (again typically declared as a lambda expression or method reference).
* Register a bean from the given bean class, optionally customizing its
* bean definition metadata (typically declared as a lambda expression).
* @param beanName the name of the bean (may be {@code null})
* @param beanClass the class of the bean (resolving a public constructor
* to be autowired, possibly simply the default constructor)
@ -396,7 +427,7 @@ public class GenericApplicationContext extends AbstractApplicationContext implem @@ -396,7 +427,7 @@ public class GenericApplicationContext extends AbstractApplicationContext implem
* Register a bean from the given bean class, using the given supplier for
* obtaining a new instance (typically declared as a lambda expression or
* method reference), optionally customizing its bean definition metadata
* (again typically declared as a lambda expression or method reference).
* (again typically declared as a lambda expression).
* @param beanClass the class of the bean
* @param supplier a callback for creating an instance of the bean
* @param customizers one or more callbacks for customizing the factory's
@ -414,7 +445,7 @@ public class GenericApplicationContext extends AbstractApplicationContext implem @@ -414,7 +445,7 @@ public class GenericApplicationContext extends AbstractApplicationContext implem
* Register a bean from the given bean class, using the given supplier for
* obtaining a new instance (typically declared as a lambda expression or
* method reference), optionally customizing its bean definition metadata
* (again typically declared as a lambda expression or method reference).
* (again typically declared as a lambda expression).
* <p>This method can be overridden to adapt the registration mechanism for
* all {@code registerBean} methods (since they all delegate to this one).
* @param beanName the name of the bean (may be {@code null})

Loading…
Cancel
Save