Instantiating spring bean objects


I've been playing with Spring and had a quick question...

I have a loop within class A which instantiates new objects of class B. To do this I've used the new operator however I cannot reference any Spring beans injected into instances of class B as I get a null pointer exception. I think I understand that this would be due to spring not managing these instances as beans and therefore not being able to manage the lifecycle however I was just wondering what the best way to go about creating multiple instances would be i.e. should I used appContext.getBean("beanA"); ?


First - are right with your assumptions. Using new means spring doesn't manage the object.

Solutions can be:

<ul><li>appContext.getBean("beanA"), where the bean is of scope "prototype". You obtain the appContext by injecting it, or by implementing ApplicationContextAware</li> <li>using @Configurable and apsectJ weaving. That way even objects instantiated with new become managed by spring (the weaver plugs into the compiler or the vm)</li> <li>using a <a href="http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/beans.html#beans-factory-lookup-method-injection" rel="nofollow">lookup-method</a> - it's the same as the first option (again requires prototype-scoped bean), but you get a method of your class that returns a new instance each time you call it.</li> </ul>

Normally, however, you shouldn't need that. In the rare cases you do, I'd recommend the 3rd option.


  • Android Get the markers that are not clustered
  • youtube video insert “onBehalfOfContentOwner” parameter value
  • How do I manage org and space users in bluemix using cf command line?
  • Capturing HTML Text Input Key press after key has been applied?
  • Number of nodes returned in MutationRecord.addedNodes nodelist (mutationObserver)
  • Recommended way to remove events on destroy with jQuery UI Widget Factory
  • Differences between drawing an Ellipse in Android and Java
  • Get rendered html code in Backing Component from Composite Component
  • Best way to produce 'colour correct' raster from a PDF or PS with ghostscript
  • In java, how do you retrieve images from a jar file?
  • Mongodb update() vs. findAndModify() performace
  • GitHub default README markup
  • SSIS Designer is running VERY slowly
  • Create registry key in 32-bit hive on x64 PC using Installshield 2012 LE - Avoid redirection
  • How to add the custom button on google's(device) native application in android?
  • Vigenere cipher not working
  • What is this strange character in chrome's resource css viewer?
  • Configuring multiple DefaultJmslistenercontainerfactory
  • How to repeat sections of a SQL query across UNIONs? (DRY in SQL)
  • Vuejs: Lifecycle hooks of child routerview components using keep alive
  • Compare struct to a constant in C
  • C# - Most efficient way to iterate through multiple arrays/list
  • Django Haystack Rebuild Index
  • Consuming a WCF service in a Java Client using wsHttpBinding
  • JPA flush vs commit
  • npm 5.4.1 install/uninstall all failing
  • Authentication in Play! and RestEasy
  • What is the purpose of TaskExecutor in spring?
  • Installing Apache MyFaces 2 on WildFly 8.2.0
  • Using jQuery closest() method with class selector
  • Array.prototype.includes - not transformed with babel
  • javascript inside java/jsp code
  • SetUp method failed while running tests from teamcity
  • Python: how to group similar lists together in a list of lists?
  • Android Studio and gradle
  • Why can't I rebase on to an ancestor of source changesets if on a different branch?
  • What are the advantages and disadvantages of reading an entire file into a single String as opposed
  • Understanding cpu registers
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • How can i traverse a binary tree from right to left in java?