[seam-dev] Postconstruct issue when registering a Bean in an extension
Antoine Sabot-Durand
antoine at sabot-durand.net
Tue Aug 2 05:05:17 EDT 2011
Hi All,
In Seam Social, in order to support polymorphism I need the service beans to be qualified with the name of the service. For instance :
@RelatedTo("LinkedIn")
public class LinkedInJackson extends OAuthServiceBase implements LinkedIn
It allows users to inject a service bean like that
@Inject @RelatedTo("LinkedIn")
OAuthService service;
As I want to ease user life I also need to support this kind of injection (same results as above)
@Inject
LinkedIn service;
As LinkedIn interface has only one implementation, using a qualifier when I use its specific type is useless and could be confusing.
Until now I registered the second version of the bean (without a qualifier) thru a producer, but as each service bean will have both registration (with and without qualifier) I decided to simplify this bi-registration with an extension (it will also ease the writing of new service bean for third party developers).
My extension works in 2 steps :
First it collects in a Map all the annotated type which are OAuthService children and have a @RelatedTo qualifier :
public void processServicesBeans(@Observes ProcessManagedBean<OAuthService> pbean) {
Annotated annotated = pbean.getAnnotated();
if (annotated.isAnnotationPresent(RelatedTo.class)) {
RelatedTo related = annotated.getAnnotation(RelatedTo.class);
String name = related.value();
servicesBean.put(name, pbean.getAnnotatedBeanClass());
}
}
Then, after the bean discovery, it register a new version of each Qualified bean without the qualifier (thanks to solder !).
public void afterBeanDiscovery(@Observes AfterBeanDiscovery abd, BeanManager bm) {
for (String type : servicesBean.keySet()) {
AnnotatedType<? extends OAuthService> annotatedType = servicesBean.get(type);
AnnotatedTypeBuilder annoBuilder = new AnnotatedTypeBuilder().readFromType(annotatedType).removeFromClass(
RelatedTo.class);
AnnotatedType myAnnotatedType = annoBuilder.create();
BeanBuilder beanBuilder = new BeanBuilder(bm).readFromType(myAnnotatedType);
abd.addBean(beanBuilder.create());
}
}
This seems to work quite well except that my service Beans have a @Postconstruct method and at initialization Weld throws the following exception
org.jboss.weld.exceptions.DefinitionException: WELD-000805 Cannot have more than one post construct method annotated with @PostConstruct for public class org.jboss.seam.social.linkedin.LinkedInJackson
If I remove the @Postconstuct in the AnnotatedTypeBuilder by changing its declaration by :
AnnotatedTypeBuilder annoBuilder = new AnnotatedTypeBuilder().readFromType(annotatedType).removeFromClass(
RelatedTo.class).removeFromAll(PostConstruct.class);
Initialization works, I have my 2 beans, but the one I registered without qualifier won't execute the @PostConstruct method (since I had to remove it). I guess I'm missing something in the CDI meta-meta-maze :-). Any help or suggestion would be nice.
Thanks,
Antoine SABOT-DURAND
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/seam-dev/attachments/20110802/ad9df3e8/attachment-0001.html
More information about the seam-dev
mailing list