File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSF and the fly likes Doubt with Dependency Injection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "Doubt with Dependency Injection" Watch "Doubt with Dependency Injection" New topic
Author

Doubt with Dependency Injection

cesar vasde
Greenhorn

Joined: Jan 01, 2011
Posts: 15
I've got following bean





Then I've this page xhtml (I will a fragment form):



Is the typical form to save data. I'm working in the Netbeans 7.0

I have the following issue:



This means that dependency injection only works with another bean. i.e. my bean LibroBean must be declared so:



And after that add annotations @ManagedProperty in the bean LibroController???

If so, because in this example here if it works without having the object to inject is a bean.

Example

Car class
TableBean class
face-config

I hope to help
Mark Reyes
Ranch Hand

Joined: Jul 09, 2007
Posts: 426
Why dont you try changing your managed bean scope from @RequestScoped to something higher like @ViewScoped or @SessionScoped.

I guess, your bean only last on one Request Cycle.

..and by the way, I am not sure if you are using Dependency Injection here, my guess is that you are only having a reference on other class.
You could use @ManagedProperty/@Inject/@EJB when injecting dependency but this is just my guess.


Sean Clark ---> I love this place!!!
Me ------> I definitely love this place!!!
cesar vasde
Greenhorn

Joined: Jan 01, 2011
Posts: 15
My doubt is because the netbeans marks the object "libroBean" as Target Unreachable



How do I fix this?
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15641
    
  15

"libroBean" is unreachable. It's an inner class object and you didn't provide a "get" accessor method for the libroBean property.

For the record, JSF backing beans are not Controllers. They are Models. A lot of people make this mistake. Most of the Controller logic in JSF is pre-written code in the FacesServlet and in the JSF tag implementations, not user-written code.

Customer surveys are for companies who didn't pay proper attention to begin with.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15641
    
  15

I almost forgot. If you're using a dataTable model wrapper, don't use Request Scope for the bean that contains it. The entire backing bean will be destroyed and re-created (improperly) on postback and the re-created model wrapper (if any) will be out of sync with the View.

Use View or Session Scope.
cesar vasde
Greenhorn

Joined: Jan 01, 2011
Posts: 15
Tim Holloway wrote:"libroBean" is unreachable. It's an inner class object and you didn't provide a "get" accessor method for the libroBean property.



My class Libro Controller does provide all the getters and setters their attributes.


So I write //Getters and Setters



i.e. my object libroBean have a method get accesor Target Unreachable

If so, because comes this error

Pd: My class Librocontroller complete is

Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15641
    
  15

I misread the original posting, which is why I thought LibroBean was an inner class.

However, the error message you're getting says:

Target Unreachable, 'libroBean' returned null


You didn't actually provide the code for the "public LibroBean getLibroBean()" method, but according to that error message, it's returning a null value. Since your "Controller's" constructor does construct and assign libroBean, the logical assumption is that somewhere something is replacing that value before the getLibroBean returns or that getLibroBean() isn't doing what we expect it to be doing.

As Mark Reyes pointed out, there isn't any evidence in your example code that you are actually even doing any dependency injection. All we can see is internal construction.

Regardless, Request scope is almost totally useless in JSF because of JSF's postback processing mechanism. Any time you have problems with stuff coming up uninitialized in JSF, the first thing to do is change the scope to session scope (or View scope in JSF2 if the same View will be retained). Nine times out of ten that will resolve the problem.
cesar vasde
Greenhorn

Joined: Jan 01, 2011
Posts: 15
I have also tried to use @ ViewScoped instead of @SessionScoped, but the results are the same :S


How can find the problem ???
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15641
    
  15

Well, according to that latest sample, your LibroController no longer constructs and sets the libroBean property variable. So unless you have a dependency injection for libroBean in faces-config.xml that you're not showing us, the libroBean property isn't being initialized. Uninitialized would mean that the getLibroBean() returns null.
cesar vasde
Greenhorn

Joined: Jan 01, 2011
Posts: 15
Tim Holloway wrote:Well, according to that latest sample, your LibroController no longer constructs and sets the libroBean property variable. So unless you have a dependency injection for libroBean in faces-config.xml that you're not showing us, the libroBean property isn't being initialized. Uninitialized would mean that the getLibroBean() returns null.


The injection I do it this way???

Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15641
    
  15

As long as you're injecting that property into libroController.

Or you can use the Annotation instead of faces-config.xml:
 
jQuery in Action, 2nd edition
 
subject: Doubt with Dependency Injection
 
Similar Threads
from-view-id with params
ERROR PropertyAccessException IllegalArgumentException occurred calling getter of...
Injection problem - not quite sure if this is something i should be using injection for
Dynamic Tree using GWT
Get the selected row in datatable [PRIMEFACES]