Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Injecting the properties of object through Spring which is created outside the Spring container

 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My bean-cfg-Two.xml,


My Manager.java which is annotated by @Configurable


My MainPractise19.java having the main method,


in the above code i have created Manager object outside the spring container,
but what i expected was the properties of the Manager object will be injected by the spring container,

but still i am getting a null pointer exception in Managers income() method ant emp.income() line, why ?

Whether spring has injected the properties ?

I am missing some thing ?
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Spring container will inject values into Manager only if the instance of the class created is obtained from spring application context i.e. by doing getBean.

But I do not see any bean defined with id="empl-inj" .. in the commented code.

obj = (Manager) applicationContext.getBean("empl-inj");

Thanks,
Jeevan
 
Rahul Shivsharan
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
forget the commented code,

i repeat my question

the object of Manager is created outside the Spring container,

but still Spring Container should be able to inject the properties in object Manager as it is anotated @Configurable,

but the properties are not getting injected ?
why ?

do i am missing any xml schema definition or a jar,

i have inccluded all spring 3.0 jars plus
aopalliance-1.0.jar
aspectjrt-1.0.5.jar
aspectjweaver-1.5.2.jar


so why nullpointer exception
 
Ranch Hand
Posts: 218
Hibernate Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Spring can only manage dependencies of objects created/managed by Spring. By using new operator you are bypassing Spring, this is the reason properties are not getting injected.
Your understanding of @Configurable is incorrect. Please refer to Spring docs.
 
And then we all jump out and yell "surprise! we got you this tiny ad!"
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic