Constructor and installation design

I am currently developing an API where I want to enable customization using various methods. One of the methods is through the XML configuration scheme, and the other is through the API, which I want to play well with Spring.

My XML Schema syntax code was previously hidden, and so the only problem was for it to work, but now I want to create a public API, and I am very concerned about best practice.

Many seem to prefer a javabean of type PoJo with default null parameter constructors and then setting the injection. The problem I'm trying to solve is that some implementations of setter methods depend on other setter methods that are called before them in sequence.

I could write anal setters that will endure themselves, called in many orders, but this will not solve the problem that the user forgot to install the corresponding setter and, therefore, the bean is in an incomplete state.

The only solution I can think of is to forget about the objects, 'beans', and force the required parameters using the constructor insert.

An example of this is the default value for a component identifier based on the identifier of the parent components.

My interface

public interface IMyIdentityInterface {
    public String getId();
    /* A null value should create a unique meaningful default */
    public void setId(String id);
    public IMyIdentityInterface getParent();
    public void setParent(IMyIdentityInterface parent);
}

Basic implementation of the interface:

public abstract class MyIdentityBaseClass implements IMyIdentityInterface {
   private String _id;
   private IMyIdentityInterface _parent;

   public MyIdentityBaseClass () {}

   @Override
   public String getId() {
      return _id;
   }

   /**
    * If the id is null, then use the id of the parent component
    * appended with a lower-cased simple name of the current impl
    * class along with a counter suffix to enforce uniqueness
    */
   @Override
   public void setId(String id) {
      if (id == null) {
          IMyIdentityInterface parent = getParent();
          if (parent == null) {
              // this may be the top level component or it may be that
              // the user called setId() before setParent(..)
          } else {
              _id = Helpers.makeIdFromParent(parent,getClass());
          }
      } else {
          _id = id;
      }
   }

   @Override
   public IMyIdentityInterface getParent() {
      return _parent;
   }

   @Override
   public void setParent(IMyIdentityInterface parent) {
      _parent = parent;
   }

}

Each component in the structure will have a parent, with the exception of the top-level component. Using the type of setup for injection, setters will have different behavior based on the order in which the setters are called.

, , , , ? , IoC?

+3
4

, . , null ...

: /* A null value should create a unique meaningful default */

"" setId() -, . setId(), getId() null, setId(null), getId() ?

- , , :

public abstract class MyIdentityBaseClass implements IMyIdentityInterface {
    private String _id;
    private IMyIdentityInterface _parent;

    public MyIdentityBaseClass () {}

    @Override
    public String getId() {
        return _id;
    }

    @Override
    public void setId(String id) {
        _id = id;
    }

    @Override
    public IMyIdentityInterface getParent() {
        return _parent;
    }

    @Override
    public void setParent(IMyIdentityInterface parent) {
        _parent = parent;
        if (_id == null) {
            // if id isn't already set, set it to the generated id.
            _id = Helpers.makeIdFromParent(parent, getClass());
        }
    }

}

, , . , ... , - , , setParent() . Container/Component ( ) . (, , .)

BTW. , IInterface _name Java. API, , .

+2

IOC . contrucxtor setter , . ( node),

+2

, , , , , , bean .

Spring - bean InitializingBean, bean afterPropertiesSet(). , () , afterPropertiesSet().

:

  • Spring InitializingBean

  • beans , , , afterPropertiesSet(), .

afterPropertiesSet() . , , , (root) . , , , leaf node, parent node. id node, , node node.

Spring parent setChild/setChildren . id afterPropertiesSet(). , , / , id .

+1

, . , bean . , , bean.

, , , ( bean). , : setId(), .

, . ID bean .

, , bean, bean , IllegalStateException IllegalArgumentException.

+1

Source: https://habr.com/ru/post/1742049/


All Articles