Vaadin: CDI Inject is null after referencing

感情迁移 提交于 2019-12-25 00:59:20

问题


Good day.

There is an application (using a CDI add on):

@VaadinScoped(VaadinScope.APPLICATION)
public class AdminApplication extends AbstractCdiApplication {

   @Inject
   private Instance<Lang> lang;

   @Override
   public void init() {
    setMainWindow(new LoginWindow(this));
   }

   public void authenticate(String login, String password) throws Exception {
    lang.get(). ...
}
     ...

And the LoginWindow:

public class LoginWindow extends Window {
     ...
     public LoginWindow(AdminApplication application) {
       super("LoginWindow Login");
       this.application = application;
       initUI();
       initLoginListener();
     }
      private void initLoginListener() {
         btnLogin.addListener(new Button.ClickListener() {
            public void buttonClick(Button.ClickEvent event) {
               try {
                   String username = (String) txtUsername.getValue();
                   String password = (String) txtPassword.getValue();

                   application.authenticate(username, password);
               } catch (Exception e) {
                showNotification(e.toString());
               }
           }
    });
}

The problem is when it is the application's initialization phase the lang.get() is not null, but when in the LoginWindow I call application.authenticate() method, the lang.get() is always null. It seem that when using a reference the Instance.get() method can't get the class' instance.

Does anybody know why this happens?


回答1:


In your application, you create the LoginWindow via "new". That way, it is not managed by the CDI container. You have to inject the window to your application and then set it in the init() method.




回答2:


I had this problem with Wildfly 8.1 and 8.2 but when I added an empty beans.xml in the WEB-INF it worked. So if the above doesn't work try that.



来源:https://stackoverflow.com/questions/12552179/vaadin-cdi-inject-is-null-after-referencing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!