Myfaces CODI、@Inject 和内存使用情况

发布于 2024-12-19 05:11:25 字数 1309 浏览 3 评论 0原文

我在 JSF 应用程序中使用 Myfaces CODI @ViewAccessScoped 支持 bean。中的一个 好处是我不需要使用视图参数来在之间传递信息 意见。根据记录,@ViewAccessScoped 确保 bean 可以访问,直到新视图的第一个请求无法访问它为止。以我想将字符串值从 page1 传递到 page2 为例:

Page1Bean.java (page1.xhtml 的支持 bean)

@Inject private Page2Bean page2Bean;
private String source = "Hello, World!";

...
page2Bean.setTarget(source);

Page2Bean.java (page2.xhtml 的支持 bean)

private String target;

如果我直接从 page1 导航到 page2,那么当我访问#{page2Bean.target} page2 它的值为“你好,世界!”。

实际上,我将数据从 page1 视图推送到 page2 视图。另一种选择是 将数据从 page1 视图拉入 page2 视图,因此在 page2Bean 中我 @Inject Page1Bean 和 @ViewAccessScoped 确保我可以访问 page1Bean.getSource() (只要 就像之前的视图一样)。

这一切都很好,但在现实世界中,我可能想从 page1 导航到任何 许多其他页面之一,具体取决于用户输入。所以 Page1Bean.java 最终会寻找 像这样:

Page1Bean.java(修订版)

@Inject private Page2Bean page2Bean;
@Inject private Page3Bean page3Bean;
@Inject private Page4Bean page4Bean;
@Inject private Page5Bean page5Bean;
@Inject private Page6Bean page6Bean;
@Inject private Page7Bean page7Bean;
@Inject private Page8Bean page8Bean;

现在我的问题:page1Bean的内存占用是否总是包括page2Bean- page8Bean?或者只有当我访问 @Inject'ed beans 之一时才会使用内存 运行时?

我希望这不是一个太天真的问题,但我不确定它到底是如何工作的 如果第一个问题的答案是肯定的,我或多或少最终会使用 @SessionScoped!

感谢您的任何澄清。

I use Myfaces CODI @ViewAccessScoped backing beans in my JSF application. One of the
benefits is that I don't need to use view parameters to communicate information between
views. For the record @ViewAccessScoped ensures that a bean is accessible until until the first request of a new view doesn't access it. Take this case where I want to pass a string value from page1 to page2:

Page1Bean.java (backing bean for page1.xhtml)

@Inject private Page2Bean page2Bean;
private String source = "Hello, World!";

...
page2Bean.setTarget(source);

Page2Bean.java (backing bean for page2.xhtml)

private String target;

If I navigate directly from page1 to page2, then when I access #{page2Bean.target} from
page2 it has the value "hello, world!".

In effect I'm pushing the data from the page1 view to the page2 view. The other option is
to pull the data from the page1 view into the page2 view, so in the page2Bean I @Inject
Page1Bean and @ViewAccessScoped ensures that I can access page1Bean.getSource() (as long
as it was in the previous view).

This is all well and good, but in the real world I may want to navigate from page1 to any
one of a number of other pages, depending on user input. So Page1Bean.java ends up looking
like this:

Page1Bean.java (revised)

@Inject private Page2Bean page2Bean;
@Inject private Page3Bean page3Bean;
@Inject private Page4Bean page4Bean;
@Inject private Page5Bean page5Bean;
@Inject private Page6Bean page6Bean;
@Inject private Page7Bean page7Bean;
@Inject private Page8Bean page8Bean;

Now for my question: does the memory footprint of page1Bean always include page2Bean-
page8Bean? or will memory only be used if I access one of the @Inject'ed beans at
runtime?

I hope this isn't too naive a question, but I'm not sure exactly how it will work and
if the answer to the first question is yes, I'm more or less ending up using
@SessionScoped!.

Thanks for any clarification.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏有森光若流苏 2024-12-26 05:11:25

好吧,我认为这确实很明显,但是在使用 @Inject 注入的 Bean 的构造函数中添加了一些日志记录后,我可以看到它们在实例化 Page1Bean 时(即导航到 page1 时)都被实例化。我在 JSR-299 CDI 规范第 5.6 节编程查找中找到了解决方案:

@Inject private Instance<Page2Bean> page2BeanDynamic;
...
if(someCondition) {
  Page2Bean page2Bean = page2BeanDynamic.get();
  page2Bean.setTarget(source);
}

所以这基本上是动态 @Inject 并确保我只实例化 bean
在运行时需要时。

将 Finalize() & Page2Bean 中的 @PreDestroy 方法我看到它们都被调用
正如预期的那样,从 page2 导航到 page1。

Well, I suppose it was pretty obvious really but having put some logging in the constructors of the beans that were being injected with @Inject, I could see that they were all being instantiated when the Page1Bean was instantiated, i.e. when navigating to page1. I found the solution in the JSR-299 CDI spec section 5.6 Programmatic lookup:

@Inject private Instance<Page2Bean> page2BeanDynamic;
...
if(someCondition) {
  Page2Bean page2Bean = page2BeanDynamic.get();
  page2Bean.setTarget(source);
}

so this is basically dynamic @Inject and ensures that I'm only instantiating the beans
at runtime when needed.

Putting finalize() & @PreDestroy methods in Page2Bean I see them both called when
navigating from page2 to page1, as expected.

尐偏执 2024-12-26 05:11:25

没有真正的内存占用。仅生成代理。这就是构造函数调用的原因。您不需要手动解析 bean!

您不必注入所有豆子。您以错误的方式使用它。仅保持_独立_页面之间的状态应该通过@WindowScoped完成。如果它们不是独立的,请使用目标页面中的 bean(如果您不需要目标页面中的视图控制器回调)。

No real memory footprint. Only proxies are generated. That's the reason for the constructor calls. You don't need to resolve beans manually!

You don't have to inject all beans. You are using it in a wrong way. Just keeping state between _ independent _ pages should be done via @WindowScoped. If they aren't independent use the beans in the target page (if you don't need the view-controller callbacks in the target pages).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文