This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Spring and the fly likes doubt on Objects created using Spring object factory and ones created inside class when using Spring Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "doubt on Objects created using Spring object factory and ones created inside class when using Spring" Watch "doubt on Objects created using Spring object factory and ones created inside class when using Spring" New topic
Author

doubt on Objects created using Spring object factory and ones created inside class when using Spring

Monica. Shiralkar
Ranch Hand

Joined: Jul 07, 2012
Posts: 639
Does using Spring mean that we do not have to create any objects in the classes (Objects will be made available by spring framework on requirement) and that each and every object we will be using will be instantiated this way with Spring framework acting as object Factory.
Reason for my doubt is that I saw some some code in which certain objects were created by Spring framework and certain objects were being created in class itself. So I mean to ask in Spring each object needed will come from spring object factory only or only some.
thanks.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30352
    
150

No. Spring only needs to create the objects that will be managed by Spring. It is common to have Spring create the objects that have dependencies like a database call. And regular local variable instantiation for helper objects that are just Java; you don't need to mock out, etc.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Monica. Shiralkar
Ranch Hand

Joined: Jul 07, 2012
Posts: 639

Thanks as you said "No. Spring only needs to create the objects that will be managed by Spring. It is common to have Spring create the objects that have dependencies like a database call"

I understand that such objects should not be created inside class to avoid tight coupling and be provided from bean xml configuration.

Now on
" And regular local variable instantiation for helper objects that are just Java; you don't need to mock out, etc."


[i]Suppose there is a regular local object being created ...
If I create this object inside any class is that not causing tight coupling.
[/i]

that not mean that each and every object created should be from xml configuration and not inside any class because that would cause Tight coupling.

thanks
Monica. Shiralkar
Ranch Hand

Joined: Jul 07, 2012
Posts: 639
every single object i create inside a class adds to Tight coupling.Doesn't that have to be replaced by usage of Spring Setter injection to remove coupling. Doesent that apply for each and every object created in spring application inside a class?

thanks
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

There is a distinction between the type of classes that you create. In your last example, that looks like a domain object based on data. I would never create that as a Spring Bean as it holds state all the time. Typically objects that are stateless are what you create as Spring Beans like Services, DAO/Repositories, COntrollers, etc. Those you only need one instance for your app and dependency injection between them. Typically for the domain objects you cretae then with new, or use an ORM or Mapper interface to instantiate the domain objects.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: doubt on Objects created using Spring object factory and ones created inside class when using Spring