| Author |
Injecting the properties of object through Spring which is created outside the Spring container
|
Rahul Shivsharan
Ranch Hand
Joined: Jun 17, 2009
Posts: 83
|
|
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
Joined: Sep 03, 2011
Posts: 1
|
|
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
Joined: Jun 17, 2009
Posts: 83
|
|
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
Joined: Aug 11, 2010
Posts: 218
|
|
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.
|
 |
 |
|
|
subject: Injecting the properties of object through Spring which is created outside the Spring container
|
|
|