Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making sure a Spring Bean is properly initialised

Tags:

spring

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.

like image 760
Robert Munteanu Avatar asked Jul 09 '09 14:07

Robert Munteanu


1 Answers

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%
like image 58
kgiannakakis Avatar answered Sep 27 '22 23:09

kgiannakakis