Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Rahul Shivsharan
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • 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 ?
 
Jeevan Kulkarni
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Rishi Shehrawat
Ranch Hand
Posts: 218
Hibernate Java Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic