File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Injecting Beans as Parameters (Injected Bean reference is null)

 
V. Rentschler
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I am getting into JSF.. or rather not. I am trying to write a simple Application, but I just cannot get it to work. If anyone looks over this, I thank you for your time in advance!
I deploy on Jetty and my JSF-Engine is MyFaces, though I doubt that it matters.

I try to write a simple "Guestbook" application which allows an own guestbook for every user, which only he can access.
I managed to write a "NewEntry.jsp", and it works fine.

Classes I wrote:
Guestbook - Guestbook Entity, contains a Vector of...
GuestbookEntry - GuestbookEntry Entity, contains all the data of an entry as strings. I also used it as Backing Bean for adding a new Entry, dunno if that's good.. (but it worked)
User - A User (contains UserID and the username etc.)
GuestbookSvc - A managed application scope bean. So it should be Singleton..(?) manages login, holds user passwords, user guestbooks, etc.
LoginBean - A managed session scope bean. Bascially the backing bean for my login site + it should store if the user is logged in obviously

In the GuestbookSvc constructor I create some dummy data.

Now, as you can see I need the LoginBean to access the instance of GuestbookSvc. As I have no factory class, I inject it over a parameter.
Let me show you the managed beans in my faces-config.xml:


The GuestbookSvc is created succesfully, so there is no problem in that area.
Here is the code of my LoginBean:


My login-site (guestbook.jsp) looks like this:

(As you can see I actually access the guestbooksvc there, to retrieve my dummy users...)

Now, my looger says: LoginSvc: gbsvc is null. Anyone has a clue why gbsvc *is* null?
Thanks in advance,

sincerly yours
V. Rentschler
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because managed property injection only happens after construction.

JSF cannot do loginBean.setGbsvc(gbsvc) without doing LoginBean loginBean = new LoginBean() first.

Edit: if you actually want to do some logic after construction AND managed property injection, then use a method which you annotate with @PostConstruct. This method will be invoked after managed property injection.

E.g.

This is possible since Java EE 5 / JSF 1.2 only though.
 
V. Rentschler
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bauke Scholtz wrote:JSF cannot do loginBean.setGbsvc(gbsvc) without doing LoginBean loginBean = new LoginBean() first.


Oh my... it's pretty simple, isn't it? No magic happening...

Thank's alot!
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic