Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I find all beans with the custom annotation @Foo?

I have this spring configuration:

@Lazy @Configuration public class MyAppConfig {     @Foo @Bean     public IFooService service1() { return new SpecialFooServiceImpl(); } } 

How can I get a list of all beans that are annotated with @Foo?

Note: @Foo is a custom annotation defined by me. It's not one of the "official" Spring annotations.

[EDIT] Following the suggestions of Avinash T., I wrote this test case:

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<String, Object> 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" ); }     } } 

but it fails with org.junit.ComparisonFailure: expected:<[[a]]> but was:<[[]]>. Why?

like image 582
Aaron Digulla Avatar asked Jan 09 '13 13:01

Aaron Digulla


People also ask

How can we get list of all beans used in Spring application?

We can get a list of all beans within this container in two ways: Using a ListableBeanFactory interface. Using a Spring Boot Actuator.

Where can I use bean annotations?

One of the most important annotations in spring is the @Bean annotation which is applied on a method to specify that it returns a bean to be managed by Spring context. Spring Bean annotation is usually declared in Configuration classes methods. This annotation is also a part of the spring core framework.

Which annotation is used for defining beans?

We can also declare beans using the @Bean annotation in a configuration class. Finally, we can mark the class with one of the annotations from the org. springframework.

Which annotation in the bean class indicates that the specified bean property?

C - This annotation simply indicates that the affected bean property must be populated at configuration time, through an explicit property value in a bean definition or through autowiring.


1 Answers

Use getBeansWithAnnotation() method to get beans with annotation.

Map<String,Object> beans = applicationContext.getBeansWithAnnotation(Foo.class); 

Here is similar discussion.

like image 90
Avinash T. Avatar answered Sep 21 '22 19:09

Avinash T.