diff --git a/spring-context/src/main/java/org/springframework/context/annotation/AnnotatedBeanDefinitionReader.java b/spring-context/src/main/java/org/springframework/context/annotation/AnnotatedBeanDefinitionReader.java index 5bd012a730c..19448f17941 100644 --- a/spring-context/src/main/java/org/springframework/context/annotation/AnnotatedBeanDefinitionReader.java +++ b/spring-context/src/main/java/org/springframework/context/annotation/AnnotatedBeanDefinitionReader.java @@ -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 void registerBean(Class annotatedClass, @Nullable Supplier 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 void registerBean(Class annotatedClass, String name, @Nullable Supplier 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 { */ @SuppressWarnings("unchecked") public void registerBean(Class annotatedClass, Class... qualifiers) { - doRegisterBean(annotatedClass, null, null, qualifiers); + doRegisterBean(annotatedClass, null, qualifiers, null, null); } /** @@ -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... qualifiers) { - doRegisterBean(annotatedClass, null, name, qualifiers); + public void registerBean(Class annotatedClass, @Nullable String name, + Class... 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 void registerBean(Class annotatedClass, @Nullable Supplier 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 void registerBean(Class annotatedClass, @Nullable String name, @Nullable Supplier 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 void registerBean(Class annotatedClass, @Nullable String name, @Nullable Supplier 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 */ - void doRegisterBean(Class annotatedClass, @Nullable Supplier instanceSupplier, @Nullable String name, - @Nullable Class[] qualifiers, BeanDefinitionCustomizer... definitionCustomizers) { + private void doRegisterBean(Class annotatedClass, @Nullable String name, + @Nullable Class[] qualifiers, @Nullable Supplier 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 { } } } - for (BeanDefinitionCustomizer customizer : definitionCustomizers) { - customizer.customize(abd); + if (customizers != null) { + for (BeanDefinitionCustomizer customizer : customizers) { + customizer.customize(abd); + } } BeanDefinitionHolder definitionHolder = new BeanDefinitionHolder(abd, beanName); diff --git a/spring-context/src/main/java/org/springframework/context/annotation/AnnotationConfigApplicationContext.java b/spring-context/src/main/java/org/springframework/context/annotation/AnnotationConfigApplicationContext.java index e93a800cba3..ee25c664593 100644 --- a/spring-context/src/main/java/org/springframework/context/annotation/AnnotationConfigApplicationContext.java +++ b/spring-context/src/main/java/org/springframework/context/annotation/AnnotationConfigApplicationContext.java @@ -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 //--------------------------------------------------------------------- - // 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. - *

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 void registerBean(Class 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 void registerBean(@Nullable String beanName, Class annotatedClass, Object... constructorArguments) { - this.reader.doRegisterBean(annotatedClass, null, beanName, null, - bd -> { - for (Object arg : constructorArguments) { - bd.getConstructorArgumentValues().addGenericArgumentValue(arg); - } - }); - } - @Override - public void registerBean(@Nullable String beanName, Class beanClass, @Nullable Supplier supplier, - BeanDefinitionCustomizer... customizers) { + public void registerBean(@Nullable String beanName, Class beanClass, + @Nullable Supplier supplier, BeanDefinitionCustomizer... customizers) { - this.reader.doRegisterBean(beanClass, supplier, beanName, null, customizers); + this.reader.registerBean(beanClass, beanName, supplier, customizers); } } diff --git a/spring-context/src/main/java/org/springframework/context/support/GenericApplicationContext.java b/spring-context/src/main/java/org/springframework/context/support/GenericApplicationContext.java index 4b766530c99..b1105ccaaeb 100644 --- a/spring-context/src/main/java/org/springframework/context/support/GenericApplicationContext.java +++ b/spring-context/src/main/java/org/springframework/context/support/GenericApplicationContext.java @@ -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 // 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 void registerBean(Class 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 void registerBean(@Nullable String beanName, Class beanClass, Object... constructorArgs) { + registerBean(beanName, beanClass, (Supplier) 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 } /** - * 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 * 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 * 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). *

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})