title: How can I find all beans with the custom annotation @Foo?
body: <p>I have this spring configuration:</p>
  
  <pre><code>@Lazy
  @Configuration
  public class MyAppConfig {
      @Foo @Bean
      public IFooService service1() { return new SpecialFooServiceImpl(); }
  }
  </code></pre>
  
  <p>How can I get a list of all beans that are annotated with <code>@Foo</code>?</p>
  
  <p>Note: <code>@Foo</code> is a custom annotation defined by me. It's not one of the "official" Spring annotations.</p>
  
  <p><strong>[EDIT]</strong> Following the suggestions of Avinash T., I wrote this test case:</p>
  
  <pre><code>import static org.junit.Assert.*;
  import java.lang.annotation.ElementType;
  import java.lang.annotation.RetentionPolicy;
  import java.lang.annotation.Target;
  
  import java.lang.annotation.Retention;
  import java.lang.reflect.Method;
  import java.util.Map;
  import org.junit.Test;
  import org.springframework.beans.factory.config.BeanDefinition;
  import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.context.annotation.Lazy;
  
  public class CustomAnnotationsTest {
  
      @Test
      public void testFindByAnnotation() throws Exception {
  
          AnnotationConfigApplicationContext appContext = new AnnotationConfigApplicationContext( CustomAnnotationsSpringCfg.class );
  
          Method m = CustomAnnotationsSpringCfg.class.getMethod( "a" );
          assertNotNull( m );
          assertNotNull( m.getAnnotation( Foo.class ) );
  
          BeanDefinition bdf = appContext.getBeanFactory().getBeanDefinition( "a" );
          // Is there a way to list all annotations of bdf?
  
          Map&lt;String, Object&gt; beans = appContext.getBeansWithAnnotation( Foo.class );
          assertEquals( "[a]", beans.keySet().toString() );
      }
  
  
      @Retention( RetentionPolicy.RUNTIME )
      @Target( ElementType.METHOD )
      public static @interface Foo {
  
      }
  
      public static class Named {
          private final String name;
  
          public Named( String name ) {
              this.name = name;
          }
  
          @Override
          public String toString() {
              return name;
          }
      }
  
      @Lazy
      @Configuration
      public static class CustomAnnotationsSpringCfg {
  
          @Foo @Bean public Named a() { return new Named( "a" ); }
               @Bean public Named b() { return new Named( "b" ); }
      }
  }
  </code></pre>
  
  <p>but it fails with <code>org.junit.ComparisonFailure: expected:&lt;[[a]]&gt; but was:&lt;[[]]&gt;</code>. Why?</p>