0

I have a bean with autowired beans.

So something like:

class A
{
    @Autowired
    B b;

    @Autowired
    C c;

    void function()
    {
       // here I would like to do something when I an sure the wiring has been done
       // being sure that I won't wait forever
       ...

Something has to exist, but I can't find it.

Thanks for your help!

unludo
  • 4,912
  • 7
  • 47
  • 71

3 Answers3

3

You can annotate your 'function' method with @PostConstruct and specify <context:annotation-config/> in your spring config XML. Then, function will only be invoked after autowiring, so you could check in function whether your beans have been injected successfully.

Csaba
  • 965
  • 9
  • 20
1

A classic way to achieve this is to implement InitializingBean:

Interface to be implemented by beans that need to react once all their properties have been set by a BeanFactory: for example, to perform custom initialization, or merely to check that all mandatory properties have been set. An alternative to implementing InitializingBean is specifying a custom init-method, for example in an XML bean definition. For a list of all bean lifecycle methods, see the BeanFactory javadocs.

I also suggest reading other answers:

Community
  • 1
  • 1
nobeh
  • 9,784
  • 10
  • 49
  • 66
0

If you are using default scope for beans that is singleton, then autowiring will always be done at the application startup only. If wiring for any field fails, then spring container will throw an exception and the application will not start properly. So, if the control of code is in your method , it means wiring has already been done.

Sumit Desai
  • 1,542
  • 9
  • 22
  • The goal is to start a thread once autowiring of the 2 attributes is done. So I need a kind of event for this. The @PostConstruct fulfills this need. – unludo May 27 '13 at 08:24