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?
@Configuration
public class ApplicationConfiguration {
@Autowired
private ConfigurableBeanFactory beanFactory;
@PostConstruct
public void registerServices() {
beanFactory.registerSingleton("service...", new NNNService());
...
}
}
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