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.

And implementing "InitializingBean" to check that the setter was called is a complete hack. It couples your service to spring for management of it's lifecycle.

Main Topics Oldest First

Showing messages 1 through 2 of 2.

  • Good Citizen
    2006-02-08 10:43:49  wpoitras [View]

    If you don't want to use InitializingBean you can always use an init-method in the bean definition.

    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.
  • 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.