title: How to get a Prototype Scoped Osgi Component (which turn out to also be Vaadin component) reference programmatically
body: <p>I am using Vaadin 21 in Apache Karaf, an Osgi container.
  What i want to do :</p>
  <ul>
  <li><p>Create a vaadin Component, annotates it with @Component(scope=ScopeService.PROTOTYPE)</p>
  </li>
  <li><p>In the View with the @Route, the Vaadin doc says that we cannot use injection dependency to get the Osgi Component reference</p>
  </li>
  <li><p>To get the reference, the doc says to use :</p>
  <pre><code>ServiceReference&lt;MyComponent&gt; reference = ctx.getServiceReference(MyService.class);
  MyComponent myComponent = ctx.getService(reference);```
  
  </code></pre>
  </li>
  <li><p>I have a problem when i refresh my screen, the instance of MyComponent stays the same</p>
  </li>
  <li><p>To avoid that, i found an alternative :</p>
  <pre><code>    ServiceReference&lt;MyComponent&gt; reference = ctx.getServiceReference(MyComponent.class);
      ServiceObjects&lt;MyComponent&gt; res = ctx.getServiceObjects(reference);
      MyComponent myComponent = res.getService();```
  
  
  
  </code></pre>
  </li>
  </ul>
  <p>Has anyone got a better way to get my Prototype vaadin and osgi component Reference ?
  Here is my code (from base-starter-flow-osgi) :</p>
  <pre><code>public class MainView extends VerticalLayout {
  
      public MainView() {
          
          BundleContext ctx = FrameworkUtil.getBundle(MyComponent.class).getBundleContext();
          ServiceReference&lt;MyComponent&gt; reference = ctx.getServiceReference(MyComponent.class);
          ServiceObjects&lt;MyComponent&gt; 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(&quot;Hello World!&quot;));
      }
      
  }
  </code></pre>