Monday, July 6, 2015

Use a new keyword and still inject the object : Spring

Spring works on dependency injection. This means there is no need to create an object explicitly, spring will do it for you.

But there are situations where you have to create new objects (ex., Reflection). By going with this approach you are conveying string that you will have control over the current object. i.e., any Autowired fields inside the object class will be null and spring will not consider injecting it. 

But there is a way to autowire those fields as well,

appctx.getAutowireCapableBeanFactory().autowireBean(wsdlParserAPIObject);

where appctx is ApplicationContext which is autowired.

http://stackoverflow.com/questions/31237062/using-class-forname-but-want-to-autowire-members-of-the-target-class

No comments:

Post a Comment