I have a class similar to the following:
@Configuration
public class ApplicationConfiguration {
private <T> T createService(Class<T> serviceInterface) {
// implementation omitted
}
@Bean
public FooService fooService() {
return createService(FooService.class);
}
@Bean
public BarService barService() {
return createService(BarService.class);
}
...
}
The problem is that there are too many @Bean-annotated methods which differ only in their names, return types and arguments for the crateService method call.
I would like to make this class similar to the following:
@Configuration
public class ApplicationConfiguration {
private static final Class<?>[] SERVICE_INTERFACES = {
FooSerivce.class, BarService.class, ...};
private <T> T createService(Class<T> serviceInterface) {
// implementation omitted
}
@Beans // whatever
public Map<String, Object> serviceBeans() {
Map<String, Object> result = ...
for (Class<?> serviceInterface : SERVICE_INTERFACES) {
result.put(/* calculated bean name */,
createService(serviceInterface));
}
return result;
}
}
Is it possible in Spring?
@Bean-annotated methods. - Victor