看到这个stackoverflow帖子之后,我实现了以下解决方案。不知道这是否是最好的选择,但是确实有效。
我创建了一个hk2活页夹并注册了活页夹:
public class WebServiceBinder extends AbstractBinder { @Override protected void configure() { BeanManager bm = getBeanManager(); bind(getBean(bm, StudentRepository.class)) .to(StudentRepository.class); } private BeanManager getBeanManager() { // is there a better way to get the bean manager? return new Weld().getBeanManager(); } private <T> T getBean(BeanManager bm, Class<T> clazz) { Bean<T> bean = (Bean<T>) bm.getBeans(clazz).iterator().next(); CreationalContext<T> ctx = bm.createCreationalContext(bean); return (T) bm.getReference(bean, clazz, ctx); }}
然后从上面将ResourceConfig实例修改为:
final ResourceConfig resourceConfig = new ResourceConfig() .packages("training.webservice") .register(new JacksonFeature()) .register(new WebServiceBinder());
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)