This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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.
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.
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.
" 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.
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
Joined: Jul 07, 2012
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?
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.