其實我發現這篇文章從加文·金在回答在http://relation.to/12981.lace
基本上我要創建一個擴展和映射這樣的afterBeanDiscovery事件:
public void afterBeanDiscovery(@Observes AfterBeanDiscovery abd, BeanManager bm) {
for (final Class c: getBeanClasses()) {
//use this to read annotations of the class
AnnotatedType at = bm.createAnnotatedType(c);
//use this to create the class and inject dependencies
final InjectionTarget it = bm.createInjectionTarget(at);
abd.addBean(new Bean() {
@Override
public Class<?> getBeanClass() {
return c;
}
@Override
public Set<InjectionPoint> getInjectionPoints() {
return it.getInjectionPoints();
}
@Override
public String getName() {
String s = c.getSimpleName();
s = Character.toLowerCase(s.charAt(0)) + s.substring(1);
return s;
}
@Override
public Set<Annotation> getQualifiers() {
Set<Annotation> qualifiers = new HashSet<Annotation>();
qualifiers.add(new AnnotationLiteral<Default>() {});
qualifiers.add(new AnnotationLiteral<Any>() {});
return qualifiers;
}
@Override
public Class<? extends Annotation> getScope() {
return Dependent.class;
}
@Override
public Set<Class<? extends Annotation>> getStereotypes() {
return Collections.emptySet();
}
@Override
public Set<Type> getTypes() {
Set<Type> types = new HashSet<Type>();
types.add(c);
types.add(Object.class);
return types;
}
@Override
public boolean isAlternative() {
return false;
}
@Override
public boolean isNullable() {
return false;
}
@Override
public Object create(CreationalContext ctx) {
Object instance = it.produce(ctx);
it.inject(instance, ctx);
it.postConstruct(instance);
return instance;
}
@Override
public void destroy(Object instance, CreationalContext ctx) {
it.preDestroy(instance);
it.dispose(instance);
ctx.release();
}
});
}
}
我也有掃描我的罐子bean類,但這不是什麼大問題。一旦找到這些類,我將它包含在getBeanClasses()列表中。
現在我正在尋找關於如何在運行時包含JPA實體的解決方案。
但現在這個問題解決了。