views:

45

answers:

2

I am wiring my UserService type classes using spring's IOC.

But what about my User class?

I have a interface User, then a UserImpl class.

In my controller action's do I just do:

User u = new UserImpl(); 

Or would it sometimes make sense to use IOC for this also?

Sometimes I use a different constructor also when instantiating a class, based on some conditions. I guess your stuck in these situations?

+3  A: 

It will not make sense to use dependency injection or IOC for your business objects like User because business objects are not the dependencies of the class they are the part of the class using them.

Shekhar
+1  A: 

Spring IOC, by default, will create Singletons for you. Which means all user threads using your app will share that single instance of class. This is generally fine for Service type classes. If needed this singleton behavior can be changed to object per request (prototype), but this will cause you to change this setting for the users of the non-sigleton object as well.

Domain/business classes are state-full, it's easiest to create such objects once-per-request in order to avoid concurrency issues.

Shikhar