• 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
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

should arrayList object in Spring Controller also be created using Spring IOC only?

 
Ranch Hand
Posts: 1364
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a controller class which has the below line of code:

List productList=new ArrayList();


should this list object also be populated using Spring IOC or using new operator is fine..

thanks
 
Ranch Hand
Posts: 672
4
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Should be OK to use new operator.

BTW, What is the purpose of this list?
 
Monica Shiralkar
Ranch Hand
Posts: 1364
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This list is an array List which stores the list of Products to be displayed in the results JSP page.

I thought all objects which were supposed to be created using new operator should instead be created by Spring IOC. Is it not so?Then when should be used spring IOC instead.What is the practical usage of it

thanks
 
Prasad Krishnegowda
Ranch Hand
Posts: 672
4
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If that is the use case, then it is OK to use new operator.

By use of Spring DI, it does not mean we should not use new operator at all.
when we need to inject the dependencies, initialize properties, spring DI is useful as it comes handy in case of unit testing, wherein we can pass the types we need to test, without altering our java code..
Spring also does many things such as handling the scope of the beans..



 
Monica Shiralkar
Ranch Hand
Posts: 1364
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prasad Krishnegowda says

By use of Spring DI, it does not mean we should not use new operator at all.



Thanks.What is the issue if we using Spring DI all the time for creating objects and not use new operator at all?
 
Prasad Krishnegowda
Ranch Hand
Posts: 672
4
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There will be no issue, we can use Spring DI only, but, do you wish to create spring beans for all ArrayList, HashMap, HashSet and all such collections you tend to use in your application?
 
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
SPeaking in generalities here:- You should design the application so that the user state is in a class that is seperate from logic. So, for example, if you are building a web page that allows user to view, create and edit a Customer, you should have a POJO class that represents a Customer seperate from the DAO class that stores customer in database, and the Controller class.

When you are developing apps in SPring, (again speaking very generally), you should declare the beans that have logic in the spring XML. Instances of classes that have user state should generally not be Spring beans.. There are 2 reasons for this

a) Spring defaults the beans to singleton scope.. and instances that carry user state should never be singleton scoped. If you do make Instances that carry user state as singleton scoped, you will run into concurrency problems. SUre, you could make them request scoped. But then you add complexity.. which is needless. Remember that autowiring a request scoped bean into a singleton bean isn't straightforward. You have to mess around with scoped-proxies and shit.. It's unneccesary complication!

You can easily do new on the class, and pass the instance as a parameter to another method.

b) You shouldn't need dependency injection into beans that carry user state. Remember, they are POJOS. They shouldn't need other beans. Other beans like your controller and DAO might need them.. but they shouldn;t need other beans. If you do have situation where a POJO needs to call a service, then you have bad design. POJOs that carry user state are suppossed to be dumb.
 
Monica Shiralkar
Ranch Hand
Posts: 1364
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks
 
I've got no option but to sell you all for scientific experiments. Or a tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!