2323
2424import org .springframework .beans .factory .BeanFactory ;
2525import org .springframework .beans .factory .NoSuchBeanDefinitionException ;
26+ import org .springframework .beans .factory .annotation .AnnotatedBeanDefinition ;
2627import org .springframework .beans .factory .config .BeanDefinition ;
2728import org .springframework .beans .factory .config .ConfigurableListableBeanFactory ;
2829import org .springframework .beans .factory .support .BeanDefinitionRegistry ;
3334import org .springframework .core .annotation .MergedAnnotation ;
3435import org .springframework .core .annotation .MergedAnnotations ;
3536import org .springframework .core .type .AnnotationMetadata ;
37+ import org .springframework .core .type .MethodMetadata ;
3638
3739/**
3840 * {@link ImportBeanDefinitionRegistrar} used by
3941 * {@link ServiceConnectionAutoConfiguration}.
4042 *
4143 * @author Phillip Webb
44+ * @author Daeho Kwon
4245 */
4346class ServiceConnectionAutoConfigurationRegistrar implements ImportBeanDefinitionRegistrar {
4447
@@ -60,8 +63,7 @@ private void registerBeanDefinitions(ConfigurableListableBeanFactory beanFactory
6063 new ConnectionDetailsFactories (null ));
6164 for (String beanName : beanFactory .getBeanNamesForType (Container .class )) {
6265 BeanDefinition beanDefinition = getBeanDefinition (beanFactory , beanName );
63- MergedAnnotations annotations = (beanDefinition instanceof TestcontainerBeanDefinition testcontainerBeanDefinition )
64- ? testcontainerBeanDefinition .getAnnotations () : null ;
66+ MergedAnnotations annotations = getAnnotations (beanDefinition );
6567 for (ServiceConnection serviceConnection : getServiceConnections (beanFactory , beanName , annotations )) {
6668 ContainerConnectionSource <?> source = createSource (beanFactory , beanName , beanDefinition , annotations ,
6769 serviceConnection );
@@ -92,6 +94,17 @@ private BeanDefinition getBeanDefinition(ConfigurableListableBeanFactory beanFac
9294 }
9395 }
9496
97+ private MergedAnnotations getAnnotations (BeanDefinition beanDefinition ) {
98+ if (beanDefinition instanceof TestcontainerBeanDefinition testcontainerBeanDefinition ) {
99+ return testcontainerBeanDefinition .getAnnotations ();
100+ }
101+ if (beanDefinition instanceof AnnotatedBeanDefinition annotatedBeanDefinition ) {
102+ MethodMetadata metadata = annotatedBeanDefinition .getFactoryMethodMetadata ();
103+ return (metadata != null ) ? metadata .getAnnotations () : null ;
104+ }
105+ return null ;
106+ }
107+
95108 @ SuppressWarnings ("unchecked" )
96109 private <C extends Container <?>> ContainerConnectionSource <C > createSource (
97110 ConfigurableListableBeanFactory beanFactory , String beanName , BeanDefinition beanDefinition ,
0 commit comments