Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>I could write anal setters that will tolerate themselves being called in many orders but that will not solve the problem of a user forgetting to set the appropriate setter and therefore the bean being in an incomplete state.</p> </blockquote> <p>The Spring-specific solution to this is to declare the bean as implementing <code>InitializingBean</code> and then implement the necessary checking and back-filling of defaults in the bean's <code>afterPropertiesSet()</code> method. You can even wire this up so that programmers have the choice of using constructors with arguments or setters, and (maybe) have the argument-full constructors call <code>afterPropertiesSet()</code>.</p> <p>The only gotchas are:</p> <ul> <li><p>your codebase becomes Spring dependent by virtue of implementing <code>InitializingBean</code>, and</p></li> <li><p>if a developer configures the beans by hand using the setters they have to remember to call <code>afterPropertiesSet()</code> when they are done.</p></li> </ul> <p><strong>EDIT</strong></p> <p>The <code>afterPropertiesSet()</code> method also gives a neat solution to your example with calculating the id. The problem with the way you have currently written it is that it implicitly constrains you to building the tree from the top (root) down. For example, suppose you have a multi-level tree, and you set a leaf node's parent attribute <em>before</em> you set the parent node's parent attribute. This will trigger the <code>id</code> setting algorithm in the leaf node, which will wrongly think that the parent node is a root node.</p> <p>In Spring you can arrange that the setting of the <code>parent</code> attributes is done in the implementation of the relevant setChild / setChildren methods of the parent nodes. The setting of the <code>id</code> attributes would then be done in the <code>afterPropertiesSet()</code> method. Since this gets called <em>after</em> all explicit and auto-wire property settings have been applied, the parent/child relationships for the tree will have stabilized, and so the <code>id</code> calculations will be correct.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload