Skip to content

Instantly share code, notes, and snippets.

@dwelch2344
Created April 29, 2014 17:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dwelch2344/11406133 to your computer and use it in GitHub Desktop.
Save dwelch2344/11406133 to your computer and use it in GitHub Desktop.
While reading a few great articles (http://www.intertech.com/Blog/spring-4-generic-qualifiers/ comes to mind) I noticed that Spring 4 supports injecting generic objects if their type is provided when defining @beans. I'm trying to figure out how I can do that when describing beans in a BeanDefinitionRegistryPostProcessor
public class Consumer{
// these work with qualifiers...
@Inject
private GenericService<User, Long> userService;
@Inject
private GenericService<Organization, Long> orgService;
}
public class GenericService<E, ID>{
// ...
}
public class Processor extends InstantiationAwareBeanPostProcessorAdapter
implements BeanDefinitionRegistryPostProcessor{
// ...
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry r) throws BeansException {
AbstractBeanDefinition service = BeanDefinitionBuilder.rootBeanDefinition(GenericService.class)
.getBeanDefinition();
r.registerBeanDefinition(service, "userService" );
// how do I hint that this bean has the bound types <User, Long> ??
AbstractBeanDefinition service2 = BeanDefinitionBuilder.rootBeanDefinition(GenericService.class)
.getBeanDefinition();
// how do I hint that this bean has the bound types <Organization, Long> ??
r.registerBeanDefinition(service2, "orgService" );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment