JEE: Inject Facade into View

Hi, I try to inject a Facade into a View. I can inject other services (@ApplicationScoped) into the View or inject the Facade into the MainUI, but I can’t inject the Facade into the View.

Here some Code:

@CDIUI("")
public class MyUI extends UI {    
    @Inject
    private RichtextView view1;

// this will work
    @Inject
    private TextFacade facade;

    @Override
    protected void init(VaadinRequest vaadinRequest) {
        ...        
        Navigator navigator = new Navigator(this, viewContainer);
        navigator.addView("view1", view1);
    }
}
@UIScoped
public class RichtextView extends Composite implements View {
// this doesn't work -> NPE
    @Inject
    private TextFacade facade;

    public RichtextView() {        
        List<Text> customers = facade.findAll();
        ...
    }
}
@Stateless
public class TextFacade extends AbstractFacade<Text> {
    @PersistenceContext(unitName = "com.mycompany_demo_richtext_war_1.0-SNAPSHOTPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }
    public TextFacade() {
        super(Text.class);
    }
}

Has someone an idea why it doesn’t work?

Thanks for your help,
Lars

To call the face.findAll() method in the construct was the misstake. I implemented the enter-method from the view and do the staff there.

After thinking about that, it really makes more sense. :smiley:

  1. Why are you not using CDIViewProvider

Yout got serveral problems:

  1. You injection is null because you are referencing it in the constructor, you must do it a little bit later in @PostConstruct
  2. You have typed stateless service which is not ok i guess
  3. Making Views UIScoped never clean up resources view has obtained.

Thx for your advices.
3) Thx, I’ve changed my app like the bakery example. So the view is @CDIView now.
2) The facade was generated by Netbeans. But I will look on it.