问题
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