java “@inject”-ed 属性保持为空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5661022/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 12:08:39  来源:igfitidea点击:

"@inject"-ed attribute remains null

javajakarta-eedependency-injectionjava-ee-6javabeans

提问by martijnve

I am trying to inject a service into my bean but it is always null. I get the following error: WELD-001000 Error resolving property userBean against base null.

我正在尝试将服务注入到我的 bean 中,但它始终是null. 我收到以下错误: WELD-001000 将属性 userBean 解析为空值时出错。

Some code snippets:

一些代码片段:

index.xhtml

索引.xhtml

<h:body>
    Hello from Facelets
    #{userBean.name}
</h:body>

userbean.java

用户bean.java

package beans;

import Domain.User;
import java.io.Serializable;
import javax.enterprise.context.SessionScoped;
import javax.inject.Inject;
import javax.inject.Named;
import service.UserService;

@Named
@SessionScoped
public class UserBean implements Serializable{
    @Inject UserService service;
    private User user;

    public UserBean(){
        this.user = service.find_user("foo");
    }

    public String getName(){
        return "bar";
    }
}

UserService.java

用户服务.java

package service;

import Domain.User;
import javax.ejb.Stateless;
import javax.inject.Named;

@Named
@Stateless
public class UserService {
    public UserService(){}

    public User find_user(String name){
        return new User();
    }
}

采纳答案by Andreas Dolk

The error message couldbe a hint, that the JVM wasn't able to create an instance of UserBean. The following is some guessing and would have to be proven:

错误消息可能是一个提示,即 JVM 无法创建UserBean. 以下是一些猜测,必须证明:

Dependecy Injection requires a dependency injector, a tool that injectsan instance of UserServiceinto a UserBean. In your code you're already using this injected instance during instantiation of the bean: you call the injected service in the constructor.

依赖注入需要一个依赖注入器,一种的实例注入UserServiceUserBean. 在您的代码中,您已经在 bean 的实例化过程中使用了这个注入的实例:您在构造函数中调用了注入的服务。

Ifthe dependency injector starts it's work afterthe bean is created, thenthe call to the service inside the constructor will raise a NullPointerException(because serviceis still nullat that time). It's worth checking that by trying to catch NPEs in the UserBeanconstructor for a moment. If you catch one - voilà - the dependency injector starts runnning after the bean has been created and, as a consequence, we can't use injected services during class instantiation (= in the constructor)

如果依赖注入器创建 bean启动它的工作,那么对构造函数内的服务的调用将引发一个NullPointerException(因为service仍然null在那个时候)。值得通过尝试在UserBean构造函数中捕获 NPE 来检查一下。如果你发现一个 - 瞧 - 依赖注入器在 bean 被创建后开始运行,因此,我们不能在类实例化期间使用注入的服务(= 在构造函数中)



Workaround idea: implement a small service provider helper class - inner class could work:

解决方法的想法:实现一个小的服务提供者助手类 - 内部类可以工作:

public class UserBean implements Serializable {
    static class UserServiceProvider {
      @Inject static UserService service;
    }

    // ... 

    public UserBean() {
      this.user = UserServiceProvider.service.findUser("kaas"); 
    }

    // ...
}

Untested but could work - the service should be injected in the provider class beforeyou use it in the beans constructor.

未经测试但可以工作 -您在 beans 构造函数中使用它之前,应该将服务注入到提供者类中。

回答by Gere

Another alternative is use @PostConstruct method annotation.

另一种选择是使用@PostConstruct 方法注释。

@SessionScoped
public class UserBean implements Serializable {
    @Inject UserService service;
    private User user;
    public UserBean() {

    }

   @PostConstruct
    void init(){
        this.user = service.findUser("kaas"); 
    }

  }

Read docs

阅读文档