10

What is the most concise way of making sure that a Spring bean has all properties set and the init method called?

I'll favour answers which use setter injection and XML configuration, since that's what I'm using right now.


I'm trying to evade the case where I either forget to configure a setter or call the init-method.


In future projects I would favour skaffman's response, but I've chosen the one which suits me right now.

4 Answers 4

8

This poll is exactly what you are looking for.

Here are the results:

  • By using the dependency-check attribute in XML: 11.52%
  • By using the @Required annotation (or a custom annotation): 21.40%
  • By using InitializingBean and an assert facility: 23.87%
  • By using init-method and an assert facility: 14.40%
  • I don't have to, because I use constructor injection for required properties: 19.34%
  • I check my dependencies in my business methods: 7.41%
  • I don't check required dependencies: 34.16%
3
  • +1: Interesting ... although surprised that the top vote is "I don't check required dependencies".
    – Adamski
    Jul 9, 2009 at 14:22
  • 1
    I don't check either - but I want to. This is why I asked the question. Jul 9, 2009 at 14:25
  • That's a pretty old poll, also, so Spring developers will be more aware of the likes of @Required now.
    – skaffman
    Jul 9, 2009 at 14:27
6

Use the @Required annotation on the setter methods. Spring will then check that they've all been set without you having to check manually.

Alternatively, annotate your init methods with @PostConstruct, and Spring will invoke them for you.

2
  • Wouldn't spring set all the dependencies before giving you a bean? Oct 17, 2010 at 20:10
  • 1
    @fastcodejava not if someone didn't set up the Spring config correctly/completely. Easy to make mistakes... nice to find out at deploy-time, not when the app blows up in front of a user :)
    – brabster
    Jan 7, 2011 at 19:23
5

You can add the dependency-check attribute to your bean definition to ensure that all objects / primitives / both have been set.

<bean id="myBean" class="com.foo.MyBean" dependency-check="objects"/>

Skaffman's answer gives more control, but does introduce a compile-time dependency on Spring which you may / may not want.

4
  • "objects" means that Spring will check that only properties for objects (not primitives) have been set.
    – Adamski
    Jul 9, 2009 at 14:20
  • Hmm, I didn't know you could do that, that looks like hard-core Spring 1.2 stuff :)
    – skaffman
    Jul 9, 2009 at 14:24
  • This method also requires you to remember to add the dependency-check attribute to each bean definition, so it's still prone to forgetfulness.
    – skaffman
    Jul 9, 2009 at 14:29
  • 1
    @Adamski: There's also default-dependency-check and default-init-method/default-destroy-method. You might want to include those in your answer. Jul 9, 2009 at 14:41
2

Use the following attributes of the beans tag to make sure dependency checking and the init method is called on all beans, but it rather assumes you don't call your method "innit".

<beans default-init-method="init" default-dependency-check="objects">

see link

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.