从应用程序范围的 Bean 中释放 Cdi 注入对象

Release Cdi injected object from application scoped Bean

如果我有一个仅在 @PostConstruct 方法中使用注入的 @ApplicationScoped bean,如下所示:

@Named
@ApplicationScoped
public class CountriesConverter implements Converter {
    private List<Country> countries;

    @Inject
    private CountriesService cs;

    @PostConstruct
    public void init() {
        this.countries = cs.getAllCountries();
    }
 ...
}

这是否意味着 bean 不必要地持有不必要的依赖项?在注入请求数据库的服务的情况下,这是否意味着池中的服务对象少了一个?值得担心吗?如果可以我可以解除依赖吗?

cs 字段的运行时内容取决于CountriesService 的范围。如果它在正常范围内(@ApplicationScoped、@RequestScoped、@SessionScoped 等),您将拥有一个代理而不是具体对象。如果它在伪作用域中(@Dependent 或根本没有注释,或自定义作用域),那么您最终会得到 class.

的具体实例

如果您有代理,每次调用代理中的方法时,CDI 容器都会检索适当的上下文实例并将请求委托给该对象。如果范围不包含适用的对象,将创建一个新对象并将其放入上下文中,然后将请求委托给该对象。当上下文被销毁(请求结束、http 会话结束等)时,对象将被清理。

解决具体问题:

Does that mean that the bean needlessly holds onto an unnecessary dependency ?

如果 CountriesService 是依赖 bean,是的。在这种情况下,您可以在完成后将字段设置为 null,或者注入实例而不是对象本身。

@Inject
private Instance<CountriesService> serviceInstance;

@PostConstruct
private void init() {
    this.countries = serviceInstance.get().getAllCountries();
}

In the case of the injection of a service that request the DB, does that mean that there is one less service object in the pool ?

池的概念不适用于 CDI bean。在这个意义上没有池,CDI 根本不关心并发性。如果您使用的是无状态 EJB,那么您有一个指向池的代理,所以您仍然没问题。

Is it worth worrying about ?

很可能不会。如果 CountriesService 是一个巨大的、依赖范围的 bean,那么也许,但在那种情况下,您的体系结构可能有问题 :)