• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

setBeanFactory of BeanFactorAware not getting invoked

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a class which implements BeanFactoryAware and thus i have overrided the method


but its not getting invoked and throws null pointer exception. I would ike to know how is BeanFactoryAware located and resolved in Spring?
 
Ranch Hand
Posts: 66
VI Editor Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From the code you've posted, the only place the NPE could occur is at "this." and 'this' being null. Did you see any other error during the context loading period or can you load this bean the last so that we're sure all other beans are loaded before we hit this point?
 
kv ruby
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prabaharan Gopalan wrote:From the code you've posted, the only place the NPE could occur is at "this." and 'this' being null. Did you see any other error during the context loading period or can you load this bean the last so that we're sure all other beans are loaded before we hit this point?



sorry guess i had to explain more..NPE is not at the above code...i have tried the before mentioned code and the one below


and i have the same issue.

because applicationContext is not set at setApplicationContext method am not able to call a bean using getBean in my code and consecutively it throws an NPE. My issue is am not sure when is setApplicationContext method called. Should we explicitly call it or would the container initiate that call.
 
Prabaharan Gopalan
Ranch Hand
Posts: 66
VI Editor Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's still not clear on where you have this code (the two snippets - are they on the same file/different classes, how they're wired, what's your application-context looks like,..) and what you're trying to achieve.

I'd suggest you start with a smaller stand-alone Java application with Spring and have the bare minimum classes and try to achieve what you're trying to do now. That way you know what's going on and even if that doesn't work, you can share it here. (it might not be okay to paste your company's code here, so a prototype would be ideal, if things don't go well).
 
kv ruby
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prabaharan Gopalan wrote:It's still not clear on where you have this code (the two snippets - are they on the same file/different classes, how they're wired, what's your application-context looks like,..) and what you're trying to achieve.

I'd suggest you start with a smaller stand-alone Java application with Spring and have the bare minimum classes and try to achieve what you're trying to do now. That way you know what's going on and even if that doesn't work, you can share it here. (it might not be okay to paste your company's code here, so a prototype would be ideal, if things don't go well).



Ohh its that earlier i tried getting bean by beanFactory but then i changed to applicationContext. My class looks like below


My isue is when i call the method addLock() the getBean returns null and hence deferredUnlocker.getTransactionId() gives null pointer
 
Prabaharan Gopalan
Ranch Hand
Posts: 66
VI Editor Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At what point this code gets called? Are you sure that the applicationContext has finished loading by then? This could happen easily if you have Bean A which calls a Util class which tries to get Bean B from Application Context. But, there is always the chance that Bean B hasn't been loaded yet. You might want to explore the Application Context's life cycle to understand how things work before do this.

And did you wire DeferredUnlockerTest.class in your application context? And if you did, what's the issue in injecting that via the usual Spring way instead of ApplicationContextAware and then all the trouble of context.getBean()?
 
kv ruby
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prabaharan Gopalan wrote:At what point this code gets called? Are you sure that the applicationContext has finished loading by then? This could happen easily if you have Bean A which calls a Util class which tries to get Bean B from Application Context. But, there is always the chance that Bean B hasn't been loaded yet. You might want to explore the Application Context's life cycle to understand how things work before do this.

And did you wire DeferredUnlockerTest.class in your application context? And if you did, what's the issue in injecting that via the usual Spring way instead of ApplicationContextAware and then all the trouble of context.getBean()?



but my issue is in addLock() method the call doesnt pass check.thus applicationContext is not set.Now i want to know how to do that.
 
Prabaharan Gopalan
Ranch Hand
Posts: 66
VI Editor Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got to state the obvious - your setApplicationContext() is not called (I have no proof to say otherwise). you can put a System.out.println inside it to confirm. In that case, could you check this one out: http://www.manning-sandbox.com/message.jspa?messageID=55882 .

It's going to be tough for anyone who is looking at this thread. too many back-forth messages. If the link doesn't help in any way, there are two things you can do:
1. Build a small prototype and see if that works. If that doesn't, you can post it here and we can take a look. If it does, you can probably look for differences between your actual code and the prototype.
2. Give a complete overview of
a. how your beans are defined.
b. how's the bean definition getting loaded (web application/file system/classpathXmlApplicationContext)
c. can you find a way around to avoid using the AppContextAware implementation? may be a bean post processor?
 
kv ruby
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
am quite new to Spring and thus was totally clueless what is going wrong.

Its resolved now I didnt know that bean has to be added in spring configuration file.

Its solved now
 
Prabaharan Gopalan
Ranch Hand
Posts: 66
VI Editor Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I doubted that and that's why i asked specifically this:

And did you wire DeferredUnlockerTest.class in your application context?



Glad it's done now. Happy Springing.
 
kv ruby
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If only i could have got that point i dint understand that part and overlooked it. nyways thanks.
 
reply
    Bookmark Topic Watch Topic
  • New Topic