0

我在 Apache Karaf(一个 Osgi 容器)中使用 Vaadin 21。我想做的事 :

  • 创建一个 vaadin 组件,使用 @Component(scope=ScopeService.PROTOTYPE) 对其进行注释

  • 在带有@Route 的视图中,Vaadin 文档说我们不能使用注入依赖来获取 Osgi 组件引用

  • 为了获得参考,文档说要使用:

    ServiceReference<MyComponent> reference = ctx.getServiceReference(MyService.class);
    MyComponent myComponent = ctx.getService(reference);```
    
    
  • 刷新屏幕时出现问题,MyComponent 的实例保持不变

  • 为了避免这种情况,我找到了一个替代方案:

        ServiceReference<MyComponent> reference = ctx.getServiceReference(MyComponent.class);
        ServiceObjects<MyComponent> res = ctx.getServiceObjects(reference);
        MyComponent myComponent = res.getService();```
    
    
    
    

有没有人有更好的方法来获取我的原型 vaadin 和 osgi 组件参考?这是我的代码(来自 base-starter-flow-osgi):

public class MainView extends VerticalLayout {

    public MainView() {
        
        BundleContext ctx = FrameworkUtil.getBundle(MyComponent.class).getBundleContext();
        ServiceReference<MyComponent> reference = ctx.getServiceReference(MyComponent.class);
        ServiceObjects<MyComponent> res = ctx.getServiceObjects(reference);
        prestas = res.getService();

        add(myComponent);
    }
}
@Component(service=MyComponent.class, scope=ServiceScope.PROTOTYPE)
public class MyComponent extends Div{
    private static final long serialVersionUID = -8573895829405499446L;

    @Override
    protected void onAttach(AttachEvent attachEvent) {
        super.onAttach(attachEvent);
        add(new Span("Hello World!"));
    }
    
}
4

0 回答 0