tags:

views:

897

answers:

2

I see that Spring has a @Required annotation to mark member variables in beans that must be set.

Is there a best practice for using this?

For instance, might it be better to set these values in the constructor and make these parameters explicitly required (esp. when used outside of Spring)?

Thanks!

+2  A: 

While not referring to the @Required annotation directly, Martin Fowler provides this advice...

He prefers to set object values in the constructor rather than in setters as it will "give you a clear statement of what it means to create a valid object in an obvious place" while it also "allows you to clearly hide any fields that are immutable by simply not providing a setter".

Vinnie
A: 

See the following poll on Spring Community forums for a discussion of approaches.

toolkit