Why does Spring dependency injection have an init-method?

I come from the .NET background using dependency injection containers like Unity, Ninject, Castle Windsor, etc. I recently started learning how to use Spring dependency injection features for Java.

In the course of Spring training, I saw that the concepts of "init-method" and "destroy-method" can be specified in the XML configuration of the bean.

The purpose of the init-method statement is to perform any installation at the creation time bean that you might want to do. Here where I am confused. Why do you need a separate method to complete the installation, and not just use the constructor to perform any configuration necessary for the object, for example, will a normal / good object-oriented design dictate?

In other words, if a class requires a dependency, shouldn't it be injected into the constructor, which, as you know, was called, while the object can exist in the state without calling it the "init-method",

+4
source share
3 answers

There are several cases where you need a separate init() method:

  • deprecated APIs in which you simply have no choice

  • initialization having some side effects, for example. starting with Thread , connecting external som resources

    This actually has even deeper consequences: when using proxy classes based on a class (via ), the constructor of your base class is called twice (the second time for a proxy server that has the inheritis property from your class) - the init() method is called only once times at the final object.

  • you should not make virtual calls in the constructor (this should be forbidden by the compiler ...)

  • sometimes you have to use the setter / field insert (although I like the constructor injection), for example. when the aforementioned class-based proxy classes are used

using the constructor to perform any configuration necessary for the object, as it requires a normal / good object-oriented design?

This is actually not the best practice to put all the initialization code in the constructor. Side effects in the constructor make testing and mocking a lot harder. Instead, focus on creating objects in a stable and well-known state. Thus, you can, for example, separate the creation of the object that manages the connection pool and the physical connection to this pool.

BTW destroy() is a blessing in a language without destructors, because it allows you to gratefully close external resources, interrupt threads, etc. Use it often.

+7
source

Why do you need this?

The init method is called after all the bean properties have been set. This is usually necessary if the bean needs to do some initialization or property checking, which can only be done after all the properties have been set. (If you try to do this without calling the init callback, you will find that each property setter needs to check whether other setters have been called, etc. And even this strategy fails if initialization can only be completed after all properties in the beans loop.)

The destroy method is required if the bean contains resources that must be explicitly released; for example file descriptors, network sockets, database connections.

... how will a normal / good object oriented design dictate?

Any design methodology that dictates that the initialization and destruction of events / methods is โ€œwrongโ€ or โ€œforbiddenโ€ is unrealistic and should be ignored. In fact, object-oriented design methodologies usually do not dictate this. At best, they would say that this kind of thing is usually not needed.

In addition, DI actually changes the rules for the design methodology a bit ... at least in terms of initialization. In particular, by externalizing the "wiring" of instances, it pulls most of the logic out of the code in such a way that classical OO design methodologies do not expect. In any case, this suggests the need to revise the classical OO methodologies in the light of dependency injection.

+5
source

If you use the setter installation, then for initialization it is necessary to initialize the init / InitializingBean / @ PostConstruct method after all the properties have been set / auto-updated. Otherwise, you have a constructed object, but not related to dependency. If you use constructor injection, this is rarely necessary, in the cases indicated by Tomas and Stephen.

0
source

All Articles