This week's book giveaway is in the Java in General forum.
We're giving away four copies of Event Streams in Action and have Alexander Dean & Valentin Crettaz on-line!
See this thread for details.
Win a copy of Event Streams in Action this week in the Java in General 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Creating a new instance of Autowired class in specific classes

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using Spring 3.2 and using @Autowired annotation for injecting the beans into classes and component scanning for bean instantiations. When i do this spring creates one instance of the bean and wires it into all the classes where they are referenced. But in some scenarios, i want a separate instance of the injected class to be wired into certain classes. How do i do this without having to work with XML configurations? Also if this newly created class has other beans autowired into it, will a separate instance be created for them as well or a singleton class used ?


ClassX{
}

ClassA
{

@Autowired
ClassX classX;

}

ClassB{

@Autowired // Instance 1 of ClassA
ClassA classA ;

}


ClassC{

@Autowired // Instance 1 of ClassA
ClassA classA;

}

ClassD{

@Autowired // New instance 2 of ClassA
ClassA classA; // What should i do to use 1. instance 1 of ClassX within ClassA 2. new instance 2 of ClassX within ClassA
}

 
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want multiple instances of the same class, you can declare multiple beans of the same type in the spring config. When you autowire them, use the Qualifier annotation.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!