CDI @Inject Named bean in another Named bean

6

I'm using jsf 2.2. I have 2 @Named beans in @ViewScoped

     @Named(value = "menuMB")
     @ViewScoped
     public class MenuMB implements Serializable{

     }
     @Named(value = "produtoGeralMB")
     @ViewScoped
     public class ProdutoGeralMB  implements Serializable{
       @Inject
       private MenuMB menuMB;
     }

I have a method that changes a property of produtoGeralMB , but on page jsf if I use an inputText menuMB , after ajax there is no change, if I use #{menuMB.minhaPropriedade} it works fine

I noticed that the CDI works with proxy for the injected object. Is there anything so that on the page I use #{produtoMB.menuMB.minhaPropriedade} and not #{menuMB.minhaPropriedade} ? Why does this happen if there is only a single instance of #{produtoMB.menuMB.minhaPropriedade} in memory?

    
asked by anonymous 25.02.2014 / 14:09

1 answer

2

Use the javax.faces.view.ViewScoped annotation in JSF 2.2 instead of the traditional javax.faces.bean.ViewScoped of the CDI and everything will work correctly.

In fact, we recommend discontinuing the use of all annotations in the javax.faces.bean package (which was the default in JSF 2.0):

DA API :

  

The annotations in this package may be deprecated in the future version of this specification because they duplicate functionality provided by other specifications included in JavaEE. When possible, the corresponding annotations from the appropriate Java EE specification should be used in preference to these annotations. In this case, the corresponding annotation is javax.faces.view.ViewScoped . The functionality of this corresponding annotation is identical to this one, but it is implemented as a CDI custom scope.

That can be freely translated to:

  

Annotations in this package may be deprecated in a future release of this specification because they duplicate features provided by other specifications included in Java EE. When possible, the corresponding annotations of the Java EE specification should be used in preference to these annotations. In this case, the corresponding annotation is javax.faces.view.ViewScoped . The functionality of this corresponding annotation is identical to this, but it is implemented as a custom CDI scope.

Reference: JSF SPEC PUBLIC 1087

    
26.02.2014 / 01:35