• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt with Dependency Injection

 
cesar vasde
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 426
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
cesar vasde
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My doubt is because the netbeans marks the object "libroBean" as Target Unreachable



How do I fix this?
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18277
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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.
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18277
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 18277
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 18277
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 18277
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As long as you're injecting that property into libroController.

Or you can use the Annotation instead of faces-config.xml:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic