In my Spring Boot application, suppose I have interface in Java:
public interface MyFilter<E extends SomeDataInterface>
(a good example is Spring's public interface ApplicationListener< E extends ApplicationEvent > )
and I have couple of implementations like:
@Component public class DesignatedFilter1 implements MyFilter<SpecificDataInterface>{...} @Component public class DesignatedFilter2 implements MyFilter<SpecificDataInterface>{...} @Component public class DesignatedFilter3 implements MyFilter<AnotherSpecificDataInterface>{...}
Then, in some object I am interested to utilize all filters that implement MyFilter< SpecificDataInterface > but NOT MyFilter< AnotherSpecificDataInterface >
What would be the syntax for this?
In Spring Boot, you can use appContext. getBeanDefinitionNames() to get all the beans loaded by the Spring container.
We can get a list of all beans within this container in two ways: Using a ListableBeanFactory interface. Using a Spring Boot Actuator.
@Component is a class-level annotation, but @Bean is at the method level, so @Component is only an option when a class's source code is editable. @Bean can always be used, but it's more verbose. @Component is compatible with Spring's auto-detection, but @Bean requires manual class instantiation.
Annotating a class with the @Configuration indicates that the class can be used by the Spring IoC container as a source of bean definitions. The @Bean annotation tells Spring that a method annotated with @Bean will return an object that should be registered as a bean in the Spring application context.
The following will inject every MyFilter instance that has a type that extends SpecificDataInterface as generic argument into the List.
@Autowired private List<MyFilter<? extends SpecificDataInterface>> list;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With