0

I am using an existing application based on JSF2.0 and over top we are using Primefaces. I have seen, people have injected on managed bean inside another managed bean. I am looking forward to know, is it a good practice? What about the performance drawbacks? I see our application behaves very slow in Performance Testing environment. Another thing, inside a ManagedBean annotation, we have a concept of "eager", how useful this attribute is?

Thanks.

benz
  • 4,171
  • 6
  • 31
  • 55
  • 1
    Except the term *eager* regarding managed beans (which in turn, is only applicable to application scoped JSF managed beans hereby not CDI beans) is used to initialize a bean in question eagerly, everything is dependent upon your project's requirements such as injecting a bean/other services into another bean. No? – Tiny Jan 13 '15 at 16:07
  • If we have two viewscoped beans, and inject B into A, why B will be serialized two times? – benz Jan 13 '15 at 17:05
  • Nope. That bean won't be serialized twice. Please pay special attention to the "*on the same view*" part of the below answer, if you got this impression from that answer. – Tiny Jan 13 '15 at 18:23
  • If it won't be serialized twice, than why its a bad practice? – benz Jan 13 '15 at 18:29
  • I never said it is a bad practice. It is not something extreme that you need to take into special consideration. – Tiny Jan 13 '15 at 18:30
  • 3
    Something about serialization : http://stackoverflow.com/a/3372719/1391249, http://stackoverflow.com/a/20078600/1391249 – Tiny Jan 13 '15 at 18:38
  • @Tiny, thanks for sharing links. The second link is somewhat near to what i am asking. When we say proxies are better for ManagedBeans, what does it mean. I am really confused into these concepts. – benz Jan 13 '15 at 18:48
  • EJBs and CDI-like artifacts follow the proxy design pattern. "*Another difference is that CDI actually injects proxies delegating to the current instance in the target scope on a per-request/thread basis (like as how EJBs are been injected). This mechanism allows injecting a bean of a narrower scope in a bean of a broader scope, which isn't possible with JSF `@ManagedProperty`. JSF "injects" here the physical instance directly by invoking a setter (that's also exactly why a setter is required, while that is not required with `@Inject`*" [Here](http://stackoverflow.com/a/4347707/1391249). – Tiny Jan 13 '15 at 18:57
  • 1
    *I see our application behaves very slow in Performance Testing environment* there could be lot of causes for this. Since you don't show anything about the architecture and the design of your project, it's really hard for us to provide specific help . – Luiggi Mendoza Jan 13 '15 at 20:48
  • 2
    Run a profiler to learn what parts exactly are causing that "very slow". – BalusC Jan 13 '15 at 21:33

1 Answers1

0

AFAIR it's not a good practice if serialization is enabled - which is required if you use client side state or session replication/clustering.

e.g. if you have 2 viewscoped beans on the same view ("A" and "B") and inject "B" into "A". "A" will be serialized 1 time and "B" will be serialized 2 times.

This problem will not occur if you CDI because only proxies will be injected and not the real instance.

tandraschko
  • 1,705
  • 10
  • 9