如果我有一个仅在 @PostConstruct
方法中使用注入(inject)的 @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 不必要地保留了不必要的依赖项?如果注入(inject)请求数据库的服务,这是否意味着池中少了一个服务对象?值得担心吗?如果可以的话我可以释放依赖吗?
最佳答案
cs 字段的运行时内容取决于 CountryService 的范围。如果它在普通范围内(@ApplicationScoped、@RequestScoped、@SessionScoped 等),您将拥有一个代理而不是具体对象。如果它位于伪作用域中(@Dependent 或根本没有注释,或自定义作用域),那么您最终会得到该类的具体实例。
如果您有代理,则每次调用代理中的方法时,CDI 容器都会检索适当的上下文实例并将请求委托(delegate)给该对象。如果范围不包含适用的对象,则会创建一个新对象并将其放入上下文中,然后将请求委托(delegate)给该对象。当上下文被破坏(请求结束、http session 结束等)时,该对象将被清理。
解决具体问题:
Does that mean that the bean needlessly holds onto an unnecessary dependency ?
如果 CountryService 是依赖 bean,则可以。在这种情况下,您可以在完成后将该字段设置为 null,或者注入(inject)一个实例而不是对象本身。
@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 ?
很可能不会。如果 CountryService 是一个巨大的、依赖范围的 bean,那么也许是这样,但在这种情况下,您的架构可能会出现问题:)
关于jsf - 从应用程序范围的 Bean 中释放 Cdi 注入(inject)的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35167314/