| Article: |
Twelve Best Practices For Spring XML Configurations | |
| Subject: | Good Citizen | |
| Date: | 2006-01-26 06:36:31 | |
| From: | wireframe | |
|
I disagree that setter injection is better than constructor injection. Failing fast at startup if dependencies are not provided, is always better than failing at runtime if a developer forgot to set the dependency.
|
||
Showing messages 1 through 2 of 2.
-
Good Citizen
2006-02-08 10:43:49 wpoitras [View]
-
Good Citizen
2006-02-01 06:41:03 kinko [View]
I would agree that constructor injection is advantageous in a number of circumstances. Especially when you are looking for strict encapsulation i.e. Immutality. Often the config using setters is clearer but you are potentially breaking encapsulation for no other reason than to configure.



Setter injection makes your XML more readable because you know what properties you are setting without consulting the source. But for simple cases, constructor injection can make your classes more concise and not require an init-method.