Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/cepr0/sb-tagged-autowire-candidate-resolver
TaggedAutowireCandidateResolver - an example of Spring AutowireCandidateResolver
https://github.com/cepr0/sb-tagged-autowire-candidate-resolver
autowiring beans injection spring
Last synced: 9 days ago
JSON representation
TaggedAutowireCandidateResolver - an example of Spring AutowireCandidateResolver
- Host: GitHub
- URL: https://github.com/cepr0/sb-tagged-autowire-candidate-resolver
- Owner: Cepr0
- Created: 2019-08-01T14:43:12.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2019-08-02T09:28:16.000Z (over 5 years ago)
- Last Synced: 2024-11-17T02:42:22.657Z (2 months ago)
- Topics: autowiring, beans, injection, spring
- Language: Java
- Size: 8.79 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# TaggedAutowireCandidateResolver
_An example of custom implementation of Spring [AutowireCandidateResolver][1]_[TaggedAutowireCandidateResolver][2] is an implementation of `AutowireCandidateResolver`
that makes it possible to get collections of injected Spring beans
tagged by specified 'tags'. Tags are specified with [@Tags][3] annotation
in style of `@Qualifier` annotation. Collection is formed from the beans
that have the same type and the same tags as the autowired collection.```java
@Autowired
@Tags({"greeting", "other"})
private Map> greetingOrOther;@Configuration
static class Beans {
@Tags({"greeting", "2symbols", "even"})
@Bean
public Supplier hi() {
return () -> "hi";
}@Tags({"parting", "2symbols", "even"})
@Bean
public Supplier by() {
return () -> "by";
}@Tags({"greeting", "5symbols", "odd"})
@Bean
public Supplier hello() {
return () -> "hello";
}@Tags({"parting", "7symbols", "odd"})
@Bean
public Supplier goodbye() {
return () -> "goodbye";
}@Tags({"other", "5symbols", "odd"})
@Bean
public Supplier other() {
return () -> "other";
}
}
```
Here Map `greetingOrOther` will have three `Supplier` beans: `hi`, `hello` and `other`;To make it work you have to register a [CustomAutowireConfigurer][6] bean in your application
and provide it with `TaggedAutowireCandidateResolver`:```java
@Configuration
public class AutowireConfig {
@Bean
public CustomAutowireConfigurer autowireConfigurer(DefaultListableBeanFactory beanFactory) {
CustomAutowireConfigurer configurer = new CustomAutowireConfigurer();
beanFactory.setAutowireCandidateResolver(new TaggedAutowireCandidateResolver());
configurer.postProcessBeanFactory(beanFactory);
return configurer;
}
}
```More usage examples are in [Test][4]
This project is related to this [SO post][5].
[1]: https://bit.ly/2Yj8qK0
[2]: src/main/java/io/github/cepr0/resolver/TaggedAutowireCandidateResolver.java
[3]: src/main/java/io/github/cepr0/resolver/Tags.java
[4]: src/test/java/io/github/cepr0/resolver/TaggedAutowireCandidateResolverTest.java
[5]: https://stackoverflow.com/a/57169506
[6]: https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#beans-custom-autowire-configurer