CDI SessionScoped Bean的非延迟实例化 [英] Non-lazy instantiation of CDI SessionScoped beans
问题描述
CDI新手问题.简单的测试场景:JSF + CDI SessionScoped bean.
我需要一种优雅的方法来实例化一组已知的会话作用域CDI bean,而不必在JSF
页上提及它们或从其他bean调用它们的方法.作为一个测试用例-一个简单的日志记录bean,它仅记录http会话的开始和结束时间.
当然,我可以创建一个空的JSF组件,将其放置在站点范围的模板内,并使其触发所需会话bean的伪方法,但这从我的pov来看有点难看.
我看到的另一种选择是选择一个会话Bean(通过JSF
中的EL
或其他Bean的引用对其进行100%初始化),并使用其@PostConstruct
方法来触发其他会话Bean. -解决方案比以前的解决方案少一些.
好像我在这里遗漏了什么,我将不胜感激其他想法.
是的,HttpSessionListener
可以做到.只需注入bean并调用它们即可.
如果您的容器不支持HttpSessionListener中的注入,则可以查看deltaspike核心和BeanProvider
http://deltaspike.apache.org/core.html
CDI newbie question. Simple test scenario: JSF + CDI SessionScoped beans.
I need an elegant way to instantiate a known set of session scoped CDI beans without mentioning them on a JSF
page or calling their methods from other beans. As a test case - a simple logging bean, which simply logs start and end time of an http session.
Sure, I could create an empty JSF component, place it inside of a site-wide template and make it trigger dummy methods of the required session beans, but it's kinda ugly from my pov.
Another option I see, is to choose a single session bean (which gets initialized 100% either by EL
in JSF
or by references from other beans), and use its @PostConstruct
method to trigger other session beans - the solution a little less uglier than the previous one.
Looks like I'm missing something here, I'd appreciate any other ideas.
Yes, HttpSessionListener
would do it. Simply inject the beans and invoke them.
If you container does not support injection in a HttpSessionListener you could have a look at deltaspike core and BeanProvider
http://deltaspike.apache.org/core.html
这篇关于CDI SessionScoped Bean的非延迟实例化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!